Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

Commit

Permalink
feat(deps): upgrade to new IPLD prime
Browse files Browse the repository at this point in the history
upgrade to new ipld prime, with new node builders and styles. remove parts of code that will not
code gen
  • Loading branch information
hannahhoward committed Mar 31, 2020
1 parent e32bd15 commit a6d6f40
Show file tree
Hide file tree
Showing 13 changed files with 710 additions and 1,170 deletions.
59 changes: 37 additions & 22 deletions coding.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,82 @@ import (
"io"
"io/ioutil"

"github.com/ipld/go-ipld-prime/schema"

"github.com/ipfs/go-cid"
merkledag_pb "github.com/ipfs/go-merkledag/pb"
ipld "github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
)

// DecodeDagProto is a fast path decoding to protobuf
// from PBNode__NodeBuilders
func (nb _PBNode__NodeBuilder) DecodeDagProto(r io.Reader) (ipld.Node, error) {
func (nb _PBNode__NodeBuilder) DecodeDagProto(r io.Reader) error {
var pbn merkledag_pb.PBNode
encoded, err := ioutil.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("io error during unmarshal. %v", err)
return fmt.Errorf("io error during unmarshal. %v", err)
}
if err := pbn.Unmarshal(encoded); err != nil {
return nil, fmt.Errorf("unmarshal failed. %v", err)
return fmt.Errorf("unmarshal failed. %v", err)
}
pbLinks := make([]PBLink, 0, len(pbn.Links))
for _, link := range pbn.Links {
hash, err := cid.Cast(link.GetHash())

if err != nil {
return nil, fmt.Errorf("unmarshal failed. %v", err)
return fmt.Errorf("unmarshal failed. %v", err)
}
pbLinks = append(pbLinks, PBLink{
&Link{cidlink.Link{Cid: hash}},
&String{link.GetName()},
&Int{int(link.GetTsize())},
d: PBLink__Content{
Hash: MaybeLink{
Maybe: schema.Maybe_Value,
Value: Link{cidlink.Link{Cid: hash}},
},
Name: MaybeString{
Maybe: schema.Maybe_Value,
Value: String{link.GetName()},
},
Tsize: MaybeInt{
Maybe: schema.Maybe_Value,
Value: Int{int(link.GetTsize())},
},
},
})
}
pbData := Bytes{pbn.GetData()}
return PBNode{PBLinks{pbLinks}, pbData}, nil
nb.nd.d.Links.x = pbLinks
nb.nd.d.Data.x = pbn.GetData()
return nil
}

