Skip to content

Commit 593cfe8

Browse files
kirecekcapnspacehook
authored andcommitted
fix: make sure Fields map is not nil
Signed-off-by: Erik Jankovic <erik.jankovic@gmail.com>
1 parent 31fe604 commit 593cfe8

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

api/types/events/events.go

+5
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ func (m *Struct) trimToMaxSize(maxSize int) *Struct {
192192
if v != nil {
193193
if strVal := v.GetStringValue(); strVal != "" {
194194
trimmedVal := trimStr(strVal, maxSize)
195+
196+
if out.Fields == nil {
197+
out.Fields = make(map[string]*types.Value)
198+
}
199+
195200
out.Fields[trimmedKey] = &types.Value{
196201
Kind: &types.Value_StringValue{
197202
StringValue: trimmedVal,

api/types/events/struct_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Copyright 2025 Gravitational, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package events
18+
19+
import (
20+
"reflect"
21+
"strings"
22+
"testing"
23+
24+
"github.com/gogo/protobuf/types"
25+
"github.com/stretchr/testify/require"
26+
)
27+
28+
func TestStructTrimToMaxSize(t *testing.T) {
29+
testCases := []struct {
30+
name string
31+
maxSize int
32+
in *Struct
33+
want *Struct
34+
}{
35+
{
36+
name: "Field key exceeds max limit size",
37+
maxSize: 10,
38+
in: &Struct{
39+
Struct: types.Struct{
40+
Fields: map[string]*types.Value{
41+
strings.Repeat("A", 100): {
42+
Kind: &types.Value_StringValue{
43+
StringValue: "A",
44+
},
45+
},
46+
},
47+
},
48+
},
49+
want: &Struct{
50+
Struct: types.Struct{
51+
Fields: map[string]*types.Value{
52+
strings.Repeat("A", 8): {
53+
Kind: &types.Value_StringValue{
54+
StringValue: "A",
55+
},
56+
},
57+
},
58+
},
59+
},
60+
},
61+
}
62+
63+
for _, tc := range testCases {
64+
t.Run(tc.name, func(t *testing.T) {
65+
got := tc.in.trimToMaxSize(tc.maxSize)
66+
require.True(t, reflect.DeepEqual(got, tc.want))
67+
})
68+
}
69+
}

0 commit comments

Comments
 (0)