Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions util/reflectutils/jsonfield.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type SStructFieldInfo struct {
kebabFieldName string
ForceString bool
Tags map[string]string

Aliases []string
}

func (s *SStructFieldInfo) updateTags(k, v string) {
Expand All @@ -69,6 +71,9 @@ func (s SStructFieldInfo) deepCopy() *SStructFieldInfo {
tags[k] = v
}
scopy.Tags = tags
aliases := make([]string, len(s.Aliases))
copy(aliases, s.Aliases)
scopy.Aliases = aliases
return &scopy
}

Expand Down Expand Up @@ -125,6 +130,9 @@ func ParseFieldJsonInfo(name string, tag reflect.StructTag) SStructFieldInfo {
if !info.Ignore && len(info.Name) == 0 {
info.Name = info.kebabFieldName
}
if val, ok := info.Tags["alias"]; !info.Ignore && ok {
info.Aliases = strings.Split(val, ",")
}
return info
}

Expand Down Expand Up @@ -336,6 +344,8 @@ func (fields SStructFieldValueSet) GetStructFieldIndexes2(name string, strictMod
ret = append(ret, i)
} else if info.FieldName == capName {
ret = append(ret, i)
} else if len(info.Aliases) > 0 && utils.IsInArray(name, info.Aliases) {
ret = append(ret, i)
}
}
}
Expand Down
48 changes: 48 additions & 0 deletions util/reflectutils/jsonfield_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,3 +455,51 @@ func TestEmbededStructPtr(t *testing.T) {
}
}
}

func TestAliases(t *testing.T) {
type Struct1 struct {
Field1 string `json:"field1" alias:"field1_alias"`
}
type Struct2 struct {
Field2 string `json:"field2" alias:"field2_alias"`
}
type TopStruct struct {
Struct1
Struct2
}

cases := []struct {
val interface{}
name string
index int
}{
{
val: TopStruct{},
name: "field1",
index: 0,
},
{
val: TopStruct{},
name: "field2",
index: 1,
},
{
val: TopStruct{},
name: "field1_alias",
index: 0,
},
{
val: TopStruct{},
name: "field2_alias",
index: 1,
},
}

for _, c := range cases {
set := FetchStructFieldValueSet(reflect.ValueOf(c.val))
got := set.GetStructFieldIndex(c.name)
if got != c.index {
t.Errorf("Got: %v Want: %v", got, c.index)
}
}
}