Skip to content

Commit

Permalink
Merge pull request #159 from multiformats/remove-deprecated-netcodec
Browse files Browse the repository at this point in the history
remove deprecated NetCodec
  • Loading branch information
Stebalien authored Jul 23, 2021
2 parents 80c8541 + 5e9a3ae commit 87b2941
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 71 deletions.
45 changes: 0 additions & 45 deletions net/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,39 +44,6 @@ func NewCodecMap() *CodecMap {
}
}

// NetCodec is used to identify a network codec, that is, a network type for
// which we are able to translate multiaddresses into standard Go net.Addr
// and back.
//
// Deprecated: Unfortunately, these mappings aren't one to one. This abstraction
// assumes that multiple "networks" can map to a single multiaddr protocol but
// not the reverse. For example, this abstraction supports `tcp6, tcp4, tcp ->
// /tcp/` really well but doesn't support `ip -> {/ip4/, /ip6/}`.
//
// Please use `RegisterFromNetAddr` and `RegisterToNetAddr` directly.
type NetCodec struct {
// NetAddrNetworks is an array of strings that may be returned
// by net.Addr.Network() calls on addresses belonging to this type
NetAddrNetworks []string

// ProtocolName is the string value for Multiaddr address keys
ProtocolName string

// ParseNetAddr parses a net.Addr belonging to this type into a multiaddr
ParseNetAddr FromNetAddrFunc

// ConvertMultiaddr converts a multiaddr of this type back into a net.Addr
ConvertMultiaddr ToNetAddrFunc

// Protocol returns the multiaddr protocol struct for this type
Protocol ma.Protocol
}

// RegisterNetCodec adds a new NetCodec to the default codecs.
func RegisterNetCodec(a *NetCodec) {
defaultCodecs.RegisterNetCodec(a)
}

// RegisterFromNetAddr registers a conversion from net.Addr instances to multiaddrs.
func RegisterFromNetAddr(from FromNetAddrFunc, networks ...string) {
defaultCodecs.RegisterFromNetAddr(from, networks...)
Expand All @@ -87,18 +54,6 @@ func RegisterToNetAddr(to ToNetAddrFunc, protocols ...string) {
defaultCodecs.RegisterToNetAddr(to, protocols...)
}

// RegisterNetCodec adds a new NetCodec to the CodecMap. This function is
// thread safe.
func (cm *CodecMap) RegisterNetCodec(a *NetCodec) {
cm.lk.Lock()
defer cm.lk.Unlock()
for _, n := range a.NetAddrNetworks {
cm.addrParsers[n] = a.ParseNetAddr
}

cm.maddrParsers[a.ProtocolName] = a.ConvertMultiaddr
}

// RegisterFromNetAddr registers a conversion from net.Addr instances to multiaddrs
func (cm *CodecMap) RegisterFromNetAddr(from FromNetAddrFunc, networks ...string) {
cm.lk.Lock()
Expand Down
44 changes: 18 additions & 26 deletions net/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,36 @@ import (
ma "github.com/multiformats/go-multiaddr"
)

func TestRegisterSpec(t *testing.T) {
func TestRegisterFrom(t *testing.T) {
cm := NewCodecMap()
myproto := &NetCodec{
ProtocolName: "test",
NetAddrNetworks: []string{"test", "iptest", "blahtest"},
ConvertMultiaddr: func(a ma.Multiaddr) (net.Addr, error) { return nil, nil },
ParseNetAddr: func(a net.Addr) (ma.Multiaddr, error) { return nil, nil },
}

cm.RegisterNetCodec(myproto)

_, ok := cm.addrParsers["test"]
if !ok {
cm.RegisterFromNetAddr(
func(a net.Addr) (ma.Multiaddr, error) { return nil, nil },
"test", "iptest", "blahtest",
)
if _, ok := cm.addrParsers["test"]; !ok {
t.Fatal("myproto not properly registered")
}

_, ok = cm.addrParsers["iptest"]
if !ok {
if _, ok := cm.addrParsers["iptest"]; !ok {
t.Fatal("myproto not properly registered")
}

_, ok = cm.addrParsers["blahtest"]
if !ok {
if _, ok := cm.addrParsers["blahtest"]; !ok {
t.Fatal("myproto not properly registered")
}
}

_, ok = cm.maddrParsers["test"]
if !ok {
func TestRegisterTo(t *testing.T) {
cm := NewCodecMap()
cm.RegisterToNetAddr(
func(a ma.Multiaddr) (net.Addr, error) { return nil, nil },
"test", "iptest", "blahtest",
)
if _, ok := cm.maddrParsers["test"]; !ok {
t.Fatal("myproto not properly registered")
}

_, ok = cm.maddrParsers["iptest"]
if ok {
if _, ok := cm.maddrParsers["iptest"]; !ok {
t.Fatal("myproto not properly registered")
}

_, ok = cm.maddrParsers["blahtest"]
if ok {
if _, ok := cm.maddrParsers["blahtest"]; !ok {
t.Fatal("myproto not properly registered")
}
}

0 comments on commit 87b2941

Please sign in to comment.