From 7db19ee56d0fa46db62eeffdfcf28c39dc89c659 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Thu, 19 Jan 2023 16:23:10 -0500 Subject: [PATCH] missing unmarshalling of content bytes before encoding and generation of CID --- utils/json.go | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/utils/json.go b/utils/json.go index 8adc6a9f..2c47181f 100644 --- a/utils/json.go +++ b/utils/json.go @@ -7,18 +7,16 @@ package utils import ( "bytes" "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" "github.com/ipfs/go-cid" 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" + "github.com/ipld/go-ipld-prime/storage/memstore" 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). 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() // 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. }} - // 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! lnk, err := lsys.Store( 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 { return "", err } + //return cborcid.String(), nil return lnk.String(), nil }