-
-
Notifications
You must be signed in to change notification settings - Fork 299
/
binary.go
86 lines (64 loc) · 1.28 KB
/
binary.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
package values
import (
"encoding/json"
"hash/fnv"
"io"
"io/ioutil"
"github.com/MontFerret/ferret/pkg/runtime/core"
)
type Binary struct {
values []byte
}
func NewBinary(values []byte) *Binary {
return &Binary{values}
}
func NewBinaryFrom(stream io.Reader) (*Binary, error) {
values, err := ioutil.ReadAll(stream)
if err != nil {
return nil, err
}
return &Binary{values}, nil
}
func (b *Binary) MarshalJSON() ([]byte, error) {
return json.Marshal(b.values)
}
func (b *Binary) Type() core.Type {
return core.BinaryType
}
func (b *Binary) String() string {
return string(b.values)
}
func (b *Binary) Compare(other core.Value) int {
// TODO: Lame comparison, need to think more about it
switch other.Type() {
case core.BooleanType:
b2 := other.(*Binary)
if b2.Length() == b.Length() {
return 0
}
if b.Length() > b2.Length() {
return 1
}
return -1
default:
return 1
}
}
func (b *Binary) Unwrap() interface{} {
return b.values
}
func (b *Binary) Hash() uint64 {
h := fnv.New64a()
h.Write([]byte(b.Type().String()))
h.Write([]byte(":"))
h.Write(b.values)
return h.Sum64()
}
func (b *Binary) Copy() core.Value {
c := make([]byte, len(b.values))
copy(c, b.values)
return NewBinary(c)
}
func (b *Binary) Length() Int {
return NewInt(len(b.values))
}