-
Notifications
You must be signed in to change notification settings - Fork 15
/
value_tuple_dict_entry.go
220 lines (187 loc) · 5.22 KB
/
value_tuple_dict_entry.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
package rel
import (
"context"
"fmt"
"reflect"
"github.com/arr-ai/wbnf/parser"
)
const DictValueAttr = "@value"
// DictEntryTuple represents a tuple of the form (@: at, @value: item).
type DictEntryTuple struct {
at Value
value Value
}
// NewDictEntryTuple constructs a CharTuple.
func NewDictEntryTuple(at Value, item Value) DictEntryTuple {
return DictEntryTuple{at: at, value: item}
}
func newDictEntryTupleFromTuple(t Tuple) (DictEntryTuple, bool) {
var at Value
var item Value
m := NewTupleMatcher(
map[string]Matcher{
"@": Let(func(i Value) { at = i }),
DictValueAttr: Let(func(v Value) { item = v }),
},
Lit(EmptyTuple),
)
if m.Match(t) {
return NewDictEntryTuple(at, item), true
}
return DictEntryTuple{}, false
}
func maybeNewDictEntryTupleFromTuple(t Tuple) Tuple {
if t, ok := newDictEntryTupleFromTuple(t); ok {
return t
}
return t
}
func (t DictEntryTuple) asGenericTuple() Tuple {
return newTuple(
NewAttr("@", t.at),
NewAttr(DictValueAttr, t.value),
)
}
// Hash computes a hash for a CharTuple.
func (t DictEntryTuple) Hash(seed uintptr) uintptr {
return t.value.Hash(t.at.Hash(seed))
}
// Equal tests two Tuples for equality. Any other type returns false.
func (t DictEntryTuple) Equal(v interface{}) bool {
if u, ok := v.(DictEntryTuple); ok {
return t.at.Equal(u.at) && t.value.Equal(u.value)
}
return false
}
// String returns a string representation of a Tuple.
func (t DictEntryTuple) String() string {
return fmt.Sprintf("(@: %v, %s: %v)", t.at, DictValueAttr, t.value)
}
// Eval returns the tuple.
func (t DictEntryTuple) Eval(ctx context.Context, local Scope) (Value, error) {
return t, nil
}
// Source returns a scanner locating the DictEntryTuple's source code.
func (t DictEntryTuple) Source() parser.Scanner {
return *parser.NewScanner("")
}
var dictValueTupleKind = registerKind(303, reflect.TypeOf((*DictEntryTuple)(nil)))
// Kind returns a number that is unique for each major kind of Value.
func (t DictEntryTuple) Kind() int {
return dictValueTupleKind
}
// Bool returns true iff the tuple has attributes.
func (t DictEntryTuple) IsTrue() bool {
return true
}
// Less returns true iff v is not a number or tuple, or v is a tuple and t
// precedes v in a lexicographical comparison of their name/value pairs.
func (t DictEntryTuple) Less(v Value) bool {
if t.Kind() != v.Kind() {
return t.Kind() < v.Kind()
}
u := v.(DictEntryTuple)
if !t.at.Equal(u.at) {
return t.at.Less(u.at)
}
return t.value.Less(u.value)
}
func (t DictEntryTuple) Negate() Value {
return DictEntryTuple{at: t.at.Negate(), value: t.value.Negate()}
}
// Export exports a Tuple.
func (t DictEntryTuple) Export(ctx context.Context) interface{} {
return map[string]interface{}{
"@": t.at.Export(ctx),
DictValueAttr: t.value.Export(ctx),
}
}
// Count returns how many attributes are in the Tuple.
func (t DictEntryTuple) Count() int {
return 2
}
// Get returns the Value associated with a name, and true iff it was found.
func (t DictEntryTuple) Get(name string) (Value, bool) {
switch name {
case "@":
return t.at, true
case DictValueAttr:
return t.value, true
}
return nil, false
}
// MustGet returns e.Get(name) or panics if an error occurs.
func (t DictEntryTuple) MustGet(name string) Value {
if v, has := t.Get(name); has {
return v
}
panic(fmt.Errorf("%q not found", name))
}
// With returns a Tuple with all name/Value pairs in t (except the one for the
// given name, if present) with the addition of the given name/Value pair.
func (t DictEntryTuple) With(name string, value Value) Tuple {
return maybeNewDictEntryTupleFromTuple(t.asGenericTuple().With(name, value))
}
// Without returns a Tuple with all name/Value pairs in t exception the one of
// the given name.
func (t DictEntryTuple) Without(name string) Tuple {
if name == "@" || name == DictValueAttr {
return t.asGenericTuple().Without(name)
}
return t
}
func (t DictEntryTuple) Map(f func(Value) (Value, error)) (Tuple, error) {
at, err := f(t.at)
if err != nil {
return nil, err
}
value, err := f(t.value)
if err != nil {
return nil, err
}
return NewDictEntryTuple(at, value), nil
}
// HasName returns true iff the Tuple has an attribute with the given name.
func (t DictEntryTuple) HasName(name string) bool {
return name == "@" || name == DictValueAttr
}
// Names returns the attribute names.
func (t DictEntryTuple) Names() Names {
return NewNames("@", DictValueAttr)
}
// Project returns a tuple with the given names from this tuple, or nil if any
// name wasn't found.
func (t DictEntryTuple) Project(names Names) Tuple {
if names.Has("@") && names.Has(DictValueAttr) {
return t
}
return t.asGenericTuple().Project(names)
}
// Enumerator returns an enumerator over the Values in the CharTuple.
func (t DictEntryTuple) Enumerator() AttrEnumerator {
return &dictEntryTupleEnumerator{t: t, i: -1}
}
type dictEntryTupleEnumerator struct {
t DictEntryTuple
i int
name string
value Value
}
func (e *dictEntryTupleEnumerator) MoveNext() bool {
if e.i == 1 {
return false
}
e.i++
switch e.i {
case 0:
e.name = "@"
e.value = e.t.at
case 1:
e.name = DictValueAttr
e.value = e.t.value
}
return true
}
func (e *dictEntryTupleEnumerator) Current() (string, Value) {
return e.name, e.value
}