Skip to content

Commit

Permalink
Preserve Traits nil value between proto<-->API type conversion (#44492)
Browse files Browse the repository at this point in the history
  • Loading branch information
smallinsky authored Jul 24, 2024
1 parent c94112b commit 62b651f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion api/types/trait/convert/v1/trait.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,21 @@ import (

// FromProto converts an array of v1 traits into a map of string to string array.
func FromProto(traits []*traitv1.Trait) trait.Traits {
if traits == nil {
return nil
}
out := map[string][]string{}
for _, trait := range traits {
out[trait.Key] = trait.Values
}
return out
}

// ToV1 converts a map of string to string array to an array of v1 traits.
// ToProto converts a map of string to string array to an array of v1 traits.
func ToProto(traits trait.Traits) []*traitv1.Trait {
if traits == nil {
return nil
}
out := make([]*traitv1.Trait, 0, len(traits))
sortedKeys := make([]string, 0, len(traits))

Expand Down
5 changes: 5 additions & 0 deletions api/types/trait/convert/v1/trait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ func TestRoundtrip(t *testing.T) {

require.Empty(t, cmp.Diff(traits, converted))
}

func TestNil(t *testing.T) {
var traits trait.Traits
require.Nil(t, FromProto(ToProto(traits)))
}

0 comments on commit 62b651f

Please sign in to comment.