Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
ld
go-ld-prime
Commits
0ed773af
Commit
0ed773af
authored
Jul 14, 2021
by
Rod Vagg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Sort map entries marshalling dag-json
parent
fc47eb2f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
18 additions
and
6 deletions
+18
-6
codec/dagjson/marshal.go
codec/dagjson/marshal.go
+16
-4
codec/dagjson/roundtripBytes_test.go
codec/dagjson/roundtripBytes_test.go
+1
-1
codec/dagjson/roundtrip_test.go
codec/dagjson/roundtrip_test.go
+1
-1
No files found.
codec/dagjson/marshal.go
View file @
0ed773af
...
...
@@ -3,6 +3,7 @@ package dagjson
import
(
"encoding/base64"
"fmt"
"sort"
"github.com/polydawn/refmt/shared"
"github.com/polydawn/refmt/tok"
...
...
@@ -31,21 +32,32 @@ func Marshal(n ipld.Node, sink shared.TokenSink, allowLinks bool) error {
if
_
,
err
:=
sink
.
Step
(
&
tk
);
err
!=
nil
{
return
err
}
// Emit map contents (and recurse).
// Collect map entries, then sort by key
type
entry
struct
{
key
string
value
ipld
.
Node
}
entries
:=
[]
entry
{}
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
()
keyStr
,
err
:=
k
.
AsString
()
if
err
!=
nil
{
return
err
}
entries
=
append
(
entries
,
entry
{
keyStr
,
v
})
}
sort
.
Slice
(
entries
,
func
(
i
,
j
int
)
bool
{
return
entries
[
i
]
.
key
<
entries
[
j
]
.
key
})
// Emit map contents (and recurse).
for
_
,
e
:=
range
entries
{
tk
.
Type
=
tok
.
TString
tk
.
Str
=
e
.
key
if
_
,
err
:=
sink
.
Step
(
&
tk
);
err
!=
nil
{
return
err
}
if
err
:=
Marshal
(
v
,
sink
,
allowLinks
);
err
!=
nil
{
if
err
:=
Marshal
(
e
.
value
,
sink
,
allowLinks
);
err
!=
nil
{
return
err
}
}
...
...
codec/dagjson/roundtripBytes_test.go
View file @
0ed773af
...
...
@@ -15,7 +15,7 @@ var byteNode = fluent.MustBuildMap(basicnode.Prototype__Map{}, 4, func(na fluent
na
.
AssembleEntry
(
"plain"
)
.
AssignString
(
"olde string"
)
na
.
AssembleEntry
(
"bytes"
)
.
AssignBytes
([]
byte
(
"deadbeef"
))
})
var
byteSerial
=
`{"
plain":"olde string","
bytes":{"/":{"bytes":"ZGVhZGJlZWY="}}}`
var
byteSerial
=
`{"bytes":{"/":{"bytes":"ZGVhZGJlZWY="}}
,"plain":"olde string"
}`
func
TestRoundtripBytes
(
t
*
testing
.
T
)
{
t
.
Run
(
"encoding"
,
func
(
t
*
testing
.
T
)
{
...
...
codec/dagjson/roundtrip_test.go
View file @
0ed773af
...
...
@@ -27,7 +27,7 @@ var n = fluent.MustBuildMap(basicnode.Prototype__Map{}, 4, func(na fluent.MapAss
})
})
})
var
serial
=
`{"
plain":"olde string","map":{"one":1,"two":2},"list":["three","four"]
,"nested":{"deeper":["things"]}}`
var
serial
=
`{"
list":["three","four"],"map":{"one":1,"two":2}
,"nested":{"deeper":["things"]}
,"plain":"olde string"
}`
func
TestRoundtrip
(
t
*
testing
.
T
)
{
t
.
Run
(
"encoding"
,
func
(
t
*
testing
.
T
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment