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
6c9c90d1
Unverified
Commit
6c9c90d1
authored
Mar 22, 2021
by
Will Scott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add non-dag json codec
parent
94cf448d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
11 deletions
+28
-11
codec/dagjson/multicodec.go
codec/dagjson/multicodec.go
+11
-2
codec/dagjson/unmarshal.go
codec/dagjson/unmarshal.go
+17
-9
No files found.
codec/dagjson/multicodec.go
View file @
6c9c90d1
...
...
@@ -17,13 +17,23 @@ var (
func
init
()
{
multicodec
.
RegisterEncoder
(
0x0129
,
Encode
)
multicodec
.
RegisterEncoder
(
0x0200
,
Encode
)
multicodec
.
RegisterDecoder
(
0x0129
,
Decode
)
multicodec
.
RegisterDecoder
(
0x0200
,
decodeNonDagJSON
)
}
func
Decode
(
na
ipld
.
NodeAssembler
,
r
io
.
Reader
)
error
{
return
decode
(
na
,
r
,
true
)
}
func
decodeNonDagJSON
(
na
ipld
.
NodeAssembler
,
r
io
.
Reader
)
error
{
return
decode
(
na
,
r
,
false
)
}
func
decode
(
na
ipld
.
NodeAssembler
,
r
io
.
Reader
,
parseLinks
bool
)
error
{
// Shell out directly to generic builder path.
// (There's not really any fastpaths of note for json.)
err
:=
U
nmarshal
(
na
,
json
.
NewDecoder
(
r
))
err
:=
u
nmarshal
(
na
,
json
.
NewDecoder
(
r
)
,
parseLinks
)
if
err
!=
nil
{
return
err
}
...
...
@@ -49,7 +59,6 @@ func Decode(na ipld.NodeAssembler, r io.Reader) error {
return
err
}
}
return
err
}
func
Encode
(
n
ipld
.
Node
,
w
io
.
Writer
)
error
{
...
...
codec/dagjson/unmarshal.go
View file @
6c9c90d1
...
...
@@ -20,7 +20,12 @@ import (
// tokens before deciding what kind of value to create).
func
Unmarshal
(
na
ipld
.
NodeAssembler
,
tokSrc
shared
.
TokenSource
)
error
{
return
unmarshal
(
na
,
tokSrc
,
true
)
}
func
unmarshal
(
na
ipld
.
NodeAssembler
,
tokSrc
shared
.
TokenSource
,
parseLinks
bool
)
error
{
var
st
unmarshalState
st
.
parseLinks
=
parseLinks
done
,
err
:=
tokSrc
.
Step
(
&
st
.
tk
[
0
])
if
err
!=
nil
{
return
err
...
...
@@ -32,8 +37,9 @@ func Unmarshal(na ipld.NodeAssembler, tokSrc shared.TokenSource) error {
}
type
unmarshalState
struct
{
tk
[
4
]
tok
.
Token
// mostly, only 0'th is used... but [1:4] are used during lookahead for links.
shift
int
// how many times to slide something out of tk[1:4] instead of getting a new token.
tk
[
4
]
tok
.
Token
// mostly, only 0'th is used... but [1:4] are used during lookahead for links.
shift
int
// how many times to slide something out of tk[1:4] instead of getting a new token.
parseLinks
bool
}
// step leaves a "new" token in tk[0],
...
...
@@ -120,7 +126,7 @@ func (st *unmarshalState) linkLookahead(na ipld.NodeAssembler, tokSrc shared.Tok
if
err
!=
nil
{
return
false
,
err
}
if
err
:=
na
.
AssignLink
(
cidlink
.
Link
{
elCid
});
err
!=
nil
{
if
err
:=
na
.
AssignLink
(
cidlink
.
Link
{
Cid
:
elCid
});
err
!=
nil
{
return
false
,
err
}
return
true
,
nil
...
...
@@ -135,12 +141,14 @@ func (st *unmarshalState) unmarshal(na ipld.NodeAssembler, tokSrc shared.TokenSo
case
tok
.
TMapOpen
:
// dag-json has special needs: we pump a few tokens ahead to look for dag-json's "link" pattern.
// We can't actually call BeginMap until we're sure it's not gonna turn out to be a link.
gotLink
,
err
:=
st
.
linkLookahead
(
na
,
tokSrc
)
if
err
!=
nil
{
// return in error if any token peeks failed or if structure looked like a link but failed to parse as CID.
return
err
}
if
gotLink
{
return
nil
if
st
.
parseLinks
{
gotLink
,
err
:=
st
.
linkLookahead
(
na
,
tokSrc
)
if
err
!=
nil
{
// return in error if any token peeks failed or if structure looked like a link but failed to parse as CID.
return
err
}
if
gotLink
{
return
nil
}
}
// Okay, now back to regularly scheduled map logic.
...
...
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