-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue.go
More file actions
80 lines (71 loc) · 1.64 KB
/
Copy pathvalue.go
File metadata and controls
80 lines (71 loc) · 1.64 KB
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
// Package codemeta reads and validates CodeMeta metadata offline.
package codemeta
// Kind distinguishes JSON values. A zero Value is Missing.
type Kind uint8
const (
Missing Kind = iota
Null
String
Number
Boolean
Array
Object
)
// Value owns its text and preserves source order and numeric spelling.
type Value struct {
kind Kind
text string
items []Value
fields []Field
pos Position
}
type Field struct {
Name string
Value Value
Position Position
}
func (v Value) Kind() Kind { return v.kind }
func (v Value) Text() string { return v.text }
func (v Value) Position() Position { return v.pos }
func (v Value) Items() []Value { return append([]Value(nil), v.items...) }
func (v Value) Fields() []Field { return append([]Field(nil), v.fields...) }
// Values returns array items, a single scalar or object, or nil for Missing.
// An explicit null remains a single Null value.
func (v Value) Values() []Value {
if v.kind == Missing {
return nil
}
if v.kind == Array {
return v.Items()
}
return []Value{v}
}
// Get looks up the exact written key, without context resolution.
func (v Value) Get(name string) Value {
for _, field := range v.fields {
if field.Name == name {
return field.Value
}
}
return Value{}
}
type Document struct {
root Value
maxDiagnostics int
context contextState
}
func (d *Document) Get(name string) Value {
if d == nil {
return Value{}
}
if d.context.usable() {
return resolvedGet(d.root, name, contextTerms[d.context.version])
}
return d.root.Get(name)
}
func (d *Document) Fields() []Field {
if d == nil {
return nil
}
return d.root.Fields()
}