-
Notifications
You must be signed in to change notification settings - Fork 26
/
apiconv.go
172 lines (149 loc) · 4.33 KB
/
apiconv.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package registry
import (
"time"
"github.com/ipfs/go-cid"
"github.com/ipni/go-libipni/find/model"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
)
// RegToApiProviderInfo converts provider info from registry to api objects.
func RegToApiProviderInfo(pi *ProviderInfo) *model.ProviderInfo {
if pi == nil {
return nil
}
apiPI := &model.ProviderInfo{
AddrInfo: pi.AddrInfo,
Lag: pi.Lag,
Inactive: pi.Inactive(),
}
if pi.LastAdvertisement != cid.Undef {
apiPI.LastAdvertisement = pi.LastAdvertisement
if !pi.LastAdvertisementTime.IsZero() {
apiPI.LastAdvertisementTime = pi.LastAdvertisementTime.Format(time.RFC3339)
}
}
if pi.LastError != "" {
apiPI.LastError = pi.LastError
if !pi.LastErrorTime.IsZero() {
apiPI.LastErrorTime = pi.LastErrorTime.Format(time.RFC3339)
}
}
if pi.Publisher.Validate() == nil && pi.PublisherAddr != nil {
apiPI.Publisher = &peer.AddrInfo{
ID: pi.Publisher,
Addrs: []multiaddr.Multiaddr{pi.PublisherAddr},
}
}
if pi.FrozenAt != cid.Undef {
apiPI.FrozenAt = pi.FrozenAt
}
// Provider is still frozen even if there is no FrozenAt CID.
if !pi.FrozenAtTime.IsZero() {
apiPI.FrozenAtTime = pi.FrozenAtTime.Format(time.RFC3339)
}
xpiToApi := func(xpis []ExtendedProviderInfo) ([]peer.AddrInfo, [][]byte) {
if len(xpis) == 0 {
return nil, nil
}
apiProvs := make([]peer.AddrInfo, len(xpis))
var apiMetas = make([][]byte, len(xpis))
for i := range xpis {
apiProvs[i] = peer.AddrInfo{
ID: xpis[i].PeerID,
Addrs: xpis[i].Addrs,
}
apiMetas[i] = xpis[i].Metadata
}
return apiProvs, apiMetas
}
if pi.ExtendedProviders != nil {
xp := pi.ExtendedProviders
apiProvs, apiMetas := xpiToApi(xp.Providers)
var apiCtxProvs []model.ContextualExtendedProviders
if len(xp.ContextualProviders) != 0 {
apiCtxProvs = make([]model.ContextualExtendedProviders, len(xp.ContextualProviders))
var i int
for contextID, cxp := range xp.ContextualProviders {
provs, metas := xpiToApi(cxp.Providers)
apiCtxProvs[i] = model.ContextualExtendedProviders{
Override: cxp.Override,
ContextID: contextID,
Providers: provs,
Metadatas: metas,
}
i++
}
}
apiPI.ExtendedProviders = &model.ExtendedProviders{
Providers: apiProvs,
Contextual: apiCtxProvs,
Metadatas: apiMetas,
}
}
return apiPI
}
// apiToRegProviderInfo converts provider info from api to registry objects.
func apiToRegProviderInfo(apiPI *model.ProviderInfo) *ProviderInfo {
if apiPI == nil {
return nil
}
regInfo := &ProviderInfo{
AddrInfo: apiPI.AddrInfo,
}
if apiPI.Publisher != nil && apiPI.Publisher.ID.Validate() == nil {
regInfo.Publisher = apiPI.Publisher.ID
if len(apiPI.Publisher.Addrs) == 0 {
if apiPI.Publisher.ID == regInfo.AddrInfo.ID {
// Publisher does not have addresses, so use provider address
// if the publisher and provider ID are the same.
regInfo.PublisherAddr = regInfo.AddrInfo.Addrs[0]
}
} else {
regInfo.PublisherAddr = apiPI.Publisher.Addrs[0]
}
} else {
regInfo.Publisher = regInfo.AddrInfo.ID
regInfo.PublisherAddr = regInfo.AddrInfo.Addrs[0]
}
if apiPI.ExtendedProviders != nil {
apiXP := apiPI.ExtendedProviders
xpiToReg := func(providers []peer.AddrInfo, metadatas [][]byte) []ExtendedProviderInfo {
if len(providers) == 0 {
return nil
}
xpis := make([]ExtendedProviderInfo, len(providers))
mdOk := len(metadatas) == len(providers)
for i, addrInfo := range providers {
var md []byte
if mdOk {
md = metadatas[i]
}
xpis[i] = ExtendedProviderInfo{
PeerID: addrInfo.ID,
Addrs: addrInfo.Addrs,
Metadata: md,
}
}
return xpis
}
regProvs := xpiToReg(apiXP.Providers, apiXP.Metadatas)
var regCtxProvs map[string]ContextualExtendedProviders
if len(apiXP.Contextual) != 0 {
regCtxProvs = make(map[string]ContextualExtendedProviders, len(apiXP.Contextual))
for _, cep := range apiXP.Contextual {
regCtxProvs[cep.ContextID] = ContextualExtendedProviders{
ContextID: []byte(cep.ContextID),
Providers: xpiToReg(cep.Providers, cep.Metadatas),
Override: cep.Override,
}
}
}
if regProvs != nil || regCtxProvs != nil {
regInfo.ExtendedProviders = &ExtendedProviders{
Providers: regProvs,
ContextualProviders: regCtxProvs,
}
}
}
return regInfo
}