-
Notifications
You must be signed in to change notification settings - Fork 9
/
attrpath.go
61 lines (54 loc) · 1.44 KB
/
attrpath.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
package filter
import (
"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"
"strings"
)
// ParseAttrPath parses the given raw data as an AttributePath.
func ParseAttrPath(raw []byte) (AttributePath, error) {
p, err := ast.New(raw)
if err != nil {
return AttributePath{}, err
}
node, err := grammar.AttrPath(p)
if err != nil {
return AttributePath{}, err
}
if _, err := p.Expect(parser.EOD); err != nil {
return AttributePath{}, err
}
return parseAttrPath(node)
}
func parseAttrPath(node *ast.Node) (AttributePath, error) {
if node.Type != typ.AttrPath {
return AttributePath{}, invalidTypeError(typ.AttrPath, node.Type)
}
// Indicates whether we encountered an attribute name.
// These are the minimum requirements of an attribute path.
var valid bool
var attrPath AttributePath
for _, node := range node.Children() {
switch t := node.Type; t {
case typ.URI:
uri := node.Value
uri = strings.TrimSuffix(uri, ":")
attrPath.URIPrefix = &uri
case typ.AttrName:
name := node.Value
if attrPath.AttributeName == "" {
attrPath.AttributeName = name
valid = true
} else {
attrPath.SubAttribute = &name
}
default:
return AttributePath{}, invalidChildTypeError(typ.AttrPath, t)
}
}
if !valid {
return AttributePath{}, missingValueError(typ.AttrPath, typ.AttrName)
}
return attrPath, nil
}