/
marshal.go
289 lines (279 loc) · 7.41 KB
/
marshal.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
package dagjson
import (
"encoding/base64"
"fmt"
"io"
"sort"
"github.com/polydawn/refmt/json"
"github.com/polydawn/refmt/shared"
"github.com/polydawn/refmt/tok"
"github.com/ipld/go-ipld-prime/codec"
"github.com/ipld/go-ipld-prime/datamodel"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
)
// This should be identical to the general feature in the parent package,
// except for the `case datamodel.Kind_Link` block,
// which is dag-json's special sauce for schemafree links.
// EncodeOptions can be used to customize the behavior of an encoding function.
// The Encode method on this struct fits the codec.Encoder function interface.
type EncodeOptions struct {
// If true, will encode nodes with a Link kind using the DAG-JSON
// `{"/":"cid string"}` form.
EncodeLinks bool
// If true, will encode nodes with a Bytes kind using the DAG-JSON
// `{"/":{"bytes":"base64 bytes..."}}` form.
EncodeBytes bool
// Control the sorting of map keys, using one of the `codec.MapSortMode_*` constants.
MapSortMode codec.MapSortMode
}
// Encode walks the given datamodel.Node and serializes it to the given io.Writer.
// Encode fits the codec.Encoder function interface.
//
// The behavior of the encoder can be customized by setting fields in the EncodeOptions struct before calling this method.
func (cfg EncodeOptions) Encode(n datamodel.Node, w io.Writer) error {
return Marshal(n, json.NewEncoder(w, json.EncodeOptions{}), cfg)
}
// Future work: we would like to remove the Marshal function,
// and in particular, stop seeing types from refmt (like shared.TokenSink) be visible.
// Right now, some kinds of configuration (e.g. for whitespace and prettyprint) are only available through interacting with the refmt types;
// we should improve our API so that this can be done with only our own types in this package.
// Marshal is a deprecated function.
// Please consider switching to EncodeOptions.Encode instead.
func Marshal(n datamodel.Node, sink shared.TokenSink, options EncodeOptions) error {
var tk tok.Token
switch n.Kind() {
case datamodel.Kind_Invalid:
return fmt.Errorf("cannot traverse a node that is absent")
case datamodel.Kind_Null:
tk.Type = tok.TNull
_, err := sink.Step(&tk)
return err
case datamodel.Kind_Map:
// Emit start of map.
tk.Type = tok.TMapOpen
expectedLength := int(n.Length())
tk.Length = expectedLength // TODO: overflow check
if _, err := sink.Step(&tk); err != nil {
return err
}
if options.MapSortMode != codec.MapSortMode_None {
// Collect map entries, then sort by key
type entry struct {
key string
value datamodel.Node
}
entries := []entry{}
for itr := n.MapIterator(); !itr.Done(); {
k, v, err := itr.Next()
if err != nil {
return err
}
keyStr, err := k.AsString()
if err != nil {
return err
}
entries = append(entries, entry{keyStr, v})
}
if len(entries) != expectedLength {
return fmt.Errorf("map Length() does not match number of MapIterator() entries")
}
// Apply the desired sort function.
switch options.MapSortMode {
case codec.MapSortMode_Lexical:
sort.Slice(entries, func(i, j int) bool {
return entries[i].key < entries[j].key
})
case codec.MapSortMode_RFC7049:
sort.Slice(entries, func(i, j int) bool {
// RFC7049 style sort as per DAG-CBOR spec
li, lj := len(entries[i].key), len(entries[j].key)
if li == lj {
return entries[i].key < entries[j].key
}
return li < lj
})
}
// Emit map contents (and recurse).
var entryCount int
for _, e := range entries {
tk.Type = tok.TString
tk.Str = e.key
entryCount++
if _, err := sink.Step(&tk); err != nil {
return err
}
if err := Marshal(e.value, sink, options); err != nil {
return err
}
}
if entryCount != expectedLength {
return fmt.Errorf("map Length() does not match number of MapIterator() entries")
}
} else {
// Don't sort map, emit map contents (and recurse).
for itr := n.MapIterator(); !itr.Done(); {
k, v, err := itr.Next()
if err != nil {
return err
}
tk.Type = tok.TString
tk.Str, err = k.AsString()
if err != nil {
return err
}
if _, err := sink.Step(&tk); err != nil {
return err
}
if err := Marshal(v, sink, options); err != nil {
return err
}
}
}
// Emit map close.
tk.Type = tok.TMapClose
_, err := sink.Step(&tk)
return err
case datamodel.Kind_List:
// Emit start of list.
tk.Type = tok.TArrOpen
l := n.Length()
tk.Length = int(l) // TODO: overflow check
if _, err := sink.Step(&tk); err != nil {
return err
}
// Emit list contents (and recurse).
for i := int64(0); i < l; i++ {
v, err := n.LookupByIndex(i)
if err != nil {
return err
}
if err := Marshal(v, sink, options); err != nil {
return err
}
}
// Emit list close.
tk.Type = tok.TArrClose
_, err := sink.Step(&tk)
return err
case datamodel.Kind_Bool:
v, err := n.AsBool()
if err != nil {
return err
}
tk.Type = tok.TBool
tk.Bool = v
_, err = sink.Step(&tk)
return err
case datamodel.Kind_Int:
v, err := n.AsInt()
if err != nil {
return err
}
tk.Type = tok.TInt
tk.Int = int64(v)
_, err = sink.Step(&tk)
return err
case datamodel.Kind_Float:
v, err := n.AsFloat()
if err != nil {
return err
}
tk.Type = tok.TFloat64
tk.Float64 = v
_, err = sink.Step(&tk)
return err
case datamodel.Kind_String:
v, err := n.AsString()
if err != nil {
return err
}
tk.Type = tok.TString
tk.Str = v
_, err = sink.Step(&tk)
return err
case datamodel.Kind_Bytes:
v, err := n.AsBytes()
if err != nil {
return err
}
if options.EncodeBytes {
// Precisely seven tokens to emit:
tk.Type = tok.TMapOpen
tk.Length = 1
if _, err = sink.Step(&tk); err != nil {
return err
}
tk.Type = tok.TString
tk.Str = "/"
if _, err = sink.Step(&tk); err != nil {
return err
}
tk.Type = tok.TMapOpen
tk.Length = 1
if _, err = sink.Step(&tk); err != nil {
return err
}
tk.Type = tok.TString
tk.Str = "bytes"
if _, err = sink.Step(&tk); err != nil {
return err
}
tk.Str = base64.RawStdEncoding.EncodeToString(v)
if _, err = sink.Step(&tk); err != nil {
return err
}
tk.Type = tok.TMapClose
if _, err = sink.Step(&tk); err != nil {
return err
}
tk.Type = tok.TMapClose
if _, err = sink.Step(&tk); err != nil {
return err
}
return nil
} else {
tk.Type = tok.TBytes
tk.Bytes = v
_, err = sink.Step(&tk)
return err
}
case datamodel.Kind_Link:
if !options.EncodeLinks {
return fmt.Errorf("cannot Marshal ipld links to JSON")
}
v, err := n.AsLink()
if err != nil {
return err
}
switch lnk := v.(type) {
case cidlink.Link:
if !lnk.Cid.Defined() {
return fmt.Errorf("encoding undefined CIDs are not supported by this codec")
}
// Precisely four tokens to emit:
tk.Type = tok.TMapOpen
tk.Length = 1
if _, err = sink.Step(&tk); err != nil {
return err
}
tk.Type = tok.TString
tk.Str = "/"
if _, err = sink.Step(&tk); err != nil {
return err
}
tk.Str = lnk.Cid.String()
if _, err = sink.Step(&tk); err != nil {
return err
}
tk.Type = tok.TMapClose
if _, err = sink.Step(&tk); err != nil {
return err
}
return nil
default:
return fmt.Errorf("schemafree link emission only supported by this codec for CID type links; got type %T", lnk)
}
default:
panic("unreachable")
}
}