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
8b59dc29
Commit
8b59dc29
authored
Apr 28, 2020
by
Eric Myhre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add two basic examples of usage, as go tests.
parent
bd402871
Pipeline
#783
failed with stages
in 0 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
0 deletions
+47
-0
examples_test.go
examples_test.go
+47
-0
No files found.
examples_test.go
0 → 100644
View file @
8b59dc29
package
ipld_test
import
(
"bytes"
"fmt"
"os"
"github.com/ipld/go-ipld-prime/codec/dagjson"
"github.com/ipld/go-ipld-prime/node/basic"
)
func
ExampleCreateDataAndMarshal
()
{
ns
:=
basicnode
.
Style
.
Any
// Pick a style for the in-memory data.
nb
:=
ns
.
NewBuilder
()
// Create a builder.
ma
,
_
:=
nb
.
BeginMap
(
2
)
// Begin assembling a map.
ma
.
AssembleKey
()
.
AssignString
(
"hey"
)
ma
.
AssembleValue
()
.
AssignString
(
"it works!"
)
ma
.
AssembleKey
()
.
AssignString
(
"yes"
)
ma
.
AssembleValue
()
.
AssignBool
(
true
)
ma
.
Finish
()
// Call 'Finish' on the map assembly to let it know no more data is coming.
n
:=
nb
.
Build
()
// Call 'Build' to get the resulting Node. (It's immutable!)
dagjson
.
Encoder
(
n
,
os
.
Stdout
)
// Output:
// {
// "hey": "it works!",
// "yes": true
// }
}
func
ExampleUnmarshalData
()
{
serial
:=
bytes
.
NewBufferString
(
`{"hey":"it works!","yes": true}`
)
ns
:=
basicnode
.
Style
.
Any
// Pick a stle for the in-memory data.
nb
:=
ns
.
NewBuilder
()
// Create a builder.
dagjson
.
Decoder
(
nb
,
serial
)
// Hand the builder to decoding -- decoding will fill it in!
n
:=
nb
.
Build
()
// Call 'Build' to get the resulting Node. (It's immutable!)
fmt
.
Printf
(
"the data decoded was a %s kind
\n
"
,
n
.
ReprKind
())
fmt
.
Printf
(
"the length of the node is %d
\n
"
,
n
.
Length
())
// Output:
// the data decoded was a Map kind
// the length of the node is 2
}
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