Skip to content

Commit

Permalink
feat: adding func to set with prefix for properties (#41)
Browse files Browse the repository at this point in the history
* feat: adding func to set with prefix for properties

* chore: fix golangci-lint violations
  • Loading branch information
ekristen authored Mar 8, 2024
1 parent 2563f24 commit aee80cf
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/types/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
45 changes: 45 additions & 0 deletions pkg/types/properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit aee80cf

Please sign in to comment.