-
Notifications
You must be signed in to change notification settings - Fork 0
/
extensions.go
304 lines (231 loc) · 6.24 KB
/
extensions.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright (c) 2019-2021, AT&T Intellectual Property.
// All rights reserved.
//
// Copyright (c) 2016-2017 by Brocade Communications Systems, Inc.
// All rights reserved.
//
// SPDX-License-Identifier: MPL-2.0
package compile
import (
"github.com/sdcio/yang-parser/parse"
"github.com/sdcio/yang-parser/schema"
)
/*
* To add extensions to the schema tree a simple pattern is followed.
* The compiler turns a set of parse nodes into a schema node. As each
* node is compiled, it is passed to the extensions which are given an
* opportunity to wrap (decorate) the node with any extensions. If
* the extensions are misused then an error may be returned instead.
*/
type Extensions interface {
// Returns a function used to get the cardinality of any extension
NodeCardinality(parse.NodeType) map[parse.NodeType]parse.Cardinality
// Extend the complete model set, including the combined tree of
// all the underlying models
ExtendModelSet(schema.ModelSet) (schema.ModelSet, error)
// Extend the schema tree used for RPC parameters and schema Models
ExtendModel(parse.Node, schema.Model, schema.Tree) (schema.Model, error)
// Extend the RPC node which includes input and output parameters
// as trees
ExtendRpc(parse.Node, schema.Rpc) (schema.Rpc, error)
// Extend the Notification node
ExtendNotification(parse.Node, schema.Notification) (schema.Notification, error)
// Extend the schema tree used for RPC parameters and schema Models
ExtendTree(parse.Node, schema.Tree) (schema.Tree, error)
// Extend the various nodes within a schema Tree
ExtendContainer(parse.Node, schema.Container) (schema.Container, error)
ExtendList(parse.Node, schema.List) (schema.List, error)
ExtendLeaf(parse.Node, schema.Leaf) (schema.Leaf, error)
ExtendLeafList(parse.Node, schema.LeafList) (schema.LeafList, error)
// Extend choice and case nodes
ExtendChoice(parse.Node, schema.Choice) (schema.Choice, error)
ExtendCase(parse.Node, schema.Case) (schema.Case, error)
// Extend the type, given the parse node and the base type that this
// type is derived from. The base time may be nil, when the type is
// a built in type.
ExtendType(p parse.Node, base schema.Type, t schema.Type) (schema.Type, error)
// Extend must to allow for custom must optimisations
ExtendMust(p parse.Node, m parse.Node) (string, error)
ExtendOpdCommand(parse.Node, schema.OpdCommand) (schema.OpdCommand, error)
ExtendOpdOption(parse.Node, schema.OpdOption) (schema.OpdOption, error)
ExtendOpdArgument(parse.Node, schema.OpdArgument) (schema.OpdArgument, error)
}
func (comp *Compiler) extendModelSet(m schema.ModelSet) (schema.ModelSet, error) {
if comp.extensions == nil {
return m, nil
}
return comp.extensions.ExtendModelSet(m)
}
func (comp *Compiler) extendModel(p parse.Node, m schema.Model, t schema.Tree) schema.Model {
if comp.extensions == nil {
return m
}
m2, e := comp.extensions.ExtendModel(p, m, t)
if e != nil {
comp.error(p, e)
return m
}
return m2
}
func (comp *Compiler) extendRpc(p parse.Node, r schema.Rpc) schema.Rpc {
if comp.extensions == nil {
return r
}
r2, e := comp.extensions.ExtendRpc(p, r)
if e != nil {
comp.error(p, e)
return r
}
return r2
}
func (comp *Compiler) extendNotification(p parse.Node, n schema.Notification) schema.Notification {
if comp.extensions == nil {
return n
}
n2, e := comp.extensions.ExtendNotification(p, n)
if e != nil {
comp.error(p, e)
return n
}
return n2
}
func (comp *Compiler) extendTree(p parse.Node, t schema.Tree) schema.Tree {
if comp.extensions == nil {
return t
}
t2, e := comp.extensions.ExtendTree(p, t)
if e != nil {
comp.error(p, e)
return t
}
return t2
}
func (comp *Compiler) extendContainer(
p parse.Node, c schema.Container,
) schema.Container {
if comp.extensions == nil {
return c
}
c2, e := comp.extensions.ExtendContainer(p, c)
if e != nil {
comp.error(p, e)
return c
}
return c2
}
func (comp *Compiler) extendList(p parse.Node, l schema.List) schema.List {
if comp.extensions == nil {
return l
}
l2, e := comp.extensions.ExtendList(p, l)
if e != nil {
comp.error(p, e)
return l
}
return l2
}
func (comp *Compiler) extendLeaf(p parse.Node, l schema.Leaf) schema.Leaf {
if comp.extensions == nil {
return l
}
l2, e := comp.extensions.ExtendLeaf(p, l)
if e != nil {
comp.error(p, e)
return l
}
return l2
}
func (comp *Compiler) extendLeafList(p parse.Node, l schema.LeafList) schema.LeafList {
if comp.extensions == nil {
return l
}
l2, e := comp.extensions.ExtendLeafList(p, l)
if e != nil {
comp.error(p, e)
return l
}
return l2
}
func (comp *Compiler) extendChoice(p parse.Node, c schema.Choice) schema.Choice {
if comp.extensions == nil {
return c
}
choiceExt, e := comp.extensions.ExtendChoice(p, c)
if e != nil {
comp.error(p, e)
return c
}
return choiceExt
}
func (comp *Compiler) extendCase(p parse.Node, c schema.Case) schema.Case {
if comp.extensions == nil {
return c
}
caseExt, e := comp.extensions.ExtendCase(p, c)
if e != nil {
comp.error(p, e)
return c
}
return caseExt
}
func (comp *Compiler) extendType(
p parse.Node, base schema.Type, t schema.Type,
) schema.Type {
if comp.extensions == nil {
return t
}
t2, e := comp.extensions.ExtendType(p, base, t)
if e != nil {
comp.error(p, e)
return t
}
return t2
}
func (comp *Compiler) extendMust(
p parse.Node, m parse.Node,
) string {
if comp.extensions == nil {
return ""
}
mustExt, e := comp.extensions.ExtendMust(p, m)
if e != nil {
comp.error(p, e)
return ""
}
return mustExt
}
func (comp *Compiler) extendOpdCommand(
p parse.Node, c schema.OpdCommand,
) schema.OpdCommand {
if comp.extensions == nil {
return c
}
c2, e := comp.extensions.ExtendOpdCommand(p, c)
if e != nil {
comp.error(p, e)
return c
}
return c2
}
func (comp *Compiler) extendOpdOption(p parse.Node, o schema.OpdOption) schema.OpdOption {
if comp.extensions == nil {
return o
}
o2, e := comp.extensions.ExtendOpdOption(p, o)
if e != nil {
comp.error(p, e)
return o
}
return o2
}
func (comp *Compiler) extendOpdArgument(p parse.Node, a schema.OpdArgument) schema.OpdArgument {
if comp.extensions == nil {
return a
}
a2, e := comp.extensions.ExtendOpdArgument(p, a)
if e != nil {
comp.error(p, e)
return a
}
return a2
}