go.ligato.io/vpp-agent/v3@v3.5.0/plugins/kvscheduler/internal/test/values.go (about) 1 // Copyright (c) 2018 Cisco and/or its affiliates. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at: 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 //go:generate protoc --proto_path=. --go_out=paths=source_relative:. model/values.proto 16 17 package test 18 19 import ( 20 "google.golang.org/protobuf/proto" 21 22 . "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api" 23 . "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/internal/test/model" 24 ) 25 26 // NewStringValue creates a new instance of StringValue. 27 func NewStringValue(str string) proto.Message { 28 return &StringValue{Value: str} 29 } 30 31 // NewArrayValue creates a new instance of ArrayValue. 32 func NewArrayValue(items ...string) proto.Message { 33 return &ArrayValue{Items: items} 34 } 35 36 // NewArrayValue creates a new instance of ArrayValue with a suffix 37 // appended into each item value (but not key). 38 func NewArrayValueWithSuffix(suffix string, items ...string) proto.Message { 39 return &ArrayValue{Items: items, ItemSuffix: suffix} 40 } 41 42 // StringValueComparator is (a custom) KVDescriptor.ValueComparator for string values. 43 func StringValueComparator(key string, v1, v2 proto.Message) bool { 44 sv1, isStringVal1 := v1.(*StringValue) 45 sv2, isStringVal2 := v2.(*StringValue) 46 if !isStringVal1 || !isStringVal2 { 47 return false 48 } 49 return sv1.Value == sv2.Value 50 } 51 52 // ArrayValueDerBuilder can be used to derive one StringValue for every item 53 // in the array. 54 func ArrayValueDerBuilder(key string, value proto.Message) []KeyValuePair { 55 var derivedVals []KeyValuePair 56 arrayVal, isArrayVal := value.(*ArrayValue) 57 if isArrayVal { 58 for _, item := range arrayVal.Items { 59 derivedVals = append(derivedVals, KeyValuePair{ 60 Key: key + "/" + item, 61 Value: NewStringValue(item + arrayVal.ItemSuffix), 62 }) 63 } 64 } 65 return derivedVals 66 }