// EncodeDagProto is a fast path encoding to protobuf
// for PBNode types
func (nd PBNode) EncodeDagProto(w io.Writer) error {
pbn := new(merkledag_pb.PBNode)
pbn.Links = make([]*merkledag_pb.PBLink, 0, len(nd.Links.x))
for _, link := range nd.Links.x {
pbn.Links = make([]*merkledag_pb.PBLink, 0, len(nd.d.Links.x))
for _, link := range nd.d.Links.x {
var hash []byte
if link.Hash != nil {
cid := link.Hash.x.(cidlink.Link).Cid
if link.d.Hash.Maybe == schema.Maybe_Value {
cid := link.d.Hash.Value.x.(cidlink.Link).Cid
if cid.Defined() {
hash = cid.Bytes()
}
}
var name *string
if link.Name != nil {
name = &link.Name.x
if link.d.Name.Maybe == schema.Maybe_Value {
tmp := link.d.Name.Value.x
name = &tmp
}
var tsize *uint64
if link.Tsize != nil {
tmp := uint64(link.Tsize.x)
if link.d.Tsize.Maybe == schema.Maybe_Value {
tmp := uint64(link.d.Tsize.Value.x)
tsize = &tmp
}
pbn.Links = append(pbn.Links, &merkledag_pb.PBLink{
Hash: hash,
Name: name,
Tsize: tsize})
}
pbn.Data = nd.Data.x
pbn.Data = nd.d.Data.x
data, err := pbn.Marshal()
if err != nil {
return fmt.Errorf("marshal failed. %v", err)
Expand All @@ -80,12 +94,13 @@ func (nd PBNode) EncodeDagProto(w io.Writer) error {

// DecodeDagRaw is a fast path decoding to protobuf
// from RawNode__NodeBuilders
func (nb _RawNode__NodeBuilder) DecodeDagRaw(r io.Reader) (ipld.Node, error) {
func (nb _RawNode__NodeBuilder) DecodeDagRaw(r io.Reader) error {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("io error during unmarshal. %v", err)
return fmt.Errorf("io error during unmarshal. %v", err)
}
return RawNode{data}, nil
nb.nd.x = data
return nil
}

// EncodeDagRaw is a fast path encoding to protobuf
Expand Down
14 changes: 9 additions & 5 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package dagpb_test
import (
"bytes"

blocks "github.com/ipfs/go-block-format"
ipld "github.com/ipld/go-ipld-prime"
dagpb "github.com/ipld/go-ipld-prime-proto"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/jbenet/go-random"
)

Expand All @@ -20,12 +18,17 @@ func randomBytes(n int64) []byte {
}

func makeRawNode(randBytes []byte) (ipld.Node, error) {
raw_nb := dagpb.RawNode__NodeBuilder()
return raw_nb.CreateBytes(randBytes)
raw_nb := dagpb.Style.Raw.NewBuilder()
err := raw_nb.AssignBytes(randBytes)
if err != nil {
return nil, err
}
return raw_nb.Build(), nil
}

/*
func makeProtoNode(linkedNodes map[string]ipld.Node) (ipld.Node, error) {
dagpb_nb := dagpb.PBNode__NodeBuilder()
dag_ns := dagpb.PBNode__NodeBuilder()
dagpb_mb, err := dagpb_nb.CreateMap()
if err != nil {
return nil, err
Expand Down Expand Up @@ -120,3 +123,4 @@ func makeProtoNode(linkedNodes map[string]ipld.Node) (ipld.Node, error) {
}
return dagpb_mb.Build()
}
*/
141 changes: 133 additions & 8 deletions gen/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,106 @@
package main

import (
"io"
"io/ioutil"
"os"
"regexp"

"github.com/ipld/go-ipld-prime/schema"
gengo "github.com/ipld/go-ipld-prime/schema/gen/go"
)

type typedNodeGenerator interface {

// -- the natively-typed apis -->
// (might be more readable to group these in another interface and have it
// return a `typedNodeGenerator` with the rest? but structurally same.)

EmitNativeType(io.Writer)
EmitNativeAccessors(io.Writer) // depends on the kind -- field accessors for struct, typed iterators for map, etc.
EmitNativeBuilder(io.Writer) // typically emits some kind of struct that has a Build method.
EmitNativeMaybe(io.Writer) // a pointer-free 'maybe' mechanism is generated for all types.

// -- the schema.TypedNode.Type method and vars -->

EmitTypedNodeMethodType(io.Writer) // these emit dummies for now

// -- all node methods -->
// (and note that the nodeBuilder for this one should be the "semantic" one,
// e.g. it *always* acts like a map for structs, even if the repr is different.)

nodeGenerator

// -- and the representation and its node and nodebuilder -->

EmitTypedNodeMethodRepresentation(io.Writer)
}

type typedLinkNodeGenerator interface {
// all methods in typedNodeGenerator
typedNodeGenerator

// as typed.LinkNode.ReferencedNodeBuilder generator
EmitTypedLinkNodeMethodReferencedNodeBuilder(io.Writer)
}

type nodeGenerator interface {
EmitNodeType(io.Writer)
EmitNodeMethodReprKind(io.Writer)
EmitNodeMethodLookupString(io.Writer)
EmitNodeMethodLookup(io.Writer)
EmitNodeMethodLookupIndex(io.Writer)
EmitNodeMethodLookupSegment(io.Writer)
EmitNodeMethodMapIterator(io.Writer) // also iterator itself
EmitNodeMethodListIterator(io.Writer) // also iterator itself
EmitNodeMethodLength(io.Writer)
EmitNodeMethodIsUndefined(io.Writer)
EmitNodeMethodIsNull(io.Writer)
EmitNodeMethodAsBool(io.Writer)
EmitNodeMethodAsInt(io.Writer)
EmitNodeMethodAsFloat(io.Writer)
EmitNodeMethodAsString(io.Writer)
EmitNodeMethodAsBytes(io.Writer)
EmitNodeMethodAsLink(io.Writer)
}

func emitEntireType(ng nodeGenerator, w io.Writer) {
if ng == nil {
return
}
ng.EmitNodeType(w)
ng.EmitNodeMethodReprKind(w)
ng.EmitNodeMethodLookupString(w)
ng.EmitNodeMethodLookup(w)
ng.EmitNodeMethodLookupIndex(w)
ng.EmitNodeMethodLookupSegment(w)
ng.EmitNodeMethodMapIterator(w)
ng.EmitNodeMethodListIterator(w)
ng.EmitNodeMethodLength(w)
ng.EmitNodeMethodIsUndefined(w)
ng.EmitNodeMethodIsNull(w)
ng.EmitNodeMethodAsBool(w)
ng.EmitNodeMethodAsInt(w)
ng.EmitNodeMethodAsFloat(w)
ng.EmitNodeMethodAsString(w)
ng.EmitNodeMethodAsBytes(w)
ng.EmitNodeMethodAsLink(w)

tg, ok := ng.(typedNodeGenerator)
if ok {
tg.EmitNativeType(w)
tg.EmitNativeAccessors(w)
tg.EmitNativeBuilder(w)
tg.EmitNativeMaybe(w)
tg.EmitTypedNodeMethodType(w)
tg.EmitTypedNodeMethodRepresentation(w)
}
tlg, ok := ng.(typedLinkNodeGenerator)
if ok {
tlg.EmitTypedLinkNodeMethodReferencedNodeBuilder(w)
}
}

func main() {
openOrPanic := func(filename string) *os.File {
y, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
Expand Down Expand Up @@ -47,15 +141,46 @@ func main() {

f = openOrPanic("pb_node_gen.go")
gengo.EmitFileHeader("dagpb", f)
gengo.EmitEntireType(gengo.NewGeneratorForKindString(tString), f)
gengo.EmitEntireType(gengo.NewGeneratorForKindInt(tInt), f)
gengo.EmitEntireType(gengo.NewGeneratorForKindBytes(tBytes), f)
gengo.EmitEntireType(gengo.NewGeneratorForKindLink(tLink), f)
gengo.EmitEntireType(gengo.NewGeneratorForKindStruct(tPBLink), f)
gengo.EmitEntireType(gengo.NewGeneratorForKindList(tPBLinks), f)
gengo.EmitEntireType(gengo.NewGeneratorForKindStruct(tPBNode), f)
tg := gengo.NewGeneratorForKindString(tString)
emitEntireType(tg, f)
emitEntireType(tg.GetRepresentationNodeGen(), f)
tg = gengo.NewGeneratorForKindInt(tInt)
emitEntireType(tg, f)
emitEntireType(tg.GetRepresentationNodeGen(), f)
tg = gengo.NewGeneratorForKindBytes(tBytes)
emitEntireType(tg, f)
emitEntireType(tg.GetRepresentationNodeGen(), f)
tg = gengo.NewGeneratorForKindLink(tLink)
emitEntireType(tg, f)
emitEntireType(tg.GetRepresentationNodeGen(), f)
tg = gengo.NewGeneratorForKindStruct(tPBLink)
emitEntireType(tg, f)
emitEntireType(tg.GetRepresentationNodeGen(), f)
tg = gengo.NewGeneratorForKindList(tPBLinks)
emitEntireType(tg, f)
emitEntireType(tg.GetRepresentationNodeGen(), f)
tg = gengo.NewGeneratorForKindStruct(tPBNode)
emitEntireType(tg, f)
emitEntireType(tg.GetRepresentationNodeGen(), f)
if err := f.Close(); err != nil {
panic(err)
}
read, err := ioutil.ReadFile("pb_node_gen.go")
if err != nil {
panic(err)
}

re := regexp.MustCompile("ipld\\.ErrInvalidKey\\{[^}]*\\}")
newContents := re.ReplaceAll(read, []byte("err"))

err = ioutil.WriteFile("pb_node_gen.go", []byte(newContents), 0)
if err != nil {
panic(err)
}

f = openOrPanic("raw_node_gen.go")
gengo.EmitFileHeader("dagpb", f)
gengo.EmitEntireType(gengo.NewGeneratorForKindBytes(tRaw), f)
tg = gengo.NewGeneratorForKindBytes(tRaw)
emitEntireType(tg, f)
emitEntireType(tg.GetRepresentationNodeGen(), f)
}
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.13
require (
github.com/ipfs/go-block-format v0.0.2
github.com/ipfs/go-blockservice v0.1.0
github.com/ipfs/go-cid v0.0.3
github.com/ipfs/go-cid v0.0.4
github.com/ipfs/go-datastore v0.0.5
github.com/ipfs/go-ipfs-blockstore v0.0.1
github.com/ipfs/go-ipfs-chunker v0.0.1
Expand All @@ -14,8 +14,8 @@ require (
github.com/ipfs/go-ipld-format v0.0.2
github.com/ipfs/go-merkledag v0.2.3
github.com/ipfs/go-unixfs v0.2.2-0.20190827150610-868af2e9e5cb
github.com/ipld/go-ipld-prime v0.0.2-0.20191108012745-28a82f04c785
github.com/ipld/go-ipld-prime v0.0.2-0.20200327122045-fc80c2b0149d
github.com/jbenet/go-random v0.0.0-20190219211222-123a90aedc0c
github.com/multiformats/go-multihash v0.0.5
github.com/warpfork/go-wish v0.0.0-20190328234359-8b3e70f8e830
github.com/multiformats/go-multihash v0.0.10
github.com/warpfork/go-wish v0.0.0-20200122115046-b9ea61034e4a
)
Loading

0 comments on commit a6d6f40

Please sign in to comment.