Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: Add Multiaddr.AppendComponent
  • Loading branch information
MarcoPolo committed Feb 24, 2025
commit c4d0b7843c27b52b3a1d1af2bc41000dbeb35437
12 changes: 12 additions & 0 deletions multiaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,18 @@ func (m Multiaddr) AsMultiaddr() Multiaddr {
return m
}

// AppendComponent is the same as using `append(m, *c)`, but with a safety check
// for a nil Component.
func (m Multiaddr) AppendComponent(cs ...*Component) Multiaddr {
for _, c := range cs {
if c == nil {
continue
}
m = append(m, *c)
}
return m
}

// Encapsulate wraps a given Multiaddr, returning the resulting joined Multiaddr
func (m Multiaddr) Encapsulate(other asMultiaddr) Multiaddr {
return Join(m, other)
Expand Down
11 changes: 11 additions & 0 deletions multiaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,17 @@ func assertValueForProto(t *testing.T, a Multiaddr, p int, exp string) {
}
}

func TestAppendComponent(t *testing.T) {
var m Multiaddr
res := m.AppendComponent(nil)
require.Equal(t, m, res)

c, err := NewComponent("ip4", "127.0.0.1")
require.NoError(t, err)
res = m.AppendComponent(c)
require.Equal(t, "/ip4/127.0.0.1", res.String())
}

func TestGetValue(t *testing.T) {
a := newMultiaddr(t, "/ip4/127.0.0.1/utp/tcp/5555/udp/1234/tls/utp/ipfs/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP")
assertValueForProto(t, a, P_IP4, "127.0.0.1")
Expand Down