-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubtree.go
149 lines (132 loc) · 3.33 KB
/
subtree.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
149
package netconf
import (
"context"
"strings"
"github.com/freeconf/yang/node"
"github.com/freeconf/yang/patch/xml"
"github.com/freeconf/yang/val"
)
type subtreeContextKeyType struct{}
var subtreeContextKey = subtreeContextKeyType{}
type subtreeFilter struct {
containment map[xml.Name]*subtreeFilter
selection []xml.Name
matching []contentMatching
}
var subtreeSelectNone = &subtreeFilter{}
func (f *subtreeFilter) selectAll() bool {
return len(f.containment) == 0 && len(f.selection) == 0 && len(f.matching) == 0
}
func (f *subtreeFilter) selected(ident string) (*subtreeFilter, bool) {
if f == subtreeSelectNone {
return f, false
}
if f.selectAll() {
return f, true
}
if c, found := f.containment[xml.Name{Local: ident}]; found {
return c, true
}
for _, s := range f.selection {
if ident == s.Local {
return &subtreeFilter{}, true
}
}
for _, m := range f.matching {
if ident == m.field.Local {
return f, true
}
}
return subtreeSelectNone, false
}
type contentMatching struct {
field xml.Name
value string
}
func compileSubtree(x *Msg, f *subtreeFilter) error {
for _, e := range x.Elems {
if err := compileSubtreeComponents(e, f); err != nil {
return err
}
}
return nil
}
func compileSubtreeComponents(x *Msg, f *subtreeFilter) error {
if len(x.Elems) > 0 || len(x.Attrs) > 0 {
child := &subtreeFilter{}
if f.containment == nil {
f.containment = make(map[xml.Name]*subtreeFilter)
}
f.containment[x.XMLName] = child
if len(x.Attrs) > 0 {
for _, a := range x.Attrs {
child.matching = append(child.matching, contentMatching{
field: a.Name,
value: a.Value,
})
}
} else {
for _, e := range x.Elems {
if err := compileSubtreeComponents(e, child); err != nil {
return err
}
}
}
return nil
}
s := strings.TrimSpace(x.Content)
if s != "" {
f.matching = append(f.matching, contentMatching{
field: x.XMLName,
value: s,
})
return nil
}
f.selection = append(f.selection, x.XMLName)
return nil
}
func (root *subtreeFilter) CheckContainerPreConstraints(r *node.ChildRequest) (bool, error) {
f := root.currentFilter(r.Selection)
_, selected := f.selected(r.Meta.Ident())
return selected, nil
}
func (root *subtreeFilter) CheckFieldPreConstraints(r *node.FieldRequest, hnd *node.ValueHandle) (bool, error) {
f := root.currentFilter(r.Selection)
_, selected := f.selected(r.Meta.Ident())
return selected, nil
}
func (root *subtreeFilter) CheckListPostConstraints(r node.ListRequest, child *node.Selection, key []val.Value) (bool, bool, error) {
f := root.currentFilter(r.Selection)
if f.selectAll() {
return true, true, nil
}
for _, m := range f.matching {
fs, err := child.Find(m.field.Local)
if err != nil {
return true, false, err
}
v, err := fs.Get()
if err != nil {
return true, false, err
}
if v.String() != m.value {
return true, false, nil
}
}
return true, true, nil
}
func (root *subtreeFilter) currentFilter(s *node.Selection) *subtreeFilter {
if found := s.Context.Value(subtreeContextKey); found != nil {
return found.(*subtreeFilter)
}
return root
}
func (root *subtreeFilter) ContextConstraint(s *node.Selection) context.Context {
if s.InsideList {
return s.Context
}
f := root.currentFilter(s)
ctx := s.Context
next, _ := f.selected(s.Meta().Ident())
return context.WithValue(ctx, subtreeContextKey, next)
}