Skip to content

Commit

Permalink
Create NewTranscoderFromFunctions function and use it
Browse files Browse the repository at this point in the history
  • Loading branch information
Kubuxu committed Nov 2, 2016
1 parent 1919305 commit 72b4a6c
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions transcoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ type Transcoder interface {
BytesToString([]byte) (string, error)
}

func NewTranscoderFromFunctions(s2b func(string) ([]byte, error),
b2s func([]byte) (string, error)) Transcoder {

return twrp{s2b, b2s}
}

type twrp struct {
strtobyte func(string) ([]byte, error)
bytetostr func([]byte) (string, error)
Expand All @@ -29,8 +35,8 @@ func (t twrp) BytesToString(b []byte) (string, error) {
return t.bytetostr(b)
}

var TranscoderIP4 = (Transcoder)(twrp{ip4StB, ipBtS})
var TranscoderIP6 = (Transcoder)(twrp{ip6StB, ipBtS})
var TranscoderIP4 = NewTranscoderFromFunctions(ip4StB, ipBtS)
var TranscoderIP6 = NewTranscoderFromFunctions(ip6StB, ipBtS)

func ip4StB(s string) ([]byte, error) {
i := net.ParseIP(s).To4()
Expand All @@ -52,7 +58,7 @@ func ipBtS(b []byte) (string, error) {
return net.IP(b).String(), nil
}

var TranscoderPort = (Transcoder)(twrp{portStB, portBtS})
var TranscoderPort = NewTranscoderFromFunctions(portStB, portBtS)

func portStB(s string) ([]byte, error) {
i, err := strconv.Atoi(s)
Expand All @@ -72,7 +78,7 @@ func portBtS(b []byte) (string, error) {
return strconv.Itoa(int(i)), nil
}

var TranscoderOnion = (Transcoder)(twrp{onionStB, onionBtS})
var TranscoderOnion = NewTranscoderFromFunctions(onionStB, onionBtS)

func onionStB(s string) ([]byte, error) {
addr := strings.Split(s, ":")
Expand Down Expand Up @@ -115,7 +121,7 @@ func onionBtS(b []byte) (string, error) {
return addr + ":" + strconv.Itoa(int(port)), nil
}

var TranscoderIPFS = (Transcoder)(twrp{ipfsStB, ipfsBtS})
var TranscoderIPFS = NewTranscoderFromFunctions(ipfsStB, ipfsBtS)

func ipfsStB(s string) ([]byte, error) {
// the address is a varint prefixed multihash string representation
Expand Down Expand Up @@ -146,7 +152,7 @@ func ipfsBtS(b []byte) (string, error) {
return m.B58String(), nil
}

var TranscoderUnix = (Transcoder)(twrp{unixStB, unixBtS})
var TranscoderUnix = NewTranscoderFromFunctions(unixStB, unixBtS)

func unixStB(s string) ([]byte, error) {
// the address is the whole remaining string, prefixed by a varint len
Expand Down

0 comments on commit 72b4a6c

Please sign in to comment.