-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvalue.go
237 lines (210 loc) · 5.6 KB
/
value.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
package automerge
import (
"fmt"
"strings"
"time"
)
// Value represents a dynamically typed value read from a document.
// It can hold any of the supported primative types (bool, string, []byte, float64, int64, uint64, time.Time)
// the four mutable types (*Map, *List, *Text, *Counter), or it can be an explicit null,
// or a void to indicate that no value existed at all.
// You can convert from a Value to a go type using [As], or call accessor methods directly.
type Value struct {
item *item
doc *Doc
kind Kind
val any
}
func newValue(i *item, d *Doc) *Value {
v := &Value{item: i, doc: d}
v.kind = v.item.Kind()
var objID *objID
if v.kind == kindObjType {
objID = v.item.objID()
v.kind = objID.objKind(d)
}
switch v.kind {
case KindNull, KindVoid, KindUnknown:
// TODO: handle unknown values better?
v.val = nil
case KindBool:
v.val = v.item.bool()
case KindStr:
v.val = v.item.str()
case KindBytes:
v.val = v.item.bytes()
case KindFloat64:
v.val = v.item.float64()
case KindInt64:
v.val = v.item.int64()
case KindUint64:
v.val = v.item.uint64()
case KindTime:
v.val = v.item.time()
case KindCounter:
v.val = v.item.counter()
case KindMap:
v.val = &Map{doc: d, objID: objID}
case KindList:
v.val = &List{doc: d, objID: objID}
case KindText:
v.val = &Text{doc: d, objID: objID}
default:
panic(fmt.Errorf("tried to create Value with Kind == %v", v.kind))
}
return v
}
func newValueInMap(i *item, m *Map, key string) *Value {
v := newValue(i, m.doc)
if c, ok := v.val.(*Counter); ok {
c.m = m
c.key = key
}
return v
}
func newValueInList(i *item, l *List, idx int) *Value {
v := newValue(i, l.doc)
if c, ok := v.val.(*Counter); ok {
c.l = l
c.idx = idx
}
return v
}
// Kind reports the kind of the value
func (v *Value) Kind() Kind {
return v.kind
}
// IsVoid returns true if the value did not exist in the document
func (v *Value) IsVoid() bool {
return v.Kind() == KindVoid
}
// IsNull returns true if the value is null
func (v *Value) IsNull() bool {
return v.Kind() == KindNull
}
// IsUnknown returns true if the type of the value was unknown
func (v *Value) IsUnknown() bool {
return v.kind == KindUnknown
}
// Bool returns the value as a bool, it panics if Kind() != KindBool
func (v *Value) Bool() bool {
v.assertKind(KindBool)
return v.val.(bool)
}
// Str returns the Value as a string, it panics if Kind() != KindStr
func (v *Value) Str() string {
v.assertKind(KindStr)
return v.val.(string)
}
// Bytes returns the Value as a []byte, it panics if Kind() != KindBytes
func (v *Value) Bytes() []byte {
v.assertKind(KindBytes)
return v.val.([]byte)
}
// Float64 returns the value as a float64, it panics if Kind() != KindFloat64
func (v *Value) Float64() float64 {
v.assertKind(KindFloat64)
return v.val.(float64)
}
// Int64 returns the value as a int64, it panics if Kind() != KindInt64
func (v *Value) Int64() int64 {
v.assertKind(KindInt64)
return v.val.(int64)
}
// Uint64 returns the value as a uint64, it panics if Kind() != KindUint64
func (v *Value) Uint64() uint64 {
v.assertKind(KindUint64)
return v.val.(uint64)
}
// Time returns the value as a time.Time, it panics if Kind() != KindTime
func (v *Value) Time() time.Time {
v.assertKind(KindTime)
return v.val.(time.Time)
}
// Map returns the value as a [*Map], it panics if Kind() != KindMap
func (v *Value) Map() *Map {
v.assertKind(KindMap)
return v.val.(*Map)
}
// List returns the value as a [*List], it panics if Kind() != KindList
func (v *Value) List() *List {
v.assertKind(KindList)
return v.val.(*List)
}
// Counter returns the value as a [*Counter], it panics if Kind() != KindCounter
func (v *Value) Counter() *Counter {
v.assertKind(KindCounter)
return v.val.(*Counter)
}
// Text returns the value as a [*Text], it panics if Kind() != KindText
func (v *Value) Text() *Text {
v.assertKind(KindText)
return v.val.(*Text)
}
func (v *Value) assertKind(k Kind) {
if v.Kind() != k {
panic(fmt.Errorf("automerge.Value: called .%s() on value of %v", strings.TrimPrefix(k.String(), "Kind"), v.Kind()))
}
}
// Interface returns the value as a go interface.
// It recursively converts automerge.Map to map[string]any,
// automerge.List to []any, automerge.Text to string, and
// automerge.Counter to int64.
func (v *Value) Interface() any {
switch v.kind {
case KindMap:
values, err := v.Map().Values()
// this should not be able to happen because .load() is only
// called from Value.Interface() which checks that this is a map.
if err != nil {
panic(err)
}
out := map[string]any{}
for k, v := range values {
out[k] = v.Interface()
}
return out
case KindList:
values, err := v.List().Values()
// cannot happen as we know the value must have come from the doc
if err != nil {
panic(err)
}
out := []any{}
for _, v := range values {
out = append(out, v.Interface())
}
return out
case KindText:
s, err := v.Text().Get()
// cannot happen as we know the value must have come from the doc
if err != nil {
panic(err)
}
return s
case KindCounter:
c, err := v.Counter().Get()
// cannot happen as we know the value must have come from the doc
if err != nil {
panic(err)
}
return c
default:
return v.val
}
}
// GoString returns a representation suitable for debugging.
func (v *Value) GoString() string {
if v.kind == KindVoid {
return "&automerge.Value(<void>)"
}
if v.kind == KindUnknown {
return "&automerge.Value(<unknown>)"
}
return fmt.Sprintf("&automerge.Value(%#v)", v.val)
}
// String returns a representation suitable for debugging.
// Use [Value.Str] to get the underlying string.
func (v *Value) String() string {
return v.GoString()
}