Compare commits

...

4 Commits

Author SHA1 Message Date
Michael Shaw
ac6a5602ad utils/json test for comparing known, but deprecated method to new implementation 2023-01-25 15:05:48 -05:00
Michael Shaw
7de916117d golang linting is really picky 2023-01-19 17:36:16 -05:00
Michael Shaw
44ab3f6fc0 unchecked error complaint from linter 2023-01-19 17:30:47 -05:00
Michael Shaw
7db19ee56d missing unmarshalling of content bytes before encoding and generation of CID 2023-01-19 16:23:42 -05:00
2 changed files with 46 additions and 15 deletions

View File

@ -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
View 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)
}
}