/
union.go
56 lines (50 loc) · 1.17 KB
/
union.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
package datalarkengine
import (
"fmt"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/printer"
"github.com/ipld/go-ipld-prime/schema"
"go.starlark.net/starlark"
)
type unionValue struct {
node datamodel.Node
}
var _ Value = (*unionValue)(nil)
func newUnionValue(node datamodel.Node) Value {
return &unionValue{node}
}
func (v *unionValue) Node() datamodel.Node {
return v.node
}
func (v *unionValue) Type() string {
return fmt.Sprintf("datalark.Union<%T>", v.node.(schema.TypedNode).Type().Name())
}
func (v *unionValue) String() string {
return printer.Sprint(v.node)
}
func (v *unionValue) Freeze() {}
func (v *unionValue) Truth() starlark.Bool {
return true
}
func (v *unionValue) Hash() (uint32, error) {
// Riffing off Starlark's algorithm for Tuple, which is in turn riffing off Python.
var x, mult uint32 = 0x345678, 1000003
l := v.node.Length()
for itr := v.node.MapIterator(); !itr.Done(); {
_, v, err := itr.Next()
if err != nil {
return 0, err
}
w, err := ToValue(v)
if err != nil {
return 0, err
}
y, err := w.Hash()
if err != nil {
return 0, err
}
x = x ^ y*mult
mult += 82520 + uint32(l+l)
}
return x, nil
}