Skip to content

Commit

Permalink
fix(tagexpr): Fix bug in nested structure fields
Browse files Browse the repository at this point in the history
Change-Id: I46024112d5c3f9e2aed7c92d1cf119c24f79c694
  • Loading branch information
andeya committed Jul 23, 2019
1 parent c30d728 commit 544b7f5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
20 changes: 15 additions & 5 deletions tagexpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,26 +310,36 @@ func (s *structVM) copySubFields(field *fieldVM, sub *structVM) {
nameSpace := field.structField.Name
offset := field.offset
ptrDeep := field.ptrDeep
omitExpr := field.tagOp == tagOmit
tagOp := field.tagOp
omitExpr := tagOp == tagOmit
for _, k := range sub.fieldSelectorList {
v := sub.fields[k]
valueGetter := v.valueGetter
reflectValueGetter := v.reflectValueGetter
f := &fieldVM{
structField: v.structField,
offset: offset + v.offset,
origin: v.origin,
exprs: make(map[string]*Expr, len(v.exprs)),
structField: v.structField,
offset: offset,
exprs: make(map[string]*Expr, len(v.exprs)),
ptrDeep: v.ptrDeep,
elemType: v.elemType,
elemKind: v.elemKind,
origin: v.origin,
mapKeyStructVM: v.mapKeyStructVM,
mapOrSliceElemStructVM: v.mapOrSliceElemStructVM,
mapOrSliceIfaceKinds: v.mapOrSliceIfaceKinds,
}

if !omitExpr {
f.tagOp = v.tagOp
var selector string
for k, v := range v.exprs {
selector = nameSpace + FieldSeparator + k
f.exprs[selector] = v
s.exprs[selector] = v
s.exprSelectorList = append(s.exprSelectorList, selector)
}
} else {
f.tagOp = tagOp
}

if valueGetter != nil {
Expand Down
4 changes: 2 additions & 2 deletions tagexpr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,20 +717,20 @@ func TestNilField(t *testing.T) {
type (
N struct {
X string `tagexpr:"len($)>0"`
N *N `tagexpr:"?"`
S []*N `tagexpr:"?"`
M map[string]*N `tagexpr:"?"`
I interface{} `tagexpr:"-"`
MI map[string]interface{} `tagexpr:"?"`
SI []interface{}
*N `tagexpr:"?"`
N2 *N `tagexpr:"?"`
}
M struct {
X string `tagexpr:"len($)>0"`
}
)
n := &N{
X: "n",
N: nil,
S: []*N{nil},
M: map[string]*N{"": nil},
I: new(N),
Expand Down
13 changes: 8 additions & 5 deletions validator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ import (

func Example() {
type InfoRequest struct {
Name string `vd:"($!='Alice'||(Age)$==18) && regexp('\\w')"`
Age int `vd:"$>0"`
Email string `vd:"email($)"`
Phone1 string `vd:"phone($)"`
Phone2 string `vd:"phone($,'CN')"`
Name string `vd:"($!='Alice'||(Age)$==18) && regexp('\\w')"`
Age int `vd:"$>0"`
Email string `vd:"email($)"`
Phone1 string `vd:"phone($)"`
Phone2 string `vd:"phone($,'CN')"`
*InfoRequest `vd:"?"`
Info1 *InfoRequest `vd:"?"`
Info2 *InfoRequest `vd:"-"`
}
info := InfoRequest{
Name: "Alice",
Expand Down
13 changes: 8 additions & 5 deletions validator/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import (

func Example() {
type InfoRequest struct {
Name string `vd:"($!='Alice'||(Age)$==18) && regexp('\\w')"`
Age int `vd:"$>0"`
Email string `vd:"email($)"`
Phone1 string `vd:"phone($)"`
Phone2 string `vd:"phone($,'CN')"`
Name string `vd:"($!='Alice'||(Age)$==18) && regexp('\\w')"`
Age int `vd:"$>0"`
Email string `vd:"email($)"`
Phone1 string `vd:"phone($)"`
Phone2 string `vd:"phone($,'CN')"`
*InfoRequest `vd:"?"`
Info1 *InfoRequest `vd:"?"`
Info2 *InfoRequest `vd:"-"`
}
info := InfoRequest{
Name: "Alice",
Expand Down

0 comments on commit 544b7f5

Please sign in to comment.