From 0ebadd31d0e42bf77a2b3c32a47af56e46d93f9d Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Thu, 4 Aug 2016 00:33:55 -0400 Subject: [PATCH] add a type for ClientIDs --- bittorrent/client_id.go | 10 +++++++--- bittorrent/client_id_test.go | 7 ++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/bittorrent/client_id.go b/bittorrent/client_id.go index 4089639..6aab52c 100644 --- a/bittorrent/client_id.go +++ b/bittorrent/client_id.go @@ -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) } diff --git a/bittorrent/client_id_test.go b/bittorrent/client_id_test.go index 699da3e..956d0fc 100644 --- a/bittorrent/client_id_test.go +++ b/bittorrent/client_id_test.go @@ -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) } }