From 029fe6bf4727ed184604e787e876f202882cac87 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Mon, 9 May 2016 05:27:11 -0400 Subject: [PATCH 1/3] pkg/types: Build a urls map from a string map This adds a simple transformation function which is helpful when manipulating the different etcd internal data representations. --- pkg/types/urlsmap.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pkg/types/urlsmap.go b/pkg/types/urlsmap.go index e2579e2ca..47690cc38 100644 --- a/pkg/types/urlsmap.go +++ b/pkg/types/urlsmap.go @@ -40,6 +40,20 @@ func NewURLsMap(s string) (URLsMap, error) { return cl, nil } +// NewURLsMapFromStringMap takes a map of strings and returns a URLsMap. The +// string values in the map can be multiple values separated by the sep string. +func NewURLsMapFromStringMap(m map[string]string, sep string) (URLsMap, error) { + var err error + um := URLsMap{} + for k, v := range m { + um[k], err = NewURLs(strings.Split(v, sep)) + if err != nil { + return nil, err + } + } + return um, nil +} + // String turns URLsMap into discovery-formatted name-to-URLs sorted by name. func (c URLsMap) String() string { var pairs []string From 17e23769d97fd6334b9a2154fe7fa76874d5bba8 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Wed, 11 May 2016 10:46:46 -0400 Subject: [PATCH 2/3] pkg/types: gofmt existing code --- pkg/types/urlsmap_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/types/urlsmap_test.go b/pkg/types/urlsmap_test.go index 6962f393d..589e8fd5d 100644 --- a/pkg/types/urlsmap_test.go +++ b/pkg/types/urlsmap_test.go @@ -15,10 +15,9 @@ package types import ( + "github.com/coreos/etcd/pkg/testutil" "reflect" "testing" - - "github.com/coreos/etcd/pkg/testutil" ) func TestParseInitialCluster(t *testing.T) { From d41ce0a97c5cb2bd2df353763b0f7ded1c4aed6b Mon Sep 17 00:00:00 2001 From: James Shubin Date: Wed, 11 May 2016 10:48:27 -0400 Subject: [PATCH 3/3] pkg/types: Add tests for NewURLsMapFromStringMap --- pkg/types/urlsmap_test.go | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/pkg/types/urlsmap_test.go b/pkg/types/urlsmap_test.go index 589e8fd5d..6f58765c0 100644 --- a/pkg/types/urlsmap_test.go +++ b/pkg/types/urlsmap_test.go @@ -112,3 +112,44 @@ func TestNewURLsMapIPV6(t *testing.T) { t.Errorf("cluster = %#v, want %#v", c, wc) } } + +func TestNewURLsMapFromStringMapEmpty(t *testing.T) { + mss := make(map[string]string) + urlsMap, err := NewURLsMapFromStringMap(mss, ",") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + s := "" + um, err := NewURLsMap(s) + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + if um.String() != urlsMap.String() { + t.Errorf("Expected:\n%+v\ngot:\n%+v", um, urlsMap) + } +} + +func TestNewURLsMapFromStringMapNormal(t *testing.T) { + mss := make(map[string]string) + mss["host0"] = "http://127.0.0.1:2379,http://127.0.0.1:2380" + mss["host1"] = "http://127.0.0.1:2381,http://127.0.0.1:2382" + mss["host2"] = "http://127.0.0.1:2383,http://127.0.0.1:2384" + mss["host3"] = "http://127.0.0.1:2385,http://127.0.0.1:2386" + urlsMap, err := NewURLsMapFromStringMap(mss, ",") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + s := "host0=http://127.0.0.1:2379,host0=http://127.0.0.1:2380," + + "host1=http://127.0.0.1:2381,host1=http://127.0.0.1:2382," + + "host2=http://127.0.0.1:2383,host2=http://127.0.0.1:2384," + + "host3=http://127.0.0.1:2385,host3=http://127.0.0.1:2386" + um, err := NewURLsMap(s) + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + if um.String() != urlsMap.String() { + t.Errorf("Expected:\n%+v\ngot:\n%+v", um, urlsMap) + } +}