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
// 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.
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)
if length >= 6 {
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"
func TestClientID(t *testing.T) {
var clientTable = []struct {
peerID string
clientID string
}{
var clientTable = []struct{ peerID, clientID string }{
{"-AZ3034-6wfG2wk6wWLc", "AZ3034"},
{"-AZ3042-6ozMq5q6Q3NX", "AZ3042"},
{"-BS5820-oy4La2MWGEFj", "BS5820"},
@ -65,7 +62,7 @@ func TestClientID(t *testing.T) {
}
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)
}
}