diff --git a/pkg/types/properties.go b/pkg/types/properties.go index f47da1e..2357a7f 100644 --- a/pkg/types/properties.go +++ b/pkg/types/properties.go @@ -82,6 +82,21 @@ func (p Properties) SetTagWithPrefix(prefix string, tagKey *string, tagValue int return p.Set(keyStr, tagValue) } +func (p Properties) SetWithPrefix(prefix, key string, value interface{}) Properties { + key = strings.TrimSpace(key) + prefix = strings.TrimSpace(prefix) + + if key == "" { + return p + } + + if prefix != "" { + key = fmt.Sprintf("%s:%s", prefix, key) + } + + return p.Set(key, value) +} + func (p Properties) Get(key string) string { value, ok := p[key] if !ok { diff --git a/pkg/types/properties_test.go b/pkg/types/properties_test.go index bce1cff..0dbbf18 100644 --- a/pkg/types/properties_test.go +++ b/pkg/types/properties_test.go @@ -295,6 +295,51 @@ func TestPropertiesSetTagWithPrefix(t *testing.T) { } } +func TestPropertiesSetPropertiesWithPrefix(t *testing.T) { + cases := []struct { + name string + prefix string + key string + value interface{} + want string + }{ + { + name: "empty", + prefix: "", + key: "OwnerID", + value: ptr.String("123456789012"), + want: `[OwnerID: "123456789012"]`, + }, + { + name: "nonempty", + prefix: "igw", + key: "OwnerID", + value: ptr.String("123456789012"), + want: `[igw:OwnerID: "123456789012"]`, + }, + { + name: "no-property", + prefix: "igw", + key: "", + value: ptr.String("123456789012"), + want: "[]", // empty properties block + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + p := types.NewProperties() + + p.SetWithPrefix(tc.prefix, tc.key, tc.value) + have := p.String() + + if tc.want != have { + t.Errorf("'%s' != '%s'", tc.want, have) + } + }) + } +} + func getString(value interface{}) string { switch v := value.(type) { case *string: