-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmetadata.go
58 lines (51 loc) · 1.55 KB
/
metadata.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
package metadata
import (
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipfs/go-graphsync"
"github.com/ipfs/go-graphsync/message"
"github.com/ipfs/go-graphsync/message/ipldbind"
)
// Item is a single link traversed in a repsonse
type Item struct {
Link cid.Cid
BlockPresent bool
}
// Metadata is information about metadata contained in a response, which can be
// serialized back and forth to bytes
type Metadata []Item
func (md Metadata) ToGraphSyncMetadata() []message.GraphSyncLinkMetadatum {
if len(md) == 0 {
return nil
}
gsm := make([]message.GraphSyncLinkMetadatum, 0, len(md))
for _, ii := range md {
action := graphsync.LinkActionPresent
if !ii.BlockPresent {
action = graphsync.LinkActionMissing
}
gsm = append(gsm, message.GraphSyncLinkMetadatum{Link: ii.Link, Action: action})
}
return gsm
}
// DecodeMetadata assembles metadata from a raw byte array, first deserializing
// as a node and then assembling into a metadata struct.
func DecodeMetadata(data datamodel.Node) (Metadata, error) {
if data == nil {
return nil, nil
}
builder := Prototype.Metadata.Representation().NewBuilder()
err := builder.AssignNode(data)
if err != nil {
return nil, err
}
metadata, err := ipldbind.SafeUnwrap(builder.Build())
if err != nil {
return nil, err
}
return *(metadata.(*Metadata)), nil
}
// EncodeMetadata encodes metadata to an IPLD node then serializes to raw bytes
func EncodeMetadata(entries Metadata) (datamodel.Node, error) {
return ipldbind.SafeWrap(&entries, Prototype.Metadata.Type())
}