Skip to content

Commit d51248d

Browse files
committed
some tests
1 parent c9f8d75 commit d51248d

File tree

2 files changed

+103
-3
lines changed

2 files changed

+103
-3
lines changed

pkg/core/encryption/encryption.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,19 @@ func hasOmitzero(field reflect.StructField) bool {
211211
return slices.Contains(tagParts, "omitzero")
212212
}
213213

214-
// Returns the field_name from `json:"field_name"`.
214+
// Returns the "field_name" from:
215+
//
216+
// FieldName `json:"field_name"`
217+
//
218+
// Returns "FieldName" if not present.
215219
func getJsonFieldName(field reflect.StructField) string {
216220
tag := field.Tag.Get("json")
217-
tagParts := strings.Split(tag, ",")
218-
return tagParts[0] // always len > 0 -> safe
221+
tagParts := strings.Split(tag, ",") // always len > 0
222+
if tagParts[0] == "-" {
223+
return ""
224+
}
225+
if len(tagParts[0]) != 0 {
226+
return tagParts[0]
227+
}
228+
return field.Name // fallback to struct field name instead of json name
219229
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package encryption
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_hasOmitzero(t *testing.T) {
9+
type testStruct struct {
10+
WithOmitzero string `json:"field,omitzero"`
11+
WithoutOmitzero string `json:"field2"`
12+
}
13+
typ := reflect.TypeOf(testStruct{})
14+
15+
tests := []struct {
16+
name string
17+
field reflect.StructField
18+
want bool
19+
}{
20+
{
21+
name: "has omitzero",
22+
field: typ.Field(0),
23+
want: true,
24+
},
25+
{
26+
name: "does not have omitzero",
27+
field: typ.Field(1),
28+
want: false,
29+
},
30+
}
31+
32+
for _, tt := range tests {
33+
t.Run(tt.name, func(t *testing.T) {
34+
if got := hasOmitzero(tt.field); got != tt.want {
35+
t.Errorf("hasOmitzero() = %v, want %v", got, tt.want)
36+
}
37+
})
38+
}
39+
}
40+
41+
func Test_getJsonFieldName(t *testing.T) {
42+
type testStruct struct {
43+
Normal string `json:"field"`
44+
WithoutOmitzero string `json:"field2,omitzero"`
45+
NoTag string
46+
Ignore string `json:"-"`
47+
EmptyName string `json:",omitzero"`
48+
}
49+
typ := reflect.TypeOf(testStruct{})
50+
51+
tests := []struct {
52+
name string
53+
field reflect.StructField
54+
want string
55+
}{
56+
{
57+
name: "simple name",
58+
field: typ.Field(0),
59+
want: "field",
60+
},
61+
{
62+
name: "name with option",
63+
field: typ.Field(1),
64+
want: "field2",
65+
},
66+
{
67+
name: "no tag uses field name",
68+
field: typ.Field(2),
69+
want: "NoTag",
70+
},
71+
{
72+
name: "ignored field",
73+
field: typ.Field(3),
74+
want: "",
75+
},
76+
{
77+
name: "empty name in tag falls back to field name",
78+
field: typ.Field(4),
79+
want: "EmptyName",
80+
},
81+
}
82+
83+
for _, tt := range tests {
84+
t.Run(tt.name, func(t *testing.T) {
85+
if got := getJsonFieldName(tt.field); got != tt.want {
86+
t.Errorf("getJsonFieldName() = %v, want %v", got, tt.want)
87+
}
88+
})
89+
}
90+
}

0 commit comments

Comments
 (0)