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
5ab5068c
Unverified
Commit
5ab5068c
authored
Mar 22, 2021
by
Will Scott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
json as separate codec
parent
6c9c90d1
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
68 additions
and
21 deletions
+68
-21
adl/rot13adl/example_test.go
adl/rot13adl/example_test.go
+1
-1
codec/dagjson/multicodec.go
codec/dagjson/multicodec.go
+1
-13
codec/dagjson/unmarshal.go
codec/dagjson/unmarshal.go
+1
-5
codec/json/multicodec.go
codec/json/multicodec.go
+63
-0
schema/gen/go/testcase_test.go
schema/gen/go/testcase_test.go
+1
-1
schema/schema2/typesystem_test.go
schema/schema2/typesystem_test.go
+1
-1
No files found.
adl/rot13adl/example_test.go
View file @
5ab5068c
...
...
@@ -20,7 +20,7 @@ func ExampleUnmarshallingToADL() {
nb
:=
rot13adl
.
Prototype
.
SubstrateRoot
.
NewBuilder
()
// Unmarshal -- using the substrate's nodebuilder just like you'd unmarshal with any other nodebuilder.
err
:=
dagjson
.
Unmarshal
(
nb
,
json
.
NewDecoder
(
strings
.
NewReader
(
`"n pbby fgevat"`
)))
err
:=
dagjson
.
Unmarshal
(
nb
,
json
.
NewDecoder
(
strings
.
NewReader
(
`"n pbby fgevat"`
))
,
true
)
fmt
.
Printf
(
"unmarshal error: %v
\n
"
,
err
)
// Use `Reify` to get the synthetic high-level view of the ADL data.
...
...
codec/dagjson/multicodec.go
View file @
5ab5068c
...
...
@@ -17,23 +17,11 @@ 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
:=
unmarshal
(
na
,
json
.
NewDecoder
(
r
),
parseLinks
)
err
:=
Unmarshal
(
na
,
json
.
NewDecoder
(
r
),
true
)
if
err
!=
nil
{
return
err
}
...
...
codec/dagjson/unmarshal.go
View file @
5ab5068c
...
...
@@ -19,11 +19,7 @@ import (
// several steps of handling maps, because it necessitates peeking several
// 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
{
func
Unmarshal
(
na
ipld
.
NodeAssembler
,
tokSrc
shared
.
TokenSource
,
parseLinks
bool
)
error
{
var
st
unmarshalState
st
.
parseLinks
=
parseLinks
done
,
err
:=
tokSrc
.
Step
(
&
st
.
tk
[
0
])
...
...
codec/json/multicodec.go
0 → 100644
View file @
5ab5068c
package
json
import
(
"fmt"
"io"
rfmtjson
"github.com/polydawn/refmt/json"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagjson"
"github.com/ipld/go-ipld-prime/multicodec"
)
var
(
_
ipld
.
Decoder
=
Decode
_
ipld
.
Encoder
=
Encode
)
func
init
()
{
multicodec
.
RegisterEncoder
(
0x0200
,
Encode
)
multicodec
.
RegisterDecoder
(
0x0200
,
Decode
)
}
func
Decode
(
na
ipld
.
NodeAssembler
,
r
io
.
Reader
)
error
{
// Shell out directly to generic builder path.
// (There's not really any fastpaths of note for json.)
err
:=
dagjson
.
Unmarshal
(
na
,
rfmtjson
.
NewDecoder
(
r
),
false
)
if
err
!=
nil
{
return
err
}
// Slurp any remaining whitespace.
// (This is relevant if our reader is tee'ing bytes to a hasher, and
// the json contained any trailing whitespace.)
// (We can't actually support multiple objects per reader from here;
// we can't unpeek if we find a non-whitespace token, so our only
// option is to error if this reader seems to contain more content.)
var
buf
[
1
]
byte
for
{
_
,
err
:=
r
.
Read
(
buf
[
:
])
switch
buf
[
0
]
{
case
' '
,
0x0
,
'\t'
,
'\r'
,
'\n'
:
// continue
default
:
return
fmt
.
Errorf
(
"unexpected content after end of json object"
)
}
if
err
==
nil
{
continue
}
else
if
err
==
io
.
EOF
{
return
nil
}
else
{
return
err
}
}
}
func
Encode
(
n
ipld
.
Node
,
w
io
.
Writer
)
error
{
// Shell out directly to generic inspection path.
// (There's not really any fastpaths of note for json.)
// Write another function if you need to tune encoding options about whitespace.
return
dagjson
.
Marshal
(
n
,
rfmtjson
.
NewEncoder
(
w
,
rfmtjson
.
EncodeOptions
{
Line
:
[]
byte
{
'\n'
},
Indent
:
[]
byte
{
'\t'
},
}))
}
schema/gen/go/testcase_test.go
View file @
5ab5068c
...
...
@@ -190,7 +190,7 @@ func (tcase testcase) Test(t *testing.T, np, npr ipld.NodePrototype) {
func
testUnmarshal
(
t
*
testing
.
T
,
np
ipld
.
NodePrototype
,
data
string
,
expectFail
error
)
ipld
.
Node
{
t
.
Helper
()
nb
:=
np
.
NewBuilder
()
err
:=
dagjson
.
Unmarshal
(
nb
,
json
.
NewDecoder
(
strings
.
NewReader
(
data
)))
err
:=
dagjson
.
Unmarshal
(
nb
,
json
.
NewDecoder
(
strings
.
NewReader
(
data
))
,
true
)
switch
{
case
expectFail
==
nil
&&
err
!=
nil
:
t
.
Fatalf
(
"fixture parse failed: %s"
,
err
)
...
...
schema/schema2/typesystem_test.go
View file @
5ab5068c
...
...
@@ -206,7 +206,7 @@ func testParse(t *testing.T, schemajson string, expectParseErr error, expectType
func
parseSchema
(
schemajson
string
)
(
schemadmt
.
Schema
,
error
)
{
nb
:=
schemadmt
.
Type
.
Schema__Repr
.
NewBuilder
()
if
err
:=
dagjson
.
Unmarshal
(
nb
,
json
.
NewDecoder
(
strings
.
NewReader
(
schemajson
)));
err
!=
nil
{
if
err
:=
dagjson
.
Unmarshal
(
nb
,
json
.
NewDecoder
(
strings
.
NewReader
(
schemajson
))
,
true
);
err
!=
nil
{
return
nil
,
err
}
return
nb
.
Build
()
.
(
schemadmt
.
Schema
),
nil
...
...
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