-
Notifications
You must be signed in to change notification settings - Fork 9
/
attrexp.go
148 lines (133 loc) · 3.52 KB
/
attrexp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package filter
import (
"encoding/json"
"fmt"
"github.com/di-wu/parser"
"github.com/di-wu/parser/ast"
"github.com/scim2/filter-parser/v2/internal/grammar"
"github.com/scim2/filter-parser/v2/internal/types"
"strconv"
"strings"
)
// ParseAttrExp parses the given raw data as an AttributeExpression.
func ParseAttrExp(raw []byte) (AttributeExpression, error) {
return parseAttrExp(raw, config{})
}
// ParseAttrExpNumber parses the given raw data as an AttributeExpression with json.Number.
func ParseAttrExpNumber(raw []byte) (AttributeExpression, error) {
return parseAttrExp(raw, config{useNumber: true})
}
func parseAttrExp(raw []byte, c config) (AttributeExpression, error) {
p, err := ast.New(raw)
if err != nil {
return AttributeExpression{}, err
}
node, err := grammar.AttrExp(p)
if err != nil {
return AttributeExpression{}, err
}
if _, err := p.Expect(parser.EOD); err != nil {
return AttributeExpression{}, err
}
return c.parseAttrExp(node)
}
func (p config) parseAttrExp(node *ast.Node) (AttributeExpression, error) {
if node.Type != typ.AttrExp {
return AttributeExpression{}, invalidTypeError(typ.AttrExp, node.Type)
}
children := node.Children()
if len(children) == 0 {
return AttributeExpression{}, invalidLengthError(typ.AttrExp, 1, 0)
}
// AttrPath 'pr'
attrPath, err := parseAttrPath(children[0])
if err != nil {
return AttributeExpression{}, err
}
if len(children) == 1 {
return AttributeExpression{
AttributePath: attrPath,
Operator: PR,
}, nil
}
if l := len(children); l != 3 {
return AttributeExpression{}, invalidLengthError(typ.AttrExp, 3, l)
}
var (
compareOp = CompareOperator(strings.ToLower(children[1].Value))
compareValue interface{}
)
switch node := children[2]; node.Type {
case typ.False:
compareValue = false
case typ.Null:
compareValue = nil
case typ.True:
compareValue = true
case typ.Number:
value, err := p.parseNumber(node)
if err != nil {
return AttributeExpression{}, err
}
compareValue = value
case typ.String:
str := node.Value
str = strings.TrimPrefix(str, "\"")
str = strings.TrimSuffix(str, "\"")
compareValue = str
default:
return AttributeExpression{}, invalidChildTypeError(typ.AttrExp, node.Type)
}
return AttributeExpression{
AttributePath: attrPath,
Operator: compareOp,
CompareValue: compareValue,
}, nil
}
func (p config) parseNumber(node *ast.Node) (interface{}, error) {
var frac, exp bool
var nStr string
for _, node := range node.Children() {
switch t := node.Type; t {
case typ.Minus:
nStr = "-"
case typ.Int:
nStr += node.Value
case typ.Frac:
frac = true
children := node.Children()
if l := len(children); l != 1 {
return AttributeExpression{}, invalidLengthError(typ.Frac, 1, l)
}
nStr += fmt.Sprintf(".%s", children[0].Value)
case typ.Exp:
exp = true
nStr += "e"
for _, node := range node.Children() {
switch t := node.Type; t {
case typ.Sign, typ.Digits:
nStr += node.Value
default:
return AttributeExpression{}, invalidChildTypeError(typ.Number, node.Type)
}
}
default:
return AttributeExpression{}, invalidChildTypeError(typ.Number, node.Type)
}
}
if p.useNumber {
return json.Number(nStr), nil
}
f, err := strconv.ParseFloat(nStr, 64)
if err != nil {
return AttributeExpression{}, &internalError{
Message: err.Error(),
}
}
// Integers can not contain fractional or exponent parts.
// More info: https://tools.ietf.org/html/rfc7643#section-2.3.4
if !frac && !exp {
return int(f), nil
}
return f, err
}