@@ -15,7 +15,7 @@ type Component struct {
1515 // bytes is the raw bytes of the component. It includes the protocol code as
1616 // varint, possibly the size of the value, and the value.
1717 bytes string // string for immutability.
18- protocol Protocol
18+ protocol * Protocol
1919 valueStartIdx int // Index of the first byte of the Component's value in the bytes array
2020}
2121
@@ -110,18 +110,27 @@ func (c Component) Compare(o Component) int {
110110}
111111
112112func (c Component ) Protocols () []Protocol {
113- return []Protocol {c .protocol }
113+ if c .protocol == nil {
114+ return nil
115+ }
116+ return []Protocol {* c .protocol }
114117}
115118
116119func (c Component ) ValueForProtocol (code int ) (string , error ) {
120+ if c .protocol == nil {
121+ return "" , fmt .Errorf ("component has nil protocol" )
122+ }
117123 if c .protocol .Code != code {
118124 return "" , ErrProtocolNotFound
119125 }
120126 return c .Value (), nil
121127}
122128
123129func (c Component ) Protocol () Protocol {
124- return c .protocol
130+ if c .protocol == nil {
131+ return Protocol {}
132+ }
133+ return * c .protocol
125134}
126135
127136func (c Component ) RawValue () []byte {
@@ -138,6 +147,9 @@ func (c Component) Value() string {
138147}
139148
140149func (c Component ) valueAndErr () (string , error ) {
150+ if c .protocol == nil {
151+ return "" , fmt .Errorf ("component has nil protocol" )
152+ }
141153 if c .protocol .Transcoder == nil {
142154 return "" , nil
143155 }
@@ -157,6 +169,9 @@ func (c Component) String() string {
157169// writeTo is an efficient, private function for string-formatting a multiaddr.
158170// Trust me, we tend to allocate a lot when doing this.
159171func (c Component ) writeTo (b * strings.Builder ) {
172+ if c .protocol == nil {
173+ return
174+ }
160175 b .WriteByte ('/' )
161176 b .WriteString (c .protocol .Name )
162177 value := c .Value ()
@@ -188,6 +203,11 @@ func NewComponent(protocol, value string) (Component, error) {
188203}
189204
190205func newComponent (protocol Protocol , bvalue []byte ) (Component , error ) {
206+ protocolPtr := protocolPtrByCode [protocol .Code ]
207+ if protocolPtr == nil {
208+ protocolPtr = & protocol
209+ }
210+
191211 size := len (bvalue )
192212 size += len (protocol .VCode )
193213 if protocol .Size < 0 {
@@ -209,7 +229,7 @@ func newComponent(protocol Protocol, bvalue []byte) (Component, error) {
209229 return validateComponent (
210230 Component {
211231 bytes : string (maddr ),
212- protocol : protocol ,
232+ protocol : protocolPtr ,
213233 valueStartIdx : offset ,
214234 })
215235}
@@ -218,6 +238,9 @@ func newComponent(protocol Protocol, bvalue []byte) (Component, error) {
218238// It ensures that we will be able to call all methods on Component without
219239// error.
220240func validateComponent (c Component ) (Component , error ) {
241+ if c .protocol == nil {
242+ return Component {}, fmt .Errorf ("component is missing its protocol" )
243+ }
221244 if c .valueStartIdx > len (c .bytes ) {
222245 return Component {}, fmt .Errorf ("component valueStartIdx is greater than the length of the component's bytes" )
223246 }
0 commit comments