-
Notifications
You must be signed in to change notification settings - Fork 20
/
context.go
272 lines (245 loc) · 7.2 KB
/
context.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
package tosca
import (
"fmt"
"strconv"
"strings"
"github.com/tliron/kutil/ard"
"github.com/tliron/kutil/problems"
"github.com/tliron/kutil/terminal"
urlpkg "github.com/tliron/kutil/url"
"github.com/tliron/yamlkeys"
)
//
// Contextual
//
type Contextual interface {
GetContext() *Context
}
// From Contextual interface
func GetContext(entityPtr EntityPtr) *Context {
if contextual, ok := entityPtr.(Contextual); ok {
return contextual.GetContext()
} else {
panic(fmt.Sprintf("entity does not implement \"Contextual\" interface: %T", entityPtr))
}
}
func GetCanonicalName(entityPtr EntityPtr) string {
if metadata, ok := GetMetadata(entityPtr); ok {
if canonicalName, ok := metadata["canonical_name"]; ok {
return canonicalName
}
}
context := GetContext(entityPtr)
canonicalNamespace := context.GetCanonicalNamespace()
if canonicalNamespace != nil {
return fmt.Sprintf("%s::%s", *canonicalNamespace, context.Name)
} else {
return context.Name
}
}
//
// Context
//
type Context struct {
Parent *Context
Name string
Path ard.Path
URL urlpkg.URL
Data ard.Value
Locator ard.Locator
CanonicalNamespace *string
Namespace *Namespace
ScriptletNamespace *ScriptletNamespace
Hierarchy *Hierarchy
Problems *problems.Problems
Quirks Quirks
Grammar *Grammar
ReadTagOverrides map[string]string
}
func NewContext(stylist *terminal.Stylist, quirks Quirks) *Context {
if stylist == nil {
stylist = terminal.NewStylist(false)
}
return &Context{
Namespace: NewNamespace(),
ScriptletNamespace: NewScriptletNamespace(),
Hierarchy: NewHierarchy(),
Problems: problems.NewProblems(stylist),
Quirks: quirks,
}
}
func (self *Context) NewImportContext(url urlpkg.URL) *Context {
return &Context{
Name: self.Name,
Path: self.Path,
URL: url,
CanonicalNamespace: self.CanonicalNamespace,
Namespace: NewNamespace(),
ScriptletNamespace: NewScriptletNamespace(),
Hierarchy: NewHierarchy(),
Problems: self.Problems.NewProblems(),
Quirks: self.Quirks,
Grammar: self.Grammar,
}
}
func (self *Context) GetAncestor(generation int) *Context {
if generation == 0 {
return self
} else if generation == 1 {
return self.Parent
} else if self.Parent != nil {
return self.Parent.GetAncestor(generation - 1)
} else {
return nil
}
}
func (self *Context) GetCanonicalNamespace() *string {
if self.CanonicalNamespace != nil {
return self.CanonicalNamespace
} else if self.Parent != nil {
return self.Parent.GetCanonicalNamespace()
}
return nil
}
func (self *Context) HasQuirk(quirk Quirk) bool {
return self.Quirks.Has(quirk)
}
func (self *Context) SetReadTag(fieldName string, tag string) {
if self.ReadTagOverrides == nil {
self.ReadTagOverrides = make(map[string]string)
}
self.ReadTagOverrides[fieldName] = tag
}
func (self *Context) GetLocation() (int, int) {
if self.Locator != nil {
if row, column, ok := self.Locator.Locate(self.Path...); ok {
return row, column
}
}
return -1, -1
}
func (self *Context) Is(typeNames ...ard.TypeName) bool {
for _, typeName := range typeNames {
if typeValidator, ok := ard.TypeValidators[typeName]; ok {
if typeValidator(self.Data) {
return true
}
} else {
panic(fmt.Sprintf("unsupported field type: %s", typeName))
}
}
return false
}
//
// Child contexts
//
func (self *Context) Clone(data ard.Value) *Context {
return &Context{
Parent: self.Parent,
Name: self.Name,
Path: self.Path,
URL: self.URL,
Data: data,
Locator: self.Locator,
CanonicalNamespace: self.CanonicalNamespace,
Namespace: self.Namespace,
ScriptletNamespace: self.ScriptletNamespace,
Hierarchy: self.Hierarchy,
Problems: self.Problems,
Quirks: self.Quirks,
Grammar: self.Grammar,
}
}
func (self *Context) FieldChild(name ard.Value, data ard.Value) *Context {
nameString := yamlkeys.KeyString(name) // complex keys would be stringified
return &Context{
Parent: self,
Name: nameString,
Path: self.Path.AppendField(nameString),
URL: self.URL,
Data: data,
Locator: self.Locator,
CanonicalNamespace: self.CanonicalNamespace,
Namespace: self.Namespace,
ScriptletNamespace: self.ScriptletNamespace,
Hierarchy: self.Hierarchy,
Problems: self.Problems,
Quirks: self.Quirks,
Grammar: self.Grammar,
}
}
func (self *Context) GetFieldChild(name string) (*Context, bool) {
if self.ValidateType(ard.TypeMap) {
if data, ok := self.Data.(ard.Map)[name]; ok {
return self.FieldChild(name, data), true
}
}
return nil, false
}
func (self *Context) GetRequiredFieldChild(name string) (*Context, bool) {
if context, ok := self.GetFieldChild(name); ok {
return context, true
} else {
self.FieldChild(name, nil).ReportFieldMissing()
return nil, false
}
}
func (self *Context) FieldChildren() []*Context {
var children []*Context
for name, data := range self.Data.(ard.Map) {
children = append(children, self.FieldChild(name, data))
}
return children
}
func (self *Context) MapChild(name ard.Value, data ard.Value) *Context {
nameString := strings.ReplaceAll(yamlkeys.KeyString(name), "\n", "¶") // complex keys would be stringified
return &Context{
Parent: self,
Name: nameString,
Path: self.Path.AppendMap(nameString),
URL: self.URL,
Data: data,
Locator: self.Locator,
CanonicalNamespace: self.CanonicalNamespace,
Namespace: self.Namespace,
ScriptletNamespace: self.ScriptletNamespace,
Hierarchy: self.Hierarchy,
Problems: self.Problems,
Quirks: self.Quirks,
Grammar: self.Grammar,
}
}
func (self *Context) ListChild(index int, data ard.Value) *Context {
return &Context{
Parent: self,
Name: strconv.FormatInt(int64(index), 10),
Path: self.Path.AppendList(index),
URL: self.URL,
Data: data,
Locator: self.Locator,
CanonicalNamespace: self.CanonicalNamespace,
Namespace: self.Namespace,
ScriptletNamespace: self.ScriptletNamespace,
Hierarchy: self.Hierarchy,
Problems: self.Problems,
Quirks: self.Quirks,
Grammar: self.Grammar,
}
}
func (self *Context) SequencedListChild(index int, name string, data ard.Value) *Context {
return &Context{
Parent: self,
Name: name,
Path: self.Path.AppendSequencedList(index),
URL: self.URL,
Data: data,
Locator: self.Locator,
CanonicalNamespace: self.CanonicalNamespace,
Namespace: self.Namespace,
ScriptletNamespace: self.ScriptletNamespace,
Hierarchy: self.Hierarchy,
Problems: self.Problems,
Quirks: self.Quirks,
Grammar: self.Grammar,
}
}