-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
132 lines (106 loc) · 3.17 KB
/
types.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
/*
* go-leia
* Copyright (C) 2021 Nuts community
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
package leia
import (
"encoding/binary"
"encoding/hex"
"errors"
"math"
)
const boltDBFileMode = 0600
const KeyDelimiter = 0x10
// Document represents a JSON document in []byte format
type Document []byte
// ErrInvalidJSON is returned when invalid JSON is parsed
var ErrInvalidJSON = errors.New("invalid json")
// ErrInvalidQuery is returned when a collection is queried with the wrong type
var ErrInvalidQuery = errors.New("invalid query type")
// Reference equals a document hash. In an index, the values are references to docs.
type Reference []byte
// EncodeToString encodes the reference as hex encoded string
func (r Reference) EncodeToString() string {
return hex.EncodeToString(r)
}
// ByteSize returns the size of the reference, eg: 32 bytes for a sha256
func (r Reference) ByteSize() int {
return len(r)
}
// Scalar represents a JSON or JSON-LD scalar (string, number, true or false)
type Scalar interface {
// Bytes returns the byte value
Bytes() []byte
// value helps in testing
value() interface{}
}
type StringScalar string
func (ss StringScalar) Bytes() []byte {
return []byte(ss)
}
func (ss StringScalar) value() interface{} {
return string(ss)
}
type BoolScalar bool
func (bs BoolScalar) Bytes() []byte {
if bs {
return []byte{1}
}
return []byte{0}
}
func (bs BoolScalar) value() interface{} {
return bool(bs)
}
type Float64Scalar float64
func (fs Float64Scalar) Bytes() []byte {
var buf [8]byte
binary.BigEndian.PutUint64(buf[:], math.Float64bits(float64(fs)))
return buf[:]
}
func (fs Float64Scalar) value() interface{} {
return float64(fs)
}
// bytesScalar is used internally for the NotNil query
type bytesScalar []byte
func (bs bytesScalar) Bytes() []byte {
return bs
}
func (bs bytesScalar) value() interface{} {
return bs.Bytes()
}
// ErrInvalidValue is returned when an invalid value is parsed
var ErrInvalidValue = errors.New("invalid value")
// ParseScalar returns a Scalar based on an interface value. It returns ErrInvalidValue for unsupported values.
func ParseScalar(value interface{}) (Scalar, error) {
switch castValue := value.(type) {
case bool:
return BoolScalar(castValue), nil
case string:
return StringScalar(castValue), nil
case float64:
return Float64Scalar(castValue), nil
}
return nil, ErrInvalidValue
}
// MustParseScalar returns a Scalar based on an interface value. It panics when the value is not supported.
func MustParseScalar(value interface{}) Scalar {
s, err := ParseScalar(value)
if err != nil {
panic(err)
}
return s
}