Unverified Commit d721d5e5 authored by Rod Vagg's avatar Rod Vagg

full compat tests, upgrade ipld-prime

parent e12d796b
......@@ -7,10 +7,16 @@ import (
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/fluent"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
pb "github.com/rvagg/go-dagpb/pb"
)
type pbNode struct {
links []pbLink
data []byte
}
func mkcid(t *testing.T, cidStr string) cid.Cid {
c, err := cid.Decode(cidStr)
if err != nil {
......@@ -19,7 +25,7 @@ func mkcid(t *testing.T, cidStr string) cid.Cid {
return c
}
func validate(t *testing.T, actual ipld.Node, expected *pb.PBNode) {
func validate(t *testing.T, actual ipld.Node, expected pbNode) {
mi := actual.MapIterator()
_, isTyped := actual.(*_PBNode)
......@@ -44,12 +50,12 @@ func validate(t *testing.T, actual ipld.Node, expected *pb.PBNode) {
if val.IsNull() {
t.Fatal("Links is null")
}
if val.Length() != len(expected.Links) {
if val.Length() != len(expected.links) {
t.Fatal("non-empty Links list")
}
hasLinks = true
} else if keyStr == "Data" {
if isTyped && expected.Data == nil {
if isTyped && expected.data == nil {
if !val.IsAbsent() {
t.Fatalf("Empty Data is not marked as absent")
}
......@@ -62,13 +68,13 @@ func validate(t *testing.T, actual ipld.Node, expected *pb.PBNode) {
}
hasData = !isTyped || !val.IsAbsent()
if hasData {
if expected.Data == nil {
if expected.data == nil {
t.Fatal("Got unexpected Data")
} else {
byts, err := val.AsBytes()
if err != nil {
t.Fatal(err)
} else if bytes.Compare(expected.Data, byts) != 0 {
} else if bytes.Compare(expected.data, byts) != 0 {
t.Fatal("Got unexpected Data contents")
}
}
......@@ -80,12 +86,12 @@ func validate(t *testing.T, actual ipld.Node, expected *pb.PBNode) {
if !hasLinks {
t.Fatal("Did not find Links")
}
if expected.Data != nil && !hasData {
if expected.data != nil && !hasData {
t.Fatal("Did not find Data")
}
}
func runTest(t *testing.T, bytsHex string, expected *pb.PBNode) {
func runTest(t *testing.T, bytsHex string, expected pbNode) {
byts, _ := hex.DecodeString(bytsHex)
roundTrip := func(t *testing.T, node ipld.Node) {
......@@ -125,51 +131,74 @@ func runTest(t *testing.T, bytsHex string, expected *pb.PBNode) {
}
func TestEmptyNode(t *testing.T) {
runTest(t, "", pb.NewPBNode())
runTest(t, "", pbNode{})
}
func TestNodeWithData(t *testing.T) {
runTest(t, "0a050001020304", pb.NewPBNodeFromData([]byte{00, 01, 02, 03, 04}))
runTest(t, "0a050001020304", pbNode{data: []byte{00, 01, 02, 03, 04}})
}
func TestNodeWithDataZero(t *testing.T) {
runTest(t, "0a00", pb.NewPBNodeFromData([]byte{}))
runTest(t, "0a00", pbNode{data: []byte{}})
}
func TestNodeWithLink(t *testing.T) {
expected := pb.NewPBNode()
expected.Links = append(expected.Links, pb.NewPBLinkFromCid(mkcid(t, "QmWDtUQj38YLW8v3q4A6LwPn4vYKEbuKWpgSm6bjKW6Xfe")))
expected := pbNode{}
expected.links = append(expected.links, pbLink{hash: mkcid(t, "QmWDtUQj38YLW8v3q4A6LwPn4vYKEbuKWpgSm6bjKW6Xfe")})
runTest(t, "12240a2212207521fe19c374a97759226dc5c0c8e674e73950e81b211f7dd3b6b30883a08a51", expected)
}
func TestNodeWithLinkAndData(t *testing.T) {
expected := pb.NewPBNodeFromData([]byte("some data"))
expected.Links = append(expected.Links, pb.NewPBLinkFromCid(mkcid(t, "QmWDtUQj38YLW8v3q4A6LwPn4vYKEbuKWpgSm6bjKW6Xfe")))
expected := pbNode{data: []byte("some data")}
expected.links = append(expected.links, pbLink{hash: mkcid(t, "QmWDtUQj38YLW8v3q4A6LwPn4vYKEbuKWpgSm6bjKW6Xfe")})
runTest(t, "12240a2212207521fe19c374a97759226dc5c0c8e674e73950e81b211f7dd3b6b30883a08a510a09736f6d652064617461", expected)
}
func TestNodeWithTwoUnsortedLinks(t *testing.T) {
expected := pb.NewPBNodeFromData([]byte("some data"))
expected.Links = append(expected.Links, pb.NewPBLink("some link", mkcid(t, "QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39U"), 100000000))
expected.Links = append(expected.Links, pb.NewPBLink("some other link", mkcid(t, "QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V"), 8))
encoded := "12340a2212208ab7a6c5e74737878ac73863cb76739d15d4666de44e5756bf55a2f9e9ab5f431209736f6d65206c696e6b1880c2d72f12370a2212208ab7a6c5e74737878ac73863cb76739d15d4666de44e5756bf55a2f9e9ab5f44120f736f6d65206f74686572206c696e6b18080a09736f6d652064617461"
expected := pbNode{data: []byte("some data")}
expected.links = append(expected.links, pbLink{name: "some link", hasName: true, hash: mkcid(t, "QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39U"), tsize: 100000000, hasTsize: true})
expected.links = append(expected.links, pbLink{name: "some other link", hasName: true, hash: mkcid(t, "QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V"), tsize: 8, hasTsize: true})
runTest(t, encoded, expected)
// assembled in a rough order, Data coming first, links badly sorted
node := fluent.MustBuildMap(basicnode.Prototype__Map{}, 2, func(fma fluent.MapAssembler) {
fma.AssembleEntry("Data").AssignBytes([]byte("some data"))
fma.AssembleEntry("Links").CreateList(2, func(fla fluent.ListAssembler) {
fla.AssembleValue().CreateMap(3, func(fma fluent.MapAssembler) {
fma.AssembleEntry("Name").AssignString("some other link")
fma.AssembleEntry("Tsize").AssignInt(8)
fma.AssembleEntry("Hash").AssignLink(cidlink.Link{Cid: mkcid(t, "QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V")})
})
fla.AssembleValue().CreateMap(3, func(fma fluent.MapAssembler) {
fma.AssembleEntry("Name").AssignString("some link")
fma.AssembleEntry("Tsize").AssignInt(100000000)
fma.AssembleEntry("Hash").AssignLink(cidlink.Link{Cid: mkcid(t, "QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39U")})
})
})
})
runTest(
t,
"12340a2212208ab7a6c5e74737878ac73863cb76739d15d4666de44e5756bf55a2f9e9ab5f431209736f6d65206c696e6b1880c2d72f12370a2212208ab7a6c5e74737878ac73863cb76739d15d4666de44e5756bf55a2f9e9ab5f44120f736f6d65206f74686572206c696e6b18080a09736f6d652064617461",
expected)
var buf bytes.Buffer
if err := Marshal(node, &buf); err != nil {
t.Fatal(err)
}
if hex.EncodeToString(buf.Bytes()) != encoded {
t.Fatal("did not encode to expected bytes")
}
}
func TestNodeWithUnnamedLinks(t *testing.T) {
dataByts, _ := hex.DecodeString("080218cbc1819201208080e015208080e015208080e015208080e015208080e015208080e01520cbc1c10f")
expected := pb.NewPBNodeFromData(dataByts)
expected.Links = []*pb.PBLink{
pb.NewPBLink("", mkcid(t, "QmSbCgdsX12C4KDw3PDmpBN9iCzS87a5DjgSCoW9esqzXk"), 45623854),
pb.NewPBLink("", mkcid(t, "Qma4GxWNhywSvWFzPKtEswPGqeZ9mLs2Kt76JuBq9g3fi2"), 45623854),
pb.NewPBLink("", mkcid(t, "QmQfyxyys7a1e3mpz9XsntSsTGc8VgpjPj5BF1a1CGdGNc"), 45623854),
pb.NewPBLink("", mkcid(t, "QmSh2wTTZT4N8fuSeCFw7wterzdqbE93j1XDhfN3vQHzDV"), 45623854),
pb.NewPBLink("", mkcid(t, "QmVXsSVjwxMsCwKRCUxEkGb4f4B98gXVy3ih3v4otvcURK"), 45623854),
pb.NewPBLink("", mkcid(t, "QmZjhH97MEYwQXzCqSQbdjGDhXWuwW4RyikR24pNqytWLj"), 45623854),
pb.NewPBLink("", mkcid(t, "QmRs6U5YirCqC7taTynz3x2GNaHJZ3jDvMVAzaiXppwmNJ"), 32538395),
expected := pbNode{data: dataByts}
expected.links = []pbLink{
{name: "", hasName: true, hash: mkcid(t, "QmSbCgdsX12C4KDw3PDmpBN9iCzS87a5DjgSCoW9esqzXk"), tsize: 45623854, hasTsize: true},
{name: "", hasName: true, hash: mkcid(t, "Qma4GxWNhywSvWFzPKtEswPGqeZ9mLs2Kt76JuBq9g3fi2"), tsize: 45623854, hasTsize: true},
{name: "", hasName: true, hash: mkcid(t, "QmQfyxyys7a1e3mpz9XsntSsTGc8VgpjPj5BF1a1CGdGNc"), tsize: 45623854, hasTsize: true},
{name: "", hasName: true, hash: mkcid(t, "QmSh2wTTZT4N8fuSeCFw7wterzdqbE93j1XDhfN3vQHzDV"), tsize: 45623854, hasTsize: true},
{name: "", hasName: true, hash: mkcid(t, "QmVXsSVjwxMsCwKRCUxEkGb4f4B98gXVy3ih3v4otvcURK"), tsize: 45623854, hasTsize: true},
{name: "", hasName: true, hash: mkcid(t, "QmZjhH97MEYwQXzCqSQbdjGDhXWuwW4RyikR24pNqytWLj"), tsize: 45623854, hasTsize: true},
{name: "", hasName: true, hash: mkcid(t, "QmRs6U5YirCqC7taTynz3x2GNaHJZ3jDvMVAzaiXppwmNJ"), tsize: 32538395, hasTsize: true},
}
runTest(
......@@ -180,12 +209,12 @@ func TestNodeWithUnnamedLinks(t *testing.T) {
func TestNodeWithNamedLinks(t *testing.T) {
dataByts, _ := hex.DecodeString("0801")
expected := pb.NewPBNodeFromData(dataByts)
expected.Links = []*pb.PBLink{
pb.NewPBLink("audio_only.m4a", mkcid(t, "QmaUAwAQJNtvUdJB42qNbTTgDpzPYD1qdsKNtctM5i7DGB"), 23319629),
pb.NewPBLink("chat.txt", mkcid(t, "QmNVrxbB25cKTRuKg2DuhUmBVEK9NmCwWEHtsHPV6YutHw"), 996),
pb.NewPBLink("playback.m3u", mkcid(t, "QmUcjKzDLXBPmB6BKHeKSh6ZoFZjss4XDhMRdLYRVuvVfu"), 116),
pb.NewPBLink("zoom_0.mp4", mkcid(t, "QmQqy2SiEkKgr2cw5UbQ93TtLKEMsD8TdcWggR8q9JabjX"), 306281879),
expected := pbNode{data: dataByts}
expected.links = []pbLink{
{name: "audio_only.m4a", hasName: true, hash: mkcid(t, "QmaUAwAQJNtvUdJB42qNbTTgDpzPYD1qdsKNtctM5i7DGB"), tsize: 23319629, hasTsize: true},
{name: "chat.txt", hasName: true, hash: mkcid(t, "QmNVrxbB25cKTRuKg2DuhUmBVEK9NmCwWEHtsHPV6YutHw"), tsize: 996, hasTsize: true},
{name: "playback.m3u", hasName: true, hash: mkcid(t, "QmUcjKzDLXBPmB6BKHeKSh6ZoFZjss4XDhMRdLYRVuvVfu"), tsize: 116, hasTsize: true},
{name: "zoom_0.mp4", hasName: true, hash: mkcid(t, "QmQqy2SiEkKgr2cw5UbQ93TtLKEMsD8TdcWggR8q9JabjX"), tsize: 306281879, hasTsize: true},
}
runTest(
......
package dagpb
// mirrored in JavaScript @ https://github.com/ipld/js-dag-pb/blob/master/test/test-compat.js
import (
"bytes"
"encoding/hex"
"encoding/json"
"strings"
"testing"
cid "github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/fluent"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
)
var dataZero []byte = make([]byte, 0)
var dataSome []byte = []byte{0, 1, 2, 3, 4}
var acid cid.Cid = _mkcid()
var zeroName string = ""
var someName string = "some name"
var zeroTsize uint64 = 0
var someTsize uint64 = 1010
var largeTsize uint64 = 9007199254740991 // JavaScript Number.MAX_SAFE_INTEGER
type testCase struct {
name string
node *pbNode
expectedBytes string
expectedForm string
encodeError string
decodeError string
}
var testCases = []testCase{
{
name: "empty",
node: &pbNode{},
expectedBytes: "",
expectedForm: `{
"Links": []
}`,
encodeError: "missing required fields: Links",
},
{
name: "Data zero",
node: &pbNode{data: dataZero},
expectedBytes: "0a00",
expectedForm: `{
"Data": "",
"Links": []
}`,
encodeError: "missing required fields: Links",
},
{
name: "Data some",
node: &pbNode{data: dataSome},
expectedBytes: "0a050001020304",
expectedForm: `{
"Data": "0001020304",
"Links": []
}`,
encodeError: "missing required fields: Links",
},
{
name: "Links zero",
node: &pbNode{links: []pbLink{}},
expectedBytes: "",
expectedForm: `{
"Links": []
}`,
},
{
name: "Data some Links zero",
node: &pbNode{data: dataSome, links: []pbLink{}},
expectedBytes: "0a050001020304",
expectedForm: `{
"Data": "0001020304",
"Links": []
}`,
},
{
name: "Links empty",
node: &pbNode{links: []pbLink{{}}},
expectedBytes: "1200",
encodeError: "missing required fields: Hash",
decodeError: "expected CID",
},
{
name: "Data some Links empty",
node: &pbNode{data: dataSome, links: []pbLink{{}}},
expectedBytes: "12000a050001020304",
encodeError: "missing required fields: Hash",
decodeError: "expected CID",
},
{
name: "Links Hash zero",
expectedBytes: "12020a00",
decodeError: "expected CID", // error should come up from go-cid too
},
{
name: "Links Hash some",
node: &pbNode{links: []pbLink{{hash: acid}}},
expectedBytes: "120b0a09015500050001020304",
expectedForm: `{
"Links": [
{
"Hash": "015500050001020304"
}
]
}`,
},
{
name: "Links Name zero",
node: &pbNode{links: []pbLink{{name: zeroName, hasName: true}}},
expectedBytes: "12021200",
encodeError: "missing required fields: Hash",
decodeError: "expected CID",
},
{
name: "Links Hash some Name zero",
node: &pbNode{links: []pbLink{{hash: acid, name: zeroName, hasName: true}}},
expectedBytes: "120d0a090155000500010203041200",
expectedForm: `{
"Links": [
{
"Hash": "015500050001020304",
"Name": ""
}
]
}`,
},
{
name: "Links Name some",
node: &pbNode{links: []pbLink{{name: someName, hasName: true}}},
expectedBytes: "120b1209736f6d65206e616d65",
encodeError: "missing required fields: Hash",
decodeError: "expected CID",
},
{
name: "Links Hash some Name some",
node: &pbNode{links: []pbLink{{hash: acid, name: someName, hasName: true}}},
expectedBytes: "12160a090155000500010203041209736f6d65206e616d65",
expectedForm: `{
"Links": [
{
"Hash": "015500050001020304",
"Name": "some name"
}
]
}`,
},
{
name: "Links Tsize zero",
node: &pbNode{links: []pbLink{{tsize: zeroTsize, hasTsize: true}}},
expectedBytes: "12021800",
encodeError: "missing required fields: Hash",
decodeError: "expected CID",
},
{
name: "Links Hash some Tsize zero",
node: &pbNode{links: []pbLink{{hash: acid, tsize: zeroTsize, hasTsize: true}}},
expectedBytes: "120d0a090155000500010203041800",
expectedForm: `{
"Links": [
{
"Hash": "015500050001020304",
"Tsize": 0
}
]
}`,
},
{
name: "Links Tsize some",
node: &pbNode{links: []pbLink{{tsize: someTsize, hasTsize: true}}},
expectedBytes: "120318f207",
encodeError: "missing required fields: Hash",
decodeError: "expected CID",
},
{
name: "Links Hash some Tsize some",
node: &pbNode{links: []pbLink{{hash: acid, tsize: largeTsize, hasTsize: true}}},
expectedBytes: "12140a0901550005000102030418ffffffffffffff0f",
expectedForm: `{
"Links": [
{
"Hash": "015500050001020304",
"Tsize": 9007199254740991
}
]
}`,
},
}
func TestCompat(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
verifyRoundTrip(t, tc)
})
}
}
func verifyRoundTrip(t *testing.T, tc testCase) {
var err error
var actualBytes string
var actualForm string
if tc.node != nil {
node := buildNode(*tc.node)
actualBytes, err = nodeToString(t, node)
if tc.encodeError != "" {
if err != nil {
if !strings.Contains(err.Error(), tc.encodeError) {
t.Fatalf("got unexpeced encode error: [%v] (expected [%v])", err.Error(), tc.encodeError)
}
} else {
t.Fatalf("did not get expected encode error: %v", tc.encodeError)
}
} else {
if err != nil {
t.Fatal(err)
} else {
if actualBytes != tc.expectedBytes {
t.Logf(
"Expected bytes: [%v]\nGot: [%v]\n",
tc.expectedBytes,
actualBytes)
t.Error("Did not match")
}
}
}
}
actualForm, err = bytesToFormString(t, tc.expectedBytes, basicnode.Prototype__Map{}.NewBuilder())
if tc.decodeError != "" {
if err != nil {
if !strings.Contains(err.Error(), tc.decodeError) {
t.Fatalf("got unexpeced decode error: [%v] (expected [%v])", err.Error(), tc.decodeError)
}
} else {
t.Fatalf("did not get expected decode error: %v", tc.decodeError)
}
} else {
if err != nil {
t.Fatal(err)
}
if actualForm != tc.expectedForm {
t.Logf(
"Expected form: [%v]\nGot: [%v]\n",
tc.expectedForm,
actualForm)
t.Error("Did not match")
}
}
}
func buildNode(n pbNode) ipld.Node {
return fluent.MustBuildMap(basicnode.Prototype__Map{}, 2, func(fma fluent.MapAssembler) {
if n.data != nil {
fma.AssembleEntry("Data").AssignBytes(n.data)
}
if n.links != nil {
fma.AssembleEntry("Links").CreateList(len(n.links), func(fla fluent.ListAssembler) {
for _, link := range n.links {
fla.AssembleValue().CreateMap(3, func(fma fluent.MapAssembler) {
if link.hasName {
fma.AssembleEntry("Name").AssignString(link.name)
}
if link.hasTsize {
fma.AssembleEntry("Tsize").AssignInt(int(link.tsize))
}
if link.hash.ByteLen() != 0 {
fma.AssembleEntry("Hash").AssignLink(cidlink.Link{Cid: link.hash})
}
})
}
})
}
})
}
func nodeToString(t *testing.T, node ipld.Node) (string, error) {
var buf bytes.Buffer
err := Marshal(node, &buf)
if err != nil {
return "", err
}
h := hex.EncodeToString(buf.Bytes())
t.Logf("[%v]\n", h)
return h, nil
}
func bytesToFormString(t *testing.T, bytesHex string, nb ipld.NodeBuilder) (string, error) {
byts, err := hex.DecodeString(bytesHex)
if err != nil {
return "", err
}
if err = Unmarshal(nb, bytes.NewReader(byts)); err != nil {
return "", err
}
node := nb.Build()
str, err := json.MarshalIndent(cleanPBNode(t, node), "", "\t")
if err != nil {
return "", err
}
return string(str), nil
}
// convert a ipld.Node (PBLink) into a map for clean JSON marshalling
func cleanPBLink(t *testing.T, link ipld.Node) map[string]interface{} {
if link == nil {
return nil
}
nl := make(map[string]interface{})
hash, _ := link.LookupByString("Hash")
if hash != nil {
l, _ := hash.AsLink()
cl, _ := l.(cidlink.Link)
nl["Hash"] = hex.EncodeToString(cl.Bytes())
}
name, _ := link.LookupByString("Name")
if name != nil {
name, _ := name.AsString()
nl["Name"] = name
}
tsize, _ := link.LookupByString("Tsize")
if tsize != nil {
tsize, _ := tsize.AsInt()
nl["Tsize"] = tsize
}
return nl
}
// convert an ipld.Node (PBNode) into a map for clean JSON marshalling
func cleanPBNode(t *testing.T, node ipld.Node) map[string]interface{} {
nn := make(map[string]interface{})
data, _ := node.LookupByString("Data")
if data != nil {
byts, _ := data.AsBytes()
nn["Data"] = hex.EncodeToString(byts)
}
links, _ := node.LookupByString("Links")
if links != nil {
linksList := make([]map[string]interface{}, links.Length())
linksIter := links.ListIterator()
for !linksIter.Done() {
ii, link, _ := linksIter.Next()
linksList[ii] = cleanPBLink(t, link)
}
nn["Links"] = linksList
}
return nn
}
func _mkcid() cid.Cid {
_, c, _ := cid.CidFromBytes([]byte{1, 85, 0, 5, 0, 1, 2, 3, 4})
return c
}
......@@ -4,13 +4,15 @@ go 1.15
require (
github.com/ipfs/go-cid v0.0.7
github.com/ipld/go-ipld-prime v0.5.1-0.20201114141345-1110155de1fb
github.com/ipld/go-ipld-prime v0.5.1-0.20201204155930-29d37db62cf8
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-multihash v0.0.14 // indirect
github.com/multiformats/go-varint v0.0.6 // indirect
github.com/polydawn/refmt v0.0.0-20190807091052-3d65705ee9f1
github.com/rvagg/go-dagpb/pb v0.0.0-00010101000000-000000000000
golang.org/x/crypto v0.0.0-20201124201722-c8d3bf9c5392 // indirect
golang.org/x/sys v0.0.0-20201202213521-69691e467435 // indirect
golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c // indirect
golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
)
replace github.com/rvagg/go-dagpb/pb => ./pb
......@@ -5,6 +5,8 @@ github.com/ipfs/go-cid v0.0.7 h1:ysQJVJA3fNDF1qigJbsSQOdjhVLsOEoPdh0+R97k3jY=
github.com/ipfs/go-cid v0.0.7/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I=
github.com/ipld/go-ipld-prime v0.5.1-0.20201114141345-1110155de1fb h1:sjIJ7bZVV4TJmfL6pCJQYoHCg7n5nkTZHgQMqpqiXHA=
github.com/ipld/go-ipld-prime v0.5.1-0.20201114141345-1110155de1fb/go.mod h1:0xEgdD6MKbZ1vF0GC+YcR/C4SQCAlRuOjIJ2i0HxqzM=
github.com/ipld/go-ipld-prime v0.5.1-0.20201204155930-29d37db62cf8 h1:YuISF8jcJBVZXJWTs2LCuXIuBIz+xYAMhWDSbvfQN7s=
github.com/ipld/go-ipld-prime v0.5.1-0.20201204155930-29d37db62cf8/go.mod h1:0xEgdD6MKbZ1vF0GC+YcR/C4SQCAlRuOjIJ2i0HxqzM=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
......@@ -50,6 +52,8 @@ golang.org/x/crypto v0.0.0-20200117160349-530e935923ad h1:Jh8cai0fqIK+f6nG0UgPW5
golang.org/x/crypto v0.0.0-20200117160349-530e935923ad/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201124201722-c8d3bf9c5392 h1:xYJJ3S178yv++9zXV/hnr29plCAGO9vAFG9dorqaFQc=
golang.org/x/crypto v0.0.0-20201124201722-c8d3bf9c5392/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c h1:9HhBz5L/UjnK9XLtiZhYAdue5BVKep3PMmS2LuPDt8k=
golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
......@@ -59,6 +63,10 @@ golang.org/x/sys v0.0.0-20200122134326-e047566fdf82 h1:ywK/j/KkyTHcdyYSZNXGjMwgm
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201202213521-69691e467435 h1:25AvDqqB9PrNqj1FLf2/70I4W0L19qqoaFq3gjNwbKk=
golang.org/x/sys v0.0.0-20201202213521-69691e467435/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d h1:MiWWjyhUzZ+jvhZvloX6ZrUsdEghn8a64Upd8EMHglE=
golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
This diff is collapsed.
package dagpb
// Code generated by go-ipld-prime gengo. DO NOT EDIT.
import (
ipld "github.com/ipld/go-ipld-prime"
)
var _ ipld.Node = nil // suppress errors when this dependency is not referenced
// Type is a struct embeding a NodePrototype/Type for every Node implementation in this package.
// One of its major uses is to start the construction of a value.
// You can use it like this:
......@@ -28,3 +35,42 @@ type typeSlab struct {
String _String__Prototype
String__Repr _String__ReprPrototype
}
// --- type definitions follow ---
// Bytes matches the IPLD Schema type "Bytes". It has bytes kind.
type Bytes = *_Bytes
type _Bytes struct{ x []byte }
// Int matches the IPLD Schema type "Int". It has int kind.
type Int = *_Int
type _Int struct{ x int }
// Link matches the IPLD Schema type "Link". It has link kind.
type Link = *_Link
type _Link struct{ x ipld.Link }
// PBLink matches the IPLD Schema type "PBLink". It has Struct type-kind, and may be interrogated like map kind.
type PBLink = *_PBLink
type _PBLink struct {
Hash _Link
Name _String__Maybe
Tsize _Int__Maybe
}
// PBLinks matches the IPLD Schema type "PBLinks". It has list kind.
type PBLinks = *_PBLinks
type _PBLinks struct {
x []_PBLink
}
// PBNode matches the IPLD Schema type "PBNode". It has Struct type-kind, and may be interrogated like map kind.
type PBNode = *_PBNode
type _PBNode struct {
Links _PBLinks
Data _Bytes__Maybe
}
// String matches the IPLD Schema type "String". It has string kind.
type String = *_String
type _String struct{ x string }
package dagpb
import (
"fmt"
"io"
math_bits "math/bits"
"sort"
......@@ -9,6 +8,7 @@ import (
"github.com/ipfs/go-cid"
ipld "github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"golang.org/x/xerrors"
)
type pbLink struct {
......@@ -47,8 +47,6 @@ func Marshal(inNode ipld.Node, out io.Writer) error {
if err != nil {
return err
}
// TODO:
// return 0, fmt.Errorf("invalid DAG-PB form (link must have a Hash)")
l, err := d.AsLink()
if err != nil {
return err
......@@ -58,7 +56,8 @@ func Marshal(inNode ipld.Node, out io.Writer) error {
}
cl, ok := l.(cidlink.Link)
if !ok {
return fmt.Errorf("unexpected Link type [%v]", l)
// this _should_ be taken care of by the Typed conversion above "missing required fields: Hash"
return xerrors.Errorf("invalid DAG-PB form (link must have a Hash)")
}
pbLinks[ii].hash = cl.Cid
}
......@@ -89,7 +88,7 @@ func Marshal(inNode ipld.Node, out io.Writer) error {
return err
}
if tsize < 0 {
return fmt.Errorf("Link has negative Tsize value [%v]", tsize)
return xerrors.Errorf("Link has negative Tsize value [%v]", tsize)
}
utsize := uint64(tsize)
pbLinks[ii].tsize = utsize
......@@ -109,7 +108,7 @@ func Marshal(inNode ipld.Node, out io.Writer) error {
return err
}
if wrote != size {
return fmt.Errorf("bad PBLink marshal, wrote wrong number of bytes")
return xerrors.Errorf("bad PBLink marshal, wrote wrong number of bytes")
}
out.Write(chunk)
}
......
package dagpb
// Code generated by go-ipld-prime gengo. DO NOT EDIT.
import (
ipld "github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/node/mixins"
"github.com/ipld/go-ipld-prime/schema"
)
type _Bytes struct{ x []byte }
type Bytes = *_Bytes
func (n Bytes) Bytes() []byte {
return n.x
}
func (_Bytes__Prototype) FromBytes(v []byte) (Bytes, error) {
n := _Bytes{v}
return &n, nil
}
type _Bytes__Maybe struct {
m schema.Maybe
v Bytes
}
type MaybeBytes = *_Bytes__Maybe
func (m MaybeBytes) IsNull() bool {
return m.m == schema.Maybe_Null
}
func (m MaybeBytes) IsAbsent() bool {
return m.m == schema.Maybe_Absent
}
func (m MaybeBytes) Exists() bool {
return m.m == schema.Maybe_Value
}
func (m MaybeBytes) AsNode() ipld.Node {
switch m.m {
case schema.Maybe_Absent:
return ipld.Absent
case schema.Maybe_Null:
return ipld.Null
case schema.Maybe_Value:
return m.v
default:
panic("unreachable")
}
}
func (m MaybeBytes) Must() Bytes {
if !m.Exists() {
panic("unbox of a maybe rejected")
}
return m.v
}
var _ ipld.Node = (Bytes)(&_Bytes{})
var _ schema.TypedNode = (Bytes)(&_Bytes{})
func (Bytes) ReprKind() ipld.ReprKind {
return ipld.ReprKind_Bytes
}
func (Bytes) LookupByString(string) (ipld.Node, error) {
return mixins.Bytes{"dagpb.Bytes"}.LookupByString("")
}
func (Bytes) LookupByNode(ipld.Node) (ipld.Node, error) {
return mixins.Bytes{"dagpb.Bytes"}.LookupByNode(nil)
}
func (Bytes) LookupByIndex(idx int) (ipld.Node, error) {
return mixins.Bytes{"dagpb.Bytes"}.LookupByIndex(0)
}
func (Bytes) LookupBySegment(seg ipld.PathSegment) (ipld.Node, error) {
return mixins.Bytes{"dagpb.Bytes"}.LookupBySegment(seg)
}
func (Bytes) MapIterator() ipld.MapIterator {
return nil
}
func (Bytes) ListIterator() ipld.ListIterator {
return nil
}
func (Bytes) Length() int {
return -1
}
func (Bytes) IsAbsent() bool {
return false
}
func (Bytes) IsNull() bool {
return false
}
func (Bytes) AsBool() (bool, error) {
return mixins.Bytes{"dagpb.Bytes"}.AsBool()
}
func (Bytes) AsInt() (int, error) {
return mixins.Bytes{"dagpb.Bytes"}.AsInt()
}
func (Bytes) AsFloat() (float64, error) {
return mixins.Bytes{"dagpb.Bytes"}.AsFloat()
}
func (Bytes) AsString() (string, error) {
return mixins.Bytes{"dagpb.Bytes"}.AsString()
}
func (n Bytes) AsBytes() ([]byte, error) {
return n.x, nil
}
func (Bytes) AsLink() (ipld.Link, error) {
return mixins.Bytes{"dagpb.Bytes"}.AsLink()
}
func (Bytes) Prototype() ipld.NodePrototype {
return _Bytes__Prototype{}
}
type _Bytes__Prototype struct{}
func (_Bytes__Prototype) NewBuilder() ipld.NodeBuilder {
var nb _Bytes__Builder
nb.Reset()
return &nb
}
type _Bytes__Builder struct {
_Bytes__Assembler
}
func (nb *_Bytes__Builder) Build() ipld.Node {
if *nb.m != schema.Maybe_Value {
panic("invalid state: cannot call Build on an assembler that's not finished")
}
return nb.w
}
func (nb *_Bytes__Builder) Reset() {
var w _Bytes
var m schema.Maybe
*nb = _Bytes__Builder{_Bytes__Assembler{w: &w, m: &m}}
}
type _Bytes__Assembler struct {
w *_Bytes
m *schema.Maybe
}
func (na *_Bytes__Assembler) reset() {}
func (_Bytes__Assembler) BeginMap(sizeHint int) (ipld.MapAssembler, error) {
return mixins.BytesAssembler{"dagpb.Bytes"}.BeginMap(0)
}
func (_Bytes__Assembler) BeginList(sizeHint int) (ipld.ListAssembler, error) {
return mixins.BytesAssembler{"dagpb.Bytes"}.BeginList(0)
}
func (na *_Bytes__Assembler) AssignNull() error {
switch *na.m {
case allowNull:
*na.m = schema.Maybe_Null
return nil
case schema.Maybe_Absent:
return mixins.BytesAssembler{"dagpb.Bytes"}.AssignNull()
case schema.Maybe_Value, schema.Maybe_Null:
panic("invalid state: cannot assign into assembler that's already finished")
}
panic("unreachable")
}
func (_Bytes__Assembler) AssignBool(bool) error {
return mixins.BytesAssembler{"dagpb.Bytes"}.AssignBool(false)
}
func (_Bytes__Assembler) AssignInt(int) error {
return mixins.BytesAssembler{"dagpb.Bytes"}.AssignInt(0)
}
func (_Bytes__Assembler) AssignFloat(float64) error {
return mixins.BytesAssembler{"dagpb.Bytes"}.AssignFloat(0)
}
func (_Bytes__Assembler) AssignString(string) error {
return mixins.BytesAssembler{"dagpb.Bytes"}.AssignString("")
}
func (na *_Bytes__Assembler) AssignBytes(v []byte) error {
switch *na.m {
case schema.Maybe_Value, schema.Maybe_Null:
panic("invalid state: cannot assign into assembler that's already finished")
}
if na.w == nil {
na.w = &_Bytes{}
}
na.w.x = v
*na.m = schema.Maybe_Value
return nil
}
func (_Bytes__Assembler) AssignLink(ipld.Link) error {
return mixins.BytesAssembler{"dagpb.Bytes"}.AssignLink(nil)
}
func (na *_Bytes__Assembler) AssignNode(v ipld.Node) error {
if v.IsNull() {
return na.AssignNull()
}
if v2, ok := v.(*_Bytes); ok {
switch *na.m {
case schema.Maybe_Value, schema.Maybe_Null:
panic("invalid state: cannot assign into assembler that's already finished")
}
if na.w == nil {
na.w = v2
*na.m = schema.Maybe_Value
return nil
}
*na.w = *v2
*na.m = schema.Maybe_Value
return nil
}
if v2, err := v.AsBytes(); err != nil {
return err
} else {
return na.AssignBytes(v2)
}
}
func (_Bytes__Assembler) Prototype() ipld.NodePrototype {
return _Bytes__Prototype{}
}
func (Bytes) Type() schema.Type {
return nil /*TODO:typelit*/
}
func (n Bytes) Representation() ipld.Node {
return (*_Bytes__Repr)(n)
}
type _Bytes__Repr = _Bytes
var _ ipld.Node = &_Bytes__Repr{}
type _Bytes__ReprPrototype = _Bytes__Prototype
type _Bytes__ReprAssembler = _Bytes__Assembler
package dagpb
// Code generated by go-ipld-prime gengo. DO NOT EDIT.
import (
ipld "github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/node/mixins"
"github.com/ipld/go-ipld-prime/schema"
)
type _Int struct{ x int }
type Int = *_Int
func (n Int) Int() int {
return n.x
}
func (_Int__Prototype) FromInt(v int) (Int, error) {
n := _Int{v}
return &n, nil
}
type _Int__Maybe struct {
m schema.Maybe
v Int
}
type MaybeInt = *_Int__Maybe
func (m MaybeInt) IsNull() bool {
return m.m == schema.Maybe_Null
}
func (m MaybeInt) IsAbsent() bool {
return m.m == schema.Maybe_Absent
}
func (m MaybeInt) Exists() bool {
return m.m == schema.Maybe_Value
}
func (m MaybeInt) AsNode() ipld.Node {
switch m.m {
case schema.Maybe_Absent:
return ipld.Absent
case schema.Maybe_Null:
return ipld.Null
case schema.Maybe_Value:
return m.v
default:
panic("unreachable")
}
}
func (m MaybeInt) Must() Int {
if !m.Exists() {
panic("unbox of a maybe rejected")
}
return m.v
}
var _ ipld.Node = (Int)(&_Int{})
var _ schema.TypedNode = (Int)(&_Int{})
func (Int) ReprKind() ipld.ReprKind {
return ipld.ReprKind_Int
}
func (Int) LookupByString(string) (ipld.Node, error) {
return mixins.Int{"dagpb.Int"}.LookupByString("")
}
func (Int) LookupByNode(ipld.Node) (ipld.Node, error) {
return mixins.Int{"dagpb.Int"}.LookupByNode(nil)
}
func (Int) LookupByIndex(idx int) (ipld.Node, error) {
return mixins.Int{"dagpb.Int"}.LookupByIndex(0)
}
func (Int) LookupBySegment(seg ipld.PathSegment) (ipld.Node, error) {
return mixins.Int{"dagpb.Int"}.LookupBySegment(seg)
}
func (Int) MapIterator() ipld.MapIterator {
return nil
}
func (Int) ListIterator() ipld.ListIterator {
return nil
}
func (Int) Length() int {
return -1
}
func (Int) IsAbsent() bool {
return false
}
func (Int) IsNull() bool {
return false
}
func (Int) AsBool() (bool, error) {
return mixins.Int{"dagpb.Int"}.AsBool()
}
func (n Int) AsInt() (int, error) {
return n.x, nil
}
func (Int) AsFloat() (float64, error) {
return mixins.Int{"dagpb.Int"}.AsFloat()
}
func (Int) AsString() (string, error) {
return mixins.Int{"dagpb.Int"}.AsString()
}
func (Int) AsBytes() ([]byte, error) {
return mixins.Int{"dagpb.Int"}.AsBytes()
}
func (Int) AsLink() (ipld.Link, error) {
return mixins.Int{"dagpb.Int"}.AsLink()
}
func (Int) Prototype() ipld.NodePrototype {
return _Int__Prototype{}
}
type _Int__Prototype struct{}
func (_Int__Prototype) NewBuilder() ipld.NodeBuilder {
var nb _Int__Builder
nb.Reset()
return &nb
}
type _Int__Builder struct {
_Int__Assembler
}
func (nb *_Int__Builder) Build() ipld.Node {
if *nb.m != schema.Maybe_Value {
panic("invalid state: cannot call Build on an assembler that's not finished")
}
return nb.w
}
func (nb *_Int__Builder) Reset() {
var w _Int
var m schema.Maybe
*nb = _Int__Builder{_Int__Assembler{w: &w, m: &m}}
}
type _Int__Assembler struct {
w *_Int
m *schema.Maybe
}
func (na *_Int__Assembler) reset() {}
func (_Int__Assembler) BeginMap(sizeHint int) (ipld.MapAssembler, error) {
return mixins.IntAssembler{"dagpb.Int"}.BeginMap(0)
}
func (_Int__Assembler) BeginList(sizeHint int) (ipld.ListAssembler, error) {
return mixins.IntAssembler{"dagpb.Int"}.BeginList(0)
}
func (na *_Int__Assembler) AssignNull() error {
switch *na.m {
case allowNull:
*na.m = schema.Maybe_Null
return nil
case schema.Maybe_Absent:
return mixins.IntAssembler{"dagpb.Int"}.AssignNull()
case schema.Maybe_Value, schema.Maybe_Null:
panic("invalid state: cannot assign into assembler that's already finished")
}
panic("unreachable")
}
func (_Int__Assembler) AssignBool(bool) error {
return mixins.IntAssembler{"dagpb.Int"}.AssignBool(false)
}
func (na *_Int__Assembler) AssignInt(v int) error {
switch *na.m {
case schema.Maybe_Value, schema.Maybe_Null:
panic("invalid state: cannot assign into assembler that's already finished")
}
if na.w == nil {
na.w = &_Int{}
}
na.w.x = v
*na.m = schema.Maybe_Value
return nil
}
func (_Int__Assembler) AssignFloat(float64) error {
return mixins.IntAssembler{"dagpb.Int"}.AssignFloat(0)
}
func (_Int__Assembler) AssignString(string) error {
return mixins.IntAssembler{"dagpb.Int"}.AssignString("")
}
func (_Int__Assembler) AssignBytes([]byte) error {
return mixins.IntAssembler{"dagpb.Int"}.AssignBytes(nil)
}
func (_Int__Assembler) AssignLink(ipld.Link) error {
return mixins.IntAssembler{"dagpb.Int"}.AssignLink(nil)
}
func (na *_Int__Assembler) AssignNode(v ipld.Node) error {
if v.IsNull() {
return na.AssignNull()
}
if v2, ok := v.(*_Int); ok {
switch *na.m {
case schema.Maybe_Value, schema.Maybe_Null:
panic("invalid state: cannot assign into assembler that's already finished")
}
if na.w == nil {
na.w = v2
*na.m = schema.Maybe_Value
return nil
}
*na.w = *v2
*na.m = schema.Maybe_Value
return nil
}
if v2, err := v.AsInt(); err != nil {
return err
} else {
return na.AssignInt(v2)
}
}
func (_Int__Assembler) Prototype() ipld.NodePrototype {
return _Int__Prototype{}
}
func (Int) Type() schema.Type {
return nil /*TODO:typelit*/
}
func (n Int) Representation() ipld.Node {
return (*_Int__Repr)(n)
}
type _Int__Repr = _Int
var _ ipld.Node = &_Int__Repr{}
type _Int__ReprPrototype = _Int__Prototype
type _Int__ReprAssembler = _Int__Assembler
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment