(expr *ast.BinaryExpr, attrs map[string]interface{})
| 48 | } |
| 49 | |
| 50 | func parseBinaryExpr(expr *ast.BinaryExpr, attrs map[string]interface{}) (interface{}, error) { |
| 51 | var ( |
| 52 | lRes interface{} |
| 53 | rRes interface{} |
| 54 | opt = expr.Op.String() |
| 55 | err error |
| 56 | ) |
| 57 | |
| 58 | if !expr.Op.IsOperator() { |
| 59 | return nil, errors.New(opt + "is invalid operator") |
| 60 | } |
| 61 | |
| 62 | switch ex := expr.X.(type) { |
| 63 | case *ast.Ident: |
| 64 | lRes = attrs[ex.Name] |
| 65 | case *ast.BasicLit: |
| 66 | lRes, err = parseBasicLit(ex) |
| 67 | if err != nil { |
| 68 | return nil, err |
| 69 | } |
| 70 | case *ast.BinaryExpr: |
| 71 | lRes, err = parseBinaryExpr(ex, attrs) |
| 72 | if err != nil { |
| 73 | return nil, err |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | switch ex := expr.Y.(type) { |
| 78 | case *ast.Ident: |
| 79 | rRes = attrs[ex.Name] |
| 80 | case *ast.BasicLit: |
| 81 | rRes, err = parseBasicLit(ex) |
| 82 | if err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | case *ast.BinaryExpr: |
| 86 | rRes, err = parseBinaryExpr(ex, attrs) |
| 87 | if err != nil { |
| 88 | return nil, err |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if !(lRes != nil && rRes != nil && reflect.TypeOf(lRes) == reflect.TypeOf(rRes)) { |
| 93 | return nil, errors.New("expr type error") |
| 94 | } |
| 95 | |
| 96 | switch opt { |
| 97 | case "==": |
| 98 | return lRes == rRes, nil |
| 99 | case "!=": |
| 100 | return lRes != rRes, nil |
| 101 | default: |
| 102 | return nil, errors.New("operator" + opt + " is not be support") |
| 103 | } |
| 104 | return nil, nil |
| 105 | } |
| 106 | |
| 107 | func parseBasicLit(expr *ast.BasicLit) (interface{}, error) { |
no test coverage detected