This repository has been archived by the owner on Nov 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
cast.go
63 lines (58 loc) · 1.46 KB
/
cast.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
package cast
import (
"net/url"
"github.com/ipfs/go-cid"
ma "github.com/multiformats/go-multiaddr"
pb "github.com/textileio/bidbot/gen/v1"
"github.com/textileio/bidbot/lib/auction"
)
// SourcesToPb converts Sources to pb.
func SourcesToPb(sources auction.Sources) *pb.Sources {
var carIPFS *pb.Sources_CARIPFS
if sources.CARIPFS != nil {
var multiaddrs []string
for _, addr := range sources.CARIPFS.Multiaddrs {
multiaddrs = append(multiaddrs, addr.String())
}
carIPFS = &pb.Sources_CARIPFS{
Cid: sources.CARIPFS.Cid.String(),
Multiaddrs: multiaddrs,
}
}
var carURL *pb.Sources_CARURL
if sources.CARURL != nil {
carURL = &pb.Sources_CARURL{
URL: sources.CARURL.URL.String(),
}
}
return &pb.Sources{
CarUrl: carURL,
CarIpfs: carIPFS,
}
}
// SourcesFromPb converts Sources back from pb.
func SourcesFromPb(pbs *pb.Sources) (sources auction.Sources, err error) {
if pbs.CarUrl != nil {
u, err := url.Parse(pbs.CarUrl.URL)
if err != nil {
return auction.Sources{}, err
}
sources.CARURL = &auction.CARURL{URL: *u}
}
if pbs.CarIpfs != nil {
id, err := cid.Parse(pbs.CarIpfs.Cid)
if err != nil {
return auction.Sources{}, err
}
var multiaddrs []ma.Multiaddr
for _, s := range pbs.CarIpfs.Multiaddrs {
addr, err := ma.NewMultiaddr(s)
if err != nil {
return auction.Sources{}, err
}
multiaddrs = append(multiaddrs, addr)
}
sources.CARIPFS = &auction.CARIPFS{Cid: id, Multiaddrs: multiaddrs}
}
return
}