Skip to content

Commit 599b03a

Browse files
committed
feat(gogenerate): add optional fields in log with omitempty annotation
Signed-off-by: T M Rezoan Tamal <[email protected]>
1 parent 72886c6 commit 599b03a

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

pkg/events/events.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ type HTTPRequest struct {
8484
Endpoint string `log:"http.url_details.endpoint"`
8585

8686
// Route is the URL path without interpolating the path variables.
87-
Route string `log:"http.route"`
87+
Route string `log:"http.route,omitempty"`
8888
}
8989

9090
// FillFieldsFromRequest fills in the standard request fields

tools/logger/logger.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ if s.{{.name}} != nil {
5454
}`
5555
)
5656

57+
const (
58+
tagOmitEmpty = ",omitempty"
59+
)
60+
5761
func main() {
5862
flag.Usage = usage
5963

@@ -140,6 +144,9 @@ func processStruct(w io.Writer, s *types.Struct, name string) {
140144
write(w, nestedNilableMarshalerFormat, args)
141145
case field == ".":
142146
write(w, nestedMarshalerFormat, args)
147+
case strings.HasSuffix(field, tagOmitEmpty):
148+
args["key"] = strings.TrimSuffix(field, tagOmitEmpty)
149+
write(w, getSimpleOptionalFieldFormat(s.Field(kk).Type()), args)
143150
default:
144151
write(w, simpleFieldFormat, args)
145152
}
@@ -168,3 +175,25 @@ func usage() {
168175
fmt.Fprintf(os.Stderr, "Flags:\n")
169176
flag.PrintDefaults()
170177
}
178+
179+
func getSimpleOptionalFieldFormat(p types.Type) string {
180+
var defaultValue string
181+
switch p.Underlying().String() {
182+
case "string":
183+
defaultValue = `""`
184+
case "int", "int8", "int16", "int32", "int64",
185+
"uint", "uint8", "uint16", "uint32", "uint64":
186+
defaultValue = "0"
187+
case "float32", "float64":
188+
defaultValue = "0.0"
189+
case "bool":
190+
defaultValue = "false"
191+
default:
192+
defaultValue = "nil"
193+
}
194+
195+
return fmt.Sprintf(`
196+
if s.{{.name}} != %s {
197+
addField("{{.key}}", s.{{.name}})
198+
}`, defaultValue)
199+
}

tools/logger/logger_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package main
2+
3+
import (
4+
"go/types"
5+
"testing"
6+
)
7+
8+
func TestGetSimpleOptionalFieldFormat(t *testing.T) {
9+
newNamedType := func(name string, underlying types.Type) types.Type {
10+
return types.NewNamed(
11+
types.NewTypeName(0, nil, name, nil),
12+
underlying,
13+
nil,
14+
)
15+
}
16+
17+
tests := []struct {
18+
name string
19+
typ types.Type
20+
expected string
21+
}{
22+
{
23+
name: "string type",
24+
typ: types.Typ[types.String],
25+
expected: "\nif s.{{.name}} != \"\" {\n\taddField(\"{{.key}}\", s.{{.name}})\n}",
26+
},
27+
{
28+
name: "int type",
29+
typ: types.Typ[types.Int],
30+
expected: "\nif s.{{.name}} != 0 {\n\taddField(\"{{.key}}\", s.{{.name}})\n}",
31+
},
32+
{
33+
name: "float64 type",
34+
typ: types.Typ[types.Float64],
35+
expected: "\nif s.{{.name}} != 0.0 {\n\taddField(\"{{.key}}\", s.{{.name}})\n}",
36+
},
37+
{
38+
name: "bool type",
39+
typ: types.Typ[types.Bool],
40+
expected: "\nif s.{{.name}} != false {\n\taddField(\"{{.key}}\", s.{{.name}})\n}",
41+
},
42+
{
43+
name: "custom string type (type Token string)",
44+
typ: newNamedType("Token", types.Typ[types.String]),
45+
expected: "\nif s.{{.name}} != \"\" {\n\taddField(\"{{.key}}\", s.{{.name}})\n}",
46+
},
47+
{
48+
name: "custom int type (type Digits int)",
49+
typ: newNamedType("Digits", types.Typ[types.Int]),
50+
expected: "\nif s.{{.name}} != 0 {\n\taddField(\"{{.key}}\", s.{{.name}})\n}",
51+
},
52+
{
53+
name: "custom float type (type Decimal float64)",
54+
typ: newNamedType("Decimal", types.Typ[types.Float64]),
55+
expected: "\nif s.{{.name}} != 0.0 {\n\taddField(\"{{.key}}\", s.{{.name}})\n}",
56+
},
57+
{
58+
name: "custom bool type (type Truther bool)",
59+
typ: newNamedType("Truther", types.Typ[types.Bool]),
60+
expected: "\nif s.{{.name}} != false {\n\taddField(\"{{.key}}\", s.{{.name}})\n}",
61+
},
62+
{
63+
name: "pointer type",
64+
typ: types.NewPointer(types.Typ[types.Int]),
65+
expected: "\nif s.{{.name}} != nil {\n\taddField(\"{{.key}}\", s.{{.name}})\n}",
66+
},
67+
{
68+
name: "pointer to custom type",
69+
typ: types.NewPointer(newNamedType("Nullable", types.Typ[types.String])),
70+
expected: "\nif s.{{.name}} != nil {\n\taddField(\"{{.key}}\", s.{{.name}})\n}",
71+
},
72+
}
73+
74+
for _, tt := range tests {
75+
t.Run(tt.name, func(t *testing.T) {
76+
got := getSimpleOptionalFieldFormat(tt.typ)
77+
if got != tt.expected {
78+
t.Errorf("getSimpleOptionalFieldFormat() =\n%v\nwant:\n%v", got, tt.expected)
79+
}
80+
})
81+
}
82+
}

0 commit comments

Comments
 (0)