...
1
16
17 package rules
18
19 import (
20 "reflect"
21 "strings"
22
23 "k8s.io/gengo/v2/types"
24 )
25
26
27
28 type OmitEmptyMatchCase struct{}
29
30 func (n *OmitEmptyMatchCase) Name() string {
31 return "omitempty_match_case"
32 }
33
34 func (n *OmitEmptyMatchCase) Validate(t *types.Type) ([]string, error) {
35 fields := make([]string, 0)
36
37
38 switch t.Kind {
39 case types.Struct:
40 for _, m := range t.Members {
41 goName := m.Name
42 jsonTag, ok := reflect.StructTag(m.Tags).Lookup("json")
43 if !ok {
44 continue
45 }
46
47 parts := strings.Split(jsonTag, ",")
48 if len(parts) < 2 {
49
50 continue
51 }
52 if parts[0] == "-" {
53
54 continue
55 }
56 for _, part := range parts[1:] {
57 if strings.EqualFold(part, "omitempty") && part != "omitempty" {
58 fields = append(fields, goName)
59 }
60 }
61 }
62 }
63 return fields, nil
64 }
65
View as plain text