/
value_tuple_array_item.go
240 lines (204 loc) · 5.68 KB
/
value_tuple_array_item.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
package rel
import (
"context"
"fmt"
"reflect"
"github.com/arr-ai/hash"
"github.com/arr-ai/wbnf/parser"
"github.com/arr-ai/arrai/pkg/fu"
)
const ArrayItemAttr = "@item"
// ArrayItemTuple represents a tuple of the form (@: at, @item: item).
type ArrayItemTuple struct {
at int
item Value
}
// NewArrayItemTuple constructs a CharTuple.
func NewArrayItemTuple(at int, item Value) ArrayItemTuple {
return ArrayItemTuple{at: at, item: item}
}
func newArrayItemTupleFromTuple(t Tuple) (ArrayItemTuple, bool) {
var at int
var item Value
m := NewTupleMatcher(
map[string]Matcher{
"@": MatchInt(func(i int) { at = i }),
ArrayItemAttr: Let(func(v Value) { item = v }),
},
Lit(EmptyTuple),
)
if m.Match(t) {
return NewArrayItemTuple(at, item), true
}
return ArrayItemTuple{}, false
}
func maybeNewArrayItemTupleFromTuple(t Tuple) Tuple {
if t, ok := newArrayItemTupleFromTuple(t); ok {
return t
}
return t
}
func (t ArrayItemTuple) asGenericTuple() Tuple {
return newGenericTuple(
NewIntAttr("@", t.at),
NewAttr(ArrayItemAttr, t.item),
)
}
// Hash computes a hash for a CharTuple.
func (t ArrayItemTuple) Hash(seed uintptr) uintptr {
h := hash.Int(t.at, seed)
return t.item.Hash(h)
}
// Equal tests two Tuples for equality. Any other type returns false.
func (t ArrayItemTuple) Equal(v interface{}) bool {
if u, ok := v.(ArrayItemTuple); ok {
return t.at == u.at && t.item.Equal(u.item)
}
return false
}
// String returns a string representation of a Tuple.
func (t ArrayItemTuple) String() string {
return fu.String(t)
}
func (t ArrayItemTuple) Format(f fmt.State, verb rune) {
fmt.Fprintf(f, "(@: %d, "+ArrayItemAttr+": %v)", t.at, t.item)
}
// Eval returns the tuple.
func (t ArrayItemTuple) Eval(ctx context.Context, local Scope) (Value, error) {
return t, nil
}
// Source returns a scanner locating the ArrayItemTuple's source code.
func (t ArrayItemTuple) Source() parser.Scanner {
return *parser.NewScanner("")
}
var arrayItemTupleKind = registerKind(302, reflect.TypeOf((*ArrayItemTuple)(nil)))
// Kind returns a number that is unique for each major kind of Value.
func (t ArrayItemTuple) Kind() int {
return arrayItemTupleKind
}
// Bool returns true iff the tuple has attributes.
func (t ArrayItemTuple) 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 ArrayItemTuple) Less(v Value) bool {
if t.Kind() != v.Kind() {
return t.Kind() < v.Kind()
}
u := v.(ArrayItemTuple)
if t.at != u.at {
return t.at < u.at
}
return t.item.Less(u.item)
}
func (t ArrayItemTuple) Negate() Value {
return ArrayItemTuple{at: -t.at, item: t.item.Negate()}
}
// Export exports a Tuple.
func (t ArrayItemTuple) Export(ctx context.Context) interface{} {
return map[string]interface{}{
"@": t.at,
ArrayItemAttr: t.item.Export(ctx),
}
}
func (ArrayItemTuple) getSetBuilder() setBuilder {
return &genericSetBuilder{values: []Value{}, finish: arrayFinish}
}
func (ArrayItemTuple) getBucket() fmt.Stringer {
return arrayItemTupleType
}
// Count returns how many attributes are in the Tuple.
func (t ArrayItemTuple) Count() int {
return 2
}
// Get returns the Value associated with a name, and true iff it was found.
func (t ArrayItemTuple) Get(name string) (Value, bool) {
switch name {
case "@":
return NewNumber(float64(t.at)), true
case ArrayItemAttr:
return t.item, true
}
return nil, false
}
// MustGet returns e.Get(name) or panics if an error occurs.
func (t ArrayItemTuple) 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 ArrayItemTuple) With(name string, value Value) Tuple {
return maybeNewArrayItemTupleFromTuple(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 ArrayItemTuple) Without(name string) Tuple {
if name == "@" || name == ArrayItemAttr {
return t.asGenericTuple().Without(name)
}
return t
}
func (t ArrayItemTuple) Map(f func(Value) (Value, error)) (Tuple, error) {
at, err := f(NewNumber(float64(t.at)))
if err != nil {
return nil, err
}
item, err := f(t.item)
if err != nil {
return nil, err
}
if at, ok := at.(Number); ok {
if at, is := at.Int(); is {
return NewArrayItemTuple(at, item), nil
}
}
return t.asGenericTuple().Map(f)
}
// HasName returns true iff the Tuple has an attribute with the given name.
func (t ArrayItemTuple) HasName(name string) bool {
return name == "@" || name == ArrayItemAttr
}
// Names returns the attribute names.
func (t ArrayItemTuple) Names() Names {
return NewNames("@", ArrayItemAttr)
}
// Project returns a tuple with the given names from this tuple, or nil if any
// name wasn't found.
func (t ArrayItemTuple) Project(names Names) Tuple {
if names.Has("@") && names.Has(ArrayItemAttr) {
return t
}
return t.asGenericTuple().Project(names)
}
// Enumerator returns an enumerator over the Values in the CharTuple.
func (t ArrayItemTuple) Enumerator() AttrEnumerator {
return &arrayItemTupleEnumerator{t: t, i: -1}
}
type arrayItemTupleEnumerator struct {
t ArrayItemTuple
i int
name string
value Value
}
func (e *arrayItemTupleEnumerator) MoveNext() bool {
if e.i == 1 {
return false
}
e.i++
switch e.i {
case 0:
e.name = "@"
e.value = NewNumber(float64(e.t.at))
case 1:
e.name = ArrayItemAttr
e.value = e.t.item
}
return true
}
func (e *arrayItemTupleEnumerator) Current() (string, Value) {
return e.name, e.value
}