-
Notifications
You must be signed in to change notification settings - Fork 15
/
value_tuple.go
409 lines (361 loc) · 9.92 KB
/
value_tuple.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package rel
import (
"bytes"
"context"
"fmt"
"reflect"
"regexp"
"sort"
"strings"
"github.com/arr-ai/frozen"
"github.com/arr-ai/wbnf/parser"
)
// GenericTuple is the default implementation of Tuple.
type GenericTuple struct {
tuple frozen.Map
names []string
}
var (
// EmptyTuple is the tuple with no attributes.
EmptyTuple Tuple = &GenericTuple{}
negateTag = "@neg"
)
type TupleBuilder frozen.MapBuilder
func (b *TupleBuilder) Put(name string, value Value) {
(*frozen.MapBuilder)(b).Put(name, value)
}
func (b *TupleBuilder) Finish() Tuple {
return &GenericTuple{tuple: (*frozen.MapBuilder)(b).Finish()}
}
// NewAttr returns an Attr with the given name and value.
func NewAttr(name string, value Value) Attr {
return Attr{Name: name, Value: value}
}
// NewBoolAttr return an attr with a bool value.
func NewBoolAttr(name string, value bool) Attr {
return NewAttr(name, NewBool(value))
}
// NewFloatAttr return an attr with a float value.
func NewFloatAttr(name string, value float64) Attr {
return NewAttr(name, NewNumber(value))
}
// NewIntAttr return an attr with an int value.
func NewIntAttr(name string, value int) Attr {
return NewFloatAttr(name, float64(value))
}
// NewStringAttr return an attr with a string value.
func NewStringAttr(name string, value []rune) Attr {
return NewAttr(name, NewString(value))
}
// NewTupleAttr return an attr with a new tuple value.
func NewTupleAttr(name string, attrs ...Attr) Attr {
return NewAttr(name, NewTuple(attrs...))
}
// NewTuple constructs a Tuple from attrs. Passes each Val to NewValue().
func NewTuple(attrs ...Attr) Tuple {
if len(attrs) == 2 {
if attrs[1].Name == "@" {
attrs[0], attrs[1] = attrs[1], attrs[0]
}
if attrs[0].Name == "@" && strings.HasPrefix(attrs[1].Name, "@") {
switch attrs[1].Name {
case StringCharAttr:
return NewStringCharTuple(
int(attrs[0].Value.(Number).Float64()),
rune(attrs[1].Value.(Number).Float64()),
)
case BytesByteAttr:
return NewBytesByteTuple(
int(attrs[0].Value.(Number).Float64()),
byte(attrs[1].Value.(Number).Float64()),
)
case ArrayItemAttr:
return NewArrayItemTuple(int(attrs[0].Value.(Number).Float64()), attrs[1].Value)
case DictValueAttr:
return NewDictEntryTuple(attrs[0].Value, attrs[1].Value)
}
}
}
return newTuple(attrs...)
}
func newTuple(attrs ...Attr) Tuple {
var b TupleBuilder
for _, kv := range attrs {
b.Put(kv.Name, kv.Value)
}
return b.Finish()
}
// NewTupleFromMap constructs a Tuple from a map of strings to Go values.
func NewTupleFromMap(m map[string]interface{}) (Tuple, error) {
var b TupleBuilder
for name, intf := range m {
value, err := NewValue(intf)
if err != nil {
return nil, err
}
b.Put(name, value)
}
return b.Finish(), nil
}
// NewXML constructs an XML Tuple from the given data
func NewXML(tag []rune, attrs []Attr, children ...Value) Tuple {
var b TupleBuilder
b.Put("tag", NewString(tag))
if len(attrs) != 0 {
b.Put("attributes", NewTuple(attrs...))
}
if len(children) != 0 {
b.Put("children", NewArray(children...))
}
return EmptyTuple.With("@xml", b.Finish())
}
func (t *GenericTuple) Canonical() Tuple {
attrs := make([]Attr, 0, t.Count())
for e := t.Enumerator(); e.MoveNext(); {
name, value := e.Current()
attrs = append(attrs, NewAttr(name, value))
}
return NewTuple(attrs...)
}
// Hash computes a hash for a GenericTuple.
func (t *GenericTuple) Hash(seed uintptr) uintptr {
return t.tuple.Hash(seed)
}
// Equal tests two Tuples for equality. Any other type returns false.
func (t *GenericTuple) Equal(v interface{}) bool {
if b, ok := v.(Tuple); ok {
for e := t.Enumerator(); e.MoveNext(); {
aName, aValue := e.Current()
if bVal, found := b.Get(aName); found {
if !aValue.Equal(bVal) {
return false
}
} else {
return false
}
}
for e := b.Enumerator(); e.MoveNext(); {
name, _ := e.Current()
if _, found := t.Get(name); !found {
return false
}
}
return true
}
return false
}
// LexerNamePat defines valid unquoted identifiers.
// This really belongs in rel/syntax/lex.go, but that creates a dep cycle.
var LexerNamePat = `([$@A-Za-z_][0-9$@A-Za-z_]*)`
var identRE = regexp.MustCompile(`\A` + LexerNamePat + `\z`)
func TupleNameRepr(name string) string {
if identRE.Match([]byte(name)) {
return name
}
var sb strings.Builder
switch {
case !strings.Contains(name, "'"):
reprEscape(name, '\'', &sb)
default:
reprEscape(name, '"', &sb)
}
return sb.String()
}
// String returns a string representation of a Tuple.
func (t *GenericTuple) String() string {
var buf bytes.Buffer
buf.WriteRune('(')
for i, name := range TupleOrderedNames(t) {
if i != 0 {
buf.WriteString(", ")
}
fmt.Fprintf(&buf, "%s: %s", TupleNameRepr(name), t.MustGet(name).String())
}
buf.WriteRune(')')
return buf.String()
}
// Eval returns the tuple.
func (t *GenericTuple) Eval(ctx context.Context, local Scope) (Value, error) {
return t, nil
}
// Source returns a scanner locating the GenericTuple's source code.
func (t *GenericTuple) Source() parser.Scanner {
return *parser.NewScanner("")
}
var genericTupleKind = registerKind(300, reflect.TypeOf((*GenericTuple)(nil)))
// Kind returns a number that is unique for each major kind of Value.
func (t *GenericTuple) Kind() int {
if t.Count() == 1 {
if x, ok := t.Get(negateTag); ok {
return -x.Kind()
}
}
return genericTupleKind
}
// Bool returns true iff the tuple has attributes.
func (t *GenericTuple) IsTrue() bool {
return t.Count() > 0
}
// 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 *GenericTuple) Less(v Value) bool {
if t.Kind() != v.Kind() {
return t.Kind() < v.Kind()
}
if t.Count() == 1 {
if x, ok := t.Get(negateTag); ok {
u := v.(Tuple)
if u.Count() != 1 {
panic(negateTag + " kind not single-attr tuple")
}
if y, ok := v.(Tuple).Get(negateTag); ok {
return y.Less(x)
}
panic(negateTag + " kind missing " + negateTag + " attr")
}
}
x := v.(*GenericTuple)
a := TupleOrderedNames(t)
b := TupleOrderedNames(x)
n := len(a)
if n > len(b) {
n = len(b)
}
for i := 0; i < n; i++ {
if a[i] != b[i] {
return a[i] < b[i]
}
va, _ := t.Get(a[i])
vb, _ := x.Get(b[i])
if va.Less(vb) {
return true
}
if vb.Less(va) {
return false
}
}
return len(a) < len(b)
}
// Negate returns x if t matches {(negateTag): x} else {(negateTag): t}.
func (t *GenericTuple) Negate() Value {
if t.Count() == 1 {
if x, ok := t.Get(negateTag); ok {
return x
}
}
if !t.IsTrue() {
return t
}
return NewTuple(NewAttr(negateTag, t))
}
// Export exports a Tuple.
func (t *GenericTuple) Export(ctx context.Context) interface{} {
result := make(map[string]interface{}, t.Count())
for e := t.Enumerator(); e.MoveNext(); {
name, value := e.Current()
result[name] = value.Export(ctx)
}
return result
}
// Count returns how many attributes are in the Tuple.
func (t *GenericTuple) Count() int {
return t.tuple.Count()
}
// Get returns the Value associated with a name, and true iff it was found.
func (t *GenericTuple) Get(name string) (Value, bool) {
if v, found := t.tuple.Get(name); found {
return v.(Value), true
}
return nil, false
}
// MustGet returns e.Get(name) or panics if an error occurs.
func (t *GenericTuple) 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 *GenericTuple) With(name string, value Value) Tuple {
// Strip view/non-view counterpart.
if strings.HasPrefix(name, "&") {
t = t.Without(name[1:]).(*GenericTuple)
} else {
t = t.Without("&" + name).(*GenericTuple)
}
return &GenericTuple{tuple: t.tuple.With(name, value)}
}
// Without returns a Tuple with all name/Value pairs in t exception the one of
// the given name.
func (t *GenericTuple) Without(name string) Tuple {
return &GenericTuple{tuple: t.tuple.Without(frozen.NewSet(name))}
}
func (t *GenericTuple) Map(f func(Value) (Value, error)) (Tuple, error) {
var b frozen.MapBuilder
for e := t.Enumerator(); e.MoveNext(); {
key, value := e.Current()
v, err := f(value)
if err != nil {
return nil, err
}
b.Put(key, v)
}
return &GenericTuple{tuple: b.Finish()}, nil
}
// HasName returns true iff the Tuple has an attribute with the given name.
func (t *GenericTuple) HasName(name string) bool {
_, found := t.tuple.Get(name)
return found
}
// Names returns the attribute names.
func (t *GenericTuple) Names() Names {
var b frozen.SetBuilder
for e := t.Enumerator(); e.MoveNext(); {
name, _ := e.Current()
b.Add(name)
}
return Names(b.Finish())
}
// Project returns a tuple with the given names from this tuple, or nil if any
// name wasn't found.
func (t *GenericTuple) Project(names Names) Tuple {
var b TupleBuilder
for e := names.Enumerator(); e.MoveNext(); {
name := e.Current()
value, found := t.Get(name)
if !found {
return nil
}
b.Put(name, value)
}
return b.Finish()
}
// GenericTupleEnumerator represents an enumerator over a GenericTuple.
type GenericTupleEnumerator struct {
i *frozen.MapIterator
}
// MoveNext moves the enumerator to the next Value.
func (e *GenericTupleEnumerator) MoveNext() bool {
return e.i.Next()
}
// Current returns the enumerator's current Value.
func (e *GenericTupleEnumerator) Current() (string, Value) {
k, v := e.i.Entry()
return k.(string), v.(Value)
}
// Enumerator returns an enumerator over the Values in the GenericTuple.
func (t *GenericTuple) Enumerator() AttrEnumerator {
return &GenericTupleEnumerator{i: t.tuple.Range()}
}
// TupleOrderedNames returns the names of this tuple in sorted order.
func TupleOrderedNames(t *GenericTuple) []string {
if len(t.names) == 0 {
for e := t.Enumerator(); e.MoveNext(); {
name, _ := e.Current()
t.names = append(t.names, name)
}
sort.Strings(t.names)
}
return t.names
}