add a type for ClientIDs

This commit is contained in:
Jimmy Zelinskie 2016-08-04 00:33:55 -04:00
parent 0d054414ab
commit 0ebadd31d0
2 changed files with 9 additions and 8 deletions

View file

@ -14,9 +14,13 @@
package bittorrent package bittorrent
// NewClientID returns the part of a PeerID that identifies a peer's client // ClientID represents the part of a PeerID that identifies a Peer's client
// software. // software.
func NewClientID(peerID string) (clientID string) { type ClientID string
// NewClientID parses a ClientID from a PeerID.
func NewClientID(peerID string) ClientID {
var clientID string
length := len(peerID) length := len(peerID)
if length >= 6 { if length >= 6 {
if peerID[0] == '-' { if peerID[0] == '-' {
@ -28,5 +32,5 @@ func NewClientID(peerID string) (clientID string) {
} }
} }
return return ClientID(clientID)
} }

View file

@ -17,10 +17,7 @@ package bittorrent
import "testing" import "testing"
func TestClientID(t *testing.T) { func TestClientID(t *testing.T) {
var clientTable = []struct { var clientTable = []struct{ peerID, clientID string }{
peerID string
clientID string
}{
{"-AZ3034-6wfG2wk6wWLc", "AZ3034"}, {"-AZ3034-6wfG2wk6wWLc", "AZ3034"},
{"-AZ3042-6ozMq5q6Q3NX", "AZ3042"}, {"-AZ3042-6ozMq5q6Q3NX", "AZ3042"},
{"-BS5820-oy4La2MWGEFj", "BS5820"}, {"-BS5820-oy4La2MWGEFj", "BS5820"},
@ -65,7 +62,7 @@ func TestClientID(t *testing.T) {
} }
for _, tt := range clientTable { for _, tt := range clientTable {
if parsedID := NewClientID(tt.peerID); parsedID != tt.clientID { if parsedID := NewClientID(tt.peerID); parsedID != ClientID(tt.clientID) {
t.Error("Incorrectly parsed peer ID", tt.peerID, "as", parsedID) t.Error("Incorrectly parsed peer ID", tt.peerID, "as", parsedID)
} }
} }