Skip to content

Commit

Permalink
fix: cache syntax error
Browse files Browse the repository at this point in the history
Change-Id: I6b918dc1f08c762d8db046900e9520147dd56842
  • Loading branch information
andeya committed Dec 8, 2021
1 parent 3c3e845 commit 7d572be
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
5 changes: 2 additions & 3 deletions expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 11 additions & 1 deletion tagexpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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()
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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
}
}
Expand Down
6 changes: 3 additions & 3 deletions tagparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))\"")
}

0 comments on commit 7d572be

Please sign in to comment.