missing unmarshalling of content bytes before encoding and generation… #86
@ -7,18 +7,16 @@ package utils
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/ipld/go-ipld-prime/codec/dagcbor"
|
|
||||||
"github.com/ipld/go-ipld-prime/fluent"
|
|
||||||
"github.com/ipld/go-ipld-prime/linking"
|
|
||||||
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
|
|
||||||
"github.com/ipld/go-ipld-prime/multicodec"
|
|
||||||
"github.com/ipld/go-ipld-prime/storage/memstore"
|
|
||||||
|
|
||||||
canonicalJson "github.com/gibson042/canonicaljson-go"
|
canonicalJson "github.com/gibson042/canonicaljson-go"
|
||||||
"github.com/ipfs/go-cid"
|
"github.com/ipfs/go-cid"
|
||||||
cbor "github.com/ipfs/go-ipld-cbor"
|
cbor "github.com/ipfs/go-ipld-cbor"
|
||||||
|
"github.com/ipld/go-ipld-prime/codec/dagcbor"
|
||||||
|
"github.com/ipld/go-ipld-prime/codec/dagjson"
|
||||||
|
"github.com/ipld/go-ipld-prime/linking"
|
||||||
|
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
|
||||||
|
"github.com/ipld/go-ipld-prime/multicodec"
|
||||||
basicnode "github.com/ipld/go-ipld-prime/node/basic"
|
basicnode "github.com/ipld/go-ipld-prime/node/basic"
|
||||||
|
"github.com/ipld/go-ipld-prime/storage/memstore"
|
||||||
mh "github.com/multiformats/go-multihash"
|
mh "github.com/multiformats/go-multihash"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -69,6 +67,14 @@ func GetAttributeAsString(obj map[string]interface{}, attr string) (string, erro
|
|||||||
|
|
||||||
// CIDFromJSONBytesUsingIpldPrime returns CID (dagcbor) for json (as bytes).
|
// CIDFromJSONBytesUsingIpldPrime returns CID (dagcbor) for json (as bytes).
|
||||||
func CIDFromJSONBytesUsingIpldPrime(content []byte) (string, error) {
|
func CIDFromJSONBytesUsingIpldPrime(content []byte) (string, error) {
|
||||||
|
|
||||||
|
//This is combination of samples for unmarshalling and linking
|
||||||
|
//see: https://pkg.go.dev/github.com/ipld/go-ipld-prime
|
||||||
|
np := basicnode.Prototype.Any // Pick a stle for the in-memory data.
|
||||||
|
nb := np.NewBuilder() // Create a builder.
|
||||||
|
dagjson.Decode(nb, bytes.NewReader(content)) // Hand the builder to decoding -- decoding will fill it in!
|
||||||
|
n := nb.Build() // Call 'Build' to get the resulting Node. (It's immutable!)
|
||||||
|
|
||||||
lsys := cidlink.DefaultLinkSystem()
|
lsys := cidlink.DefaultLinkSystem()
|
||||||
|
|
||||||
// We want to store the serialized data somewhere.
|
// We want to store the serialized data somewhere.
|
||||||
@ -87,14 +93,6 @@ func CIDFromJSONBytesUsingIpldPrime(content []byte) (string, error) {
|
|||||||
MhLength: 32, // sha2-256 hash has a 32-byte sum.
|
MhLength: 32, // sha2-256 hash has a 32-byte sum.
|
||||||
}}
|
}}
|
||||||
|
|
||||||
// And we need some data to link to! Here's a quick piece of example data:
|
|
||||||
n, err := fluent.Build(basicnode.Prototype.Any, func(na fluent.NodeAssembler) {
|
|
||||||
na.AssignBytes(content)
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now: time to apply the LinkSystem, and do the actual store operation!
|
// Now: time to apply the LinkSystem, and do the actual store operation!
|
||||||
lnk, err := lsys.Store(
|
lnk, err := lsys.Store(
|
||||||
linking.LinkContext{}, // The zero value is fine. Configure it it you want cancellability or other features.
|
linking.LinkContext{}, // The zero value is fine. Configure it it you want cancellability or other features.
|
||||||
@ -104,5 +102,6 @@ func CIDFromJSONBytesUsingIpldPrime(content []byte) (string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|||||||
|
//return cborcid.String(), nil
|
||||||
return lnk.String(), nil
|
return lnk.String(), nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user
Let's remove this left over comment