@@ -15,7 +15,7 @@ type Component struct {
15
15
// bytes is the raw bytes of the component. It includes the protocol code as
16
16
// varint, possibly the size of the value, and the value.
17
17
bytes string // string for immutability.
18
- protocol Protocol
18
+ protocol * Protocol
19
19
valueStartIdx int // Index of the first byte of the Component's value in the bytes array
20
20
}
21
21
@@ -110,18 +110,27 @@ func (c Component) Compare(o Component) int {
110
110
}
111
111
112
112
func (c Component ) Protocols () []Protocol {
113
- return []Protocol {c .protocol }
113
+ if c .protocol == nil {
114
+ return nil
115
+ }
116
+ return []Protocol {* c .protocol }
114
117
}
115
118
116
119
func (c Component ) ValueForProtocol (code int ) (string , error ) {
120
+ if c .protocol == nil {
121
+ return "" , fmt .Errorf ("component has nil protocol" )
122
+ }
117
123
if c .protocol .Code != code {
118
124
return "" , ErrProtocolNotFound
119
125
}
120
126
return c .Value (), nil
121
127
}
122
128
123
129
func (c Component ) Protocol () Protocol {
124
- return c .protocol
130
+ if c .protocol == nil {
131
+ return Protocol {}
132
+ }
133
+ return * c .protocol
125
134
}
126
135
127
136
func (c Component ) RawValue () []byte {
@@ -138,6 +147,9 @@ func (c Component) Value() string {
138
147
}
139
148
140
149
func (c Component ) valueAndErr () (string , error ) {
150
+ if c .protocol == nil {
151
+ return "" , fmt .Errorf ("component has nil protocol" )
152
+ }
141
153
if c .protocol .Transcoder == nil {
142
154
return "" , nil
143
155
}
@@ -157,6 +169,9 @@ func (c Component) String() string {
157
169
// writeTo is an efficient, private function for string-formatting a multiaddr.
158
170
// Trust me, we tend to allocate a lot when doing this.
159
171
func (c Component ) writeTo (b * strings.Builder ) {
172
+ if c .protocol == nil {
173
+ return
174
+ }
160
175
b .WriteByte ('/' )
161
176
b .WriteString (c .protocol .Name )
162
177
value := c .Value ()
@@ -188,6 +203,11 @@ func NewComponent(protocol, value string) (Component, error) {
188
203
}
189
204
190
205
func newComponent (protocol Protocol , bvalue []byte ) (Component , error ) {
206
+ protocolPtr := protocolPtrByCode [protocol .Code ]
207
+ if protocolPtr == nil {
208
+ protocolPtr = & protocol
209
+ }
210
+
191
211
size := len (bvalue )
192
212
size += len (protocol .VCode )
193
213
if protocol .Size < 0 {
@@ -209,7 +229,7 @@ func newComponent(protocol Protocol, bvalue []byte) (Component, error) {
209
229
return validateComponent (
210
230
Component {
211
231
bytes : string (maddr ),
212
- protocol : protocol ,
232
+ protocol : protocolPtr ,
213
233
valueStartIdx : offset ,
214
234
})
215
235
}
@@ -218,6 +238,9 @@ func newComponent(protocol Protocol, bvalue []byte) (Component, error) {
218
238
// It ensures that we will be able to call all methods on Component without
219
239
// error.
220
240
func validateComponent (c Component ) (Component , error ) {
241
+ if c .protocol == nil {
242
+ return Component {}, fmt .Errorf ("component is missing its protocol" )
243
+ }
221
244
if c .valueStartIdx > len (c .bytes ) {
222
245
return Component {}, fmt .Errorf ("component valueStartIdx is greater than the length of the component's bytes" )
223
246
}
0 commit comments