From a1719623280b2761f47e6e38fbc0998fa789e998 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Carvalho?= Date: Mon, 22 Aug 2016 09:29:51 -0300 Subject: [PATCH] driverutil: refactor splitPortProto to parse port as str MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: André Carvalho --- drivers/driverutil/util.go | 13 +++---------- drivers/driverutil/util_test.go | 9 ++++----- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/drivers/driverutil/util.go b/drivers/driverutil/util.go index cb43006340..cf36fc5a46 100644 --- a/drivers/driverutil/util.go +++ b/drivers/driverutil/util.go @@ -1,23 +1,16 @@ package driverutil -import ( - "fmt" - "strconv" - "strings" -) +import "strings" // SplitPortProto splits a string in the format port/protocol, defaulting // protocol to "tcp" if not provided. -func SplitPortProto(raw string) (port int, protocol string, err error) { +func SplitPortProto(raw string) (port string, protocol string, err error) { parts := strings.Split(raw, "/") if len(parts) == 1 { protocol = "tcp" } else { protocol = parts[1] } - port, err = strconv.Atoi(parts[0]) - if err != nil { - return 0, "", fmt.Errorf("invalid port number %s: %s", parts[0], err) - } + port = parts[0] return port, protocol, nil } diff --git a/drivers/driverutil/util_test.go b/drivers/driverutil/util_test.go index c26d533d76..58f1a3a7fb 100644 --- a/drivers/driverutil/util_test.go +++ b/drivers/driverutil/util_test.go @@ -9,14 +9,13 @@ import ( func TestSplitPortProtocol(t *testing.T) { tests := []struct { raw string - expectedPort int + expectedPort string expectedProto string expectedErr bool }{ - {"8080/tcp", 8080, "tcp", false}, - {"90/udp", 90, "udp", false}, - {"80", 80, "tcp", false}, - {"abc", 0, "", true}, + {"8080/tcp", "8080", "tcp", false}, + {"90/udp", "90", "udp", false}, + {"80", "80", "tcp", false}, } for _, tc := range tests {