-
Notifications
You must be signed in to change notification settings - Fork 85
/
cidlink.go
88 lines (76 loc) · 1.95 KB
/
cidlink.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
package data
import (
"encoding/json"
"fmt"
"io"
"github.com/ipfs/go-cid"
cbg "github.com/whyrusleeping/cbor-gen"
)
// Represents the "cid-link" type from the atproto data model.
//
// Implementation is a simple wrapper around the github.com/ipfs/go-cid "cid.Cid" type.
type CIDLink cid.Cid
type jsonLink struct {
Link string `json:"$link"`
}
// Unwraps the inner cid.Cid type (github.com/ipfs/go-cid)
func (ll CIDLink) CID() cid.Cid {
return cid.Cid(ll)
}
// Returns string representation.
//
// If the CID is "undefined", returns an empty string (note that this is different from how cid.Cid works).
func (ll CIDLink) String() string {
if ll.IsDefined() {
return cid.Cid(ll).String()
}
return ""
}
// Convenience helper, returns false if CID is "undefined" (golang zero value)
func (ll CIDLink) IsDefined() bool {
return cid.Cid(ll).Defined()
}
func (ll CIDLink) MarshalJSON() ([]byte, error) {
if !ll.IsDefined() {
return nil, fmt.Errorf("tried to marshal nil or undefined cid-link")
}
jl := jsonLink{
Link: ll.String(),
}
return json.Marshal(jl)
}
func (ll *CIDLink) UnmarshalJSON(raw []byte) error {
var jl jsonLink
if err := json.Unmarshal(raw, &jl); err != nil {
return fmt.Errorf("parsing cid-link JSON: %v", err)
}
c, err := cid.Decode(jl.Link)
if err != nil {
return fmt.Errorf("parsing cid-link CID: %v", err)
}
*ll = CIDLink(c)
return nil
}
func (ll *CIDLink) MarshalCBOR(w io.Writer) error {
if ll == nil {
_, err := w.Write(cbg.CborNull)
return err
}
if !ll.IsDefined() {
return fmt.Errorf("tried to marshal nil or undefined cid-link")
}
cw := cbg.NewCborWriter(w)
if err := cbg.WriteCid(cw, cid.Cid(*ll)); err != nil {
return fmt.Errorf("failed to write cid-link as CBOR: %w", err)
}
return nil
}
func (ll *CIDLink) UnmarshalCBOR(r io.Reader) error {
cr := cbg.NewCborReader(r)
c, err := cbg.ReadCid(cr)
if err != nil {
return fmt.Errorf("failed to read cid-link from CBOR: %w", err)
}
*ll = CIDLink(c)
return nil
}