diff --git a/expr.go b/expr.go index 22982ba..cb4e050 100644 --- a/expr.go +++ b/expr.go @@ -33,7 +33,7 @@ func parseExpr(expr string) (*Expr, error) { s := expr _, err := p.parseExprNode(&s, e) if err != nil { - return nil, fmt.Errorf("%q (syntax error): %s", expr, err.Error()) + return nil, err } sortPriority(e.RightOperand()) err = p.checkSyntax() @@ -146,9 +146,8 @@ func (p *Expr) parseExprNode(expr *string, e ExprNode) (ExprNode, error) { } } if operand == nil { - return nil, fmt.Errorf("parsing pos: %q", *expr) + return nil, fmt.Errorf("syntax error: %q", *expr) } - trimLeftSpace(expr) operator := p.parseOperator(expr) if operator == nil { diff --git a/tagexpr.go b/tagexpr.go index b1ab939..66c9639 100644 --- a/tagexpr.go +++ b/tagexpr.go @@ -52,6 +52,7 @@ type structVM struct { exprs map[string]*Expr exprSelectorList []string ifaceTagExprGetters []func(unsafe.Pointer, string, func(*TagExpr, error) error) error + err error } // fieldVM tag expression set of struct field @@ -141,6 +142,9 @@ func (vm *VM) Run(structPtrOrReflectValue interface{}) (*TagExpr, error) { } vm.rw.Unlock() } + if s.err != nil { + return nil, s.err + } return s.newTagExpr(ptr, ""), nil } @@ -258,6 +262,9 @@ func (vm *VM) subRun(path string, t reflect.Type, tid uintptr, ptr unsafe.Pointe } vm.rw.Unlock() } + if s.err != nil { + return nil, s.err + } return s.newTagExpr(ptr, path), nil } @@ -269,7 +276,7 @@ func (vm *VM) registerStructLocked(structType reflect.Type) (*structVM, error) { tid := ameda.RuntimeTypeID(structType) s, had := vm.structJar[tid] if had { - return s, nil + return s, s.err } s = vm.newStructVM() s.name = structType.String() @@ -281,6 +288,7 @@ func (vm *VM) registerStructLocked(structType reflect.Type) (*structVM, error) { structField = structType.Field(i) field, err := s.newFieldVM(structField) if err != nil { + s.err = err return nil, err } switch field.elemKind { @@ -290,6 +298,7 @@ func (vm *VM) registerStructLocked(structType reflect.Type) (*structVM, error) { case reflect.Struct: sub, err = vm.registerStructLocked(field.structField.Type) if err != nil { + s.err = err return nil, err } s.mergeSubStructVM(field, sub) @@ -307,6 +316,7 @@ func (vm *VM) registerStructLocked(structType reflect.Type) (*structVM, error) { case reflect.Array, reflect.Slice, reflect.Map: err = vm.registerIndirectStructLocked(field) if err != nil { + s.err = err return nil, err } } diff --git a/tagparser.go b/tagparser.go index 66d098b..864d85a 100644 --- a/tagparser.go +++ b/tagparser.go @@ -60,10 +60,10 @@ func parseTag(tag string) (map[string]string, error) { } key, val := splitExpr(one) if val == "" { - return nil, fmt.Errorf("%q (syntax error): expression string can not be empty", tag) + return nil, fmt.Errorf("syntax error: %q expression string can not be empty", tag) } if _, ok := kvs[key]; ok { - return nil, fmt.Errorf("%q (syntax error): duplicate expression name %q", tag, key) + return nil, fmt.Errorf("syntax error: %q duplicate expression name %q", tag, key) } kvs[key] = val } @@ -121,7 +121,7 @@ func readOneExpr(tag *string) (string, error) { patch++ } } - return "", fmt.Errorf("%q (syntax error): unclosed single quote \"'\"", s) + return "", fmt.Errorf("syntax error: %q unclosed single quote \"'\"", s) } func trimLeftSpace(p *string) *string { diff --git a/validator/validator_test.go b/validator/validator_test.go index d186835..c941537 100644 --- a/validator/validator_test.go +++ b/validator/validator_test.go @@ -290,3 +290,12 @@ func TestIssue30(t *testing.T) { assert.EqualError(t, vd.Validate(&TStruct{TOk: "1"}), "invalid parameter: TOk") // assert.NoError(t, vd.Validate(&TStruct{TOk: "1", TFail: "1"})) } + +func TestIssue31(t *testing.T) { + type TStruct struct { + A []int32 `vd:"$ == nil || ($ != nil && range($, in(#v, 1, 2, 3))"` + } + assert.EqualError(t, vd.Validate(&TStruct{A: []int32{1}}), "syntax error: \"($ != nil && range($, in(#v, 1, 2, 3))\"") + assert.EqualError(t, vd.Validate(&TStruct{A: []int32{1}}), "syntax error: \"($ != nil && range($, in(#v, 1, 2, 3))\"") + assert.EqualError(t, vd.Validate(&TStruct{A: []int32{1}}), "syntax error: \"($ != nil && range($, in(#v, 1, 2, 3))\"") +}