diff --git a/api/types/events/events.go b/api/types/events/events.go index a0e138c87f678..527f60fc44e3a 100644 --- a/api/types/events/events.go +++ b/api/types/events/events.go @@ -184,9 +184,16 @@ func (m *Struct) nonEmptyStrs() int { } func (m *Struct) trimToMaxSize(maxSize int) *Struct { - var out Struct + if len(m.Fields) == 0 { + return m + } + + out := Struct{ + Struct: types.Struct{ + Fields: make(map[string]*types.Value), + }, + } for k, v := range m.Fields { - delete(out.Fields, k) trimmedKey := trimStr(k, maxSize) if v != nil { diff --git a/api/types/events/events_test.go b/api/types/events/events_test.go index b76550fc60c31..74abf79895739 100644 --- a/api/types/events/events_test.go +++ b/api/types/events/events_test.go @@ -20,6 +20,7 @@ import ( "strings" "testing" + "github.com/gogo/protobuf/types" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/stretchr/testify/require" @@ -156,3 +157,46 @@ func TestTrimStr(t *testing.T) { require.Equal(t, test.want, trimStr(test.have, maxLen)) } } + +func TestStructTrimToMaxSize(t *testing.T) { + testCases := []struct { + name string + maxSize int + in *Struct + want *Struct + }{ + { + name: "Field key exceeds max limit size", + maxSize: 10, + in: &Struct{ + Struct: types.Struct{ + Fields: map[string]*types.Value{ + strings.Repeat("A", 100): { + Kind: &types.Value_StringValue{ + StringValue: "A", + }, + }, + }, + }, + }, + want: &Struct{ + Struct: types.Struct{ + Fields: map[string]*types.Value{ + strings.Repeat("A", 8): { + Kind: &types.Value_StringValue{ + StringValue: "A", + }, + }, + }, + }, + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + got := tc.in.trimToMaxSize(tc.maxSize) + require.Equal(t, tc.want, got) + }) + } +}