missing unmarshalling of content bytes before encoding and generation… #86
@ -8,17 +8,16 @@ 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"
|
||||
)
|
||||
|
||||
@ -68,7 +67,17 @@ func GetAttributeAsString(obj map[string]interface{}, attr string) (string, erro
|
||||
}
|
||||
|
||||
// CIDFromJSONBytesUsingIpldPrime returns CID (dagcbor) for json (as bytes).
|
||||
// This is combination of samples for unmarshalling and linking
|
||||
// see: https://pkg.go.dev/github.com/ipld/go-ipld-prime
|
||||
func CIDFromJSONBytesUsingIpldPrime(content []byte) (string, error) {
|
||||
np := basicnode.Prototype.Any // Pick a stle for the in-memory data.
|
||||
nb := np.NewBuilder() // Create a builder.
|
||||
err := dagjson.Decode(nb, bytes.NewReader(content)) // Hand the builder to decoding -- decoding will fill it in!
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
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 +96,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.
|
||||
|
30
utils/json_test.go
Normal file
30
utils/json_test.go
Normal file
@ -0,0 +1,30 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAndValidateCIDGeneration(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
content string
|
||||
}{
|
||||
{
|
||||
"empty string", "",
|
||||
},
|
||||
{
|
||||
"empty json", "{}",
|
||||
},
|
||||
|
||||
{
|
||||
"test record", "\\xa6curlohttps://cerc.iodtypex\\x19WebsiteRegistrationRecordgversione0.0.1ltls_cert_cidx.QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnRrbuild_artifact_cidx.QmP8jTG1m9GSDJLCbeWhVSVgEzCPPwXRdCRuJtQ5Tz9Kc9x\\x1crepo_registration_record_cidx.QmSnuWmxptJZdLJpKRarxBMS2Ju2oANVrgbr2xWbie9b2D",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
deprecatedAndCorrect, _ := CIDFromJSONBytes([]byte(tc.content))
|
||||
|
||||
newImpl, _ := CIDFromJSONBytesUsingIpldPrime([]byte(tc.content))
|
||||
require.Equal(t, deprecatedAndCorrect, newImpl, tc.name)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user
We should either catch any errors returned by these functions or make sure the returned result isn't
nil
/empty, because if both functions were to error out and return anil
/empty result it would evaluate them as equal and the test would pass.