This repository has been archived by the owner on Jan 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcoding.go
131 lines (123 loc) · 3.37 KB
/
coding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package dagpb
import (
"fmt"
"io"
"io/ioutil"
"github.com/ipfs/go-cid"
merkledag_pb "github.com/ipfs/go-merkledag/pb"
"github.com/ipld/go-ipld-prime/fluent"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
)
// byteAccessor is a reader interface that can access underlying bytes
type byteAccesor interface {
Bytes() []byte
}
// DecodeDagProto is a fast path decoding to protobuf
// from PBNode__Builders
func (nb *_PBNode__Builder) DecodeDagProto(r io.Reader) error {
var pbn merkledag_pb.PBNode
var encoded []byte
var err error
byteBuf, ok := r.(byteAccesor)
if ok {
encoded = byteBuf.Bytes()
} else {
encoded, err = ioutil.ReadAll(r)
if err != nil {
return fmt.Errorf("io error during unmarshal. %v", err)
}
}
if err := pbn.Unmarshal(encoded); err != nil {
return fmt.Errorf("unmarshal failed. %v", err)
}
return fluent.Recover(func() {
fb := fluent.WrapAssembler(nb)
fb.CreateMap(-1, func(fmb fluent.MapAssembler) {
fmb.AssembleEntry("Links").CreateList(int64(len(pbn.Links)), func(flb fluent.ListAssembler) {
for _, link := range pbn.Links {
hash, err := cid.Cast(link.GetHash())
if err != nil {
panic(fluent.Error{Err: fmt.Errorf("unmarshal failed. %v", err)})
}
flb.AssembleValue().CreateMap(-1, func(fmb fluent.MapAssembler) {
fmb.AssembleEntry("Hash").AssignLink(cidlink.Link{Cid: hash})
fmb.AssembleEntry("Name").AssignString(link.GetName())
fmb.AssembleEntry("Tsize").AssignInt(int64(link.GetTsize()))
})
}
})
fmb.AssembleEntry("Data").AssignBytes(pbn.GetData())
})
})
}
// 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, nd.FieldLinks().Length())
linksIter := nd.FieldLinks().ListIterator()
for !linksIter.Done() {
_, nlink, err := linksIter.Next()
if err != nil {
return err
}
link := nlink.(PBLink)
var hash []byte
if link.FieldHash().Exists() {
cid := link.FieldHash().Must().Link().(cidlink.Link).Cid
if cid.Defined() {
hash = cid.Bytes()
}
}
var name *string
if link.FieldName().Exists() {
tmp := link.FieldName().Must().String()
name = &tmp
}
var tsize *uint64
if link.FieldTsize().Exists() {
tmp := uint64(link.FieldTsize().Must().Int())
tsize = &tmp
}
pbn.Links = append(pbn.Links, &merkledag_pb.PBLink{
Hash: hash,
Name: name,
Tsize: tsize})
}
pbn.Data = nd.FieldData().Bytes()
data, err := pbn.Marshal()
if err != nil {
return fmt.Errorf("marshal failed. %v", err)
}
_, err = w.Write(data)
if err != nil {
return fmt.Errorf(" error during marshal. %v", err)
}
return nil
}
// DecodeDagRaw is a fast path decoding to protobuf
// from RawNode__Builders
func (nb *_RawNode__Builder) DecodeDagRaw(r io.Reader) error {
return fluent.Recover(func() {
fnb := fluent.WrapAssembler(nb)
byteBuf, ok := r.(byteAccesor)
if ok {
fnb.AssignBytes(byteBuf.Bytes())
return
}
data, err := ioutil.ReadAll(r)
if err != nil {
panic(fluent.Error{Err: fmt.Errorf("io error during unmarshal. %v", err)})
}
fnb.AssignBytes(data)
})
}
// EncodeDagRaw is a fast path encoding to protobuf
// for RawNode types
func (nd RawNode) EncodeDagRaw(w io.Writer) error {
_, err := w.Write(nd.Bytes())
if err != nil {
return fmt.Errorf(" error during marshal. %v", err)
}
return nil
}