From 6ab955f93ad88ff130d5405f44fd4f5288ba57d3 Mon Sep 17 00:00:00 2001 From: James Nugent Date: Sat, 31 Jan 2015 10:26:38 -0500 Subject: [PATCH 1/2] Add method on entities for concatenating locations This eliminates some repetition in the location client by implementing Stringer on a LocationList as a comma separated list of the location names. --- clients/locationClient/entities.go | 13 +++++++++++++ clients/locationClient/locationClient.go | 16 ++-------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/clients/locationClient/entities.go b/clients/locationClient/entities.go index 03aa7102bd79..52ce5acbb8fd 100644 --- a/clients/locationClient/entities.go +++ b/clients/locationClient/entities.go @@ -1,7 +1,10 @@ package locationClient import ( + "bytes" "encoding/xml" + "fmt" + "strings" ) type LocationList struct { @@ -17,3 +20,13 @@ type Location struct { WebWorkerRoleSizes []string `xml:"ComputeCapabilities>WebWorkerRoleSizes>RoleSize"` VirtualMachineRoleSizes []string `xml:"ComputeCapabilities>VirtualMachinesRoleSizes>RoleSize"` } + +func (locationList LocationList) String() string { + var buf bytes.Buffer + + for _, location := range locationList.Locations { + buf.WriteString(fmt.Sprintf("%s, ", location.Name)) + } + + return strings.Trim(buf.String(), ", ") +} diff --git a/clients/locationClient/locationClient.go b/clients/locationClient/locationClient.go index 12489b6ec84b..566a54a530ac 100644 --- a/clients/locationClient/locationClient.go +++ b/clients/locationClient/locationClient.go @@ -1,12 +1,10 @@ package locationClient import ( - "bytes" "encoding/xml" "errors" "fmt" azure "github.com/MSOpenTech/azure-sdk-for-go" - "strings" ) const ( @@ -32,12 +30,7 @@ func ResolveLocation(location string) error { return nil } - var availableLocations bytes.Buffer - for _, existingLocation := range locations.Locations { - availableLocations.WriteString(existingLocation.Name + ", ") - } - - return errors.New(fmt.Sprintf(invalidLocationError, location, strings.Trim(availableLocations.String(), ", "))) + return errors.New(fmt.Sprintf(invalidLocationError, location, locations.String())) } func GetLocationList() (LocationList, error) { @@ -74,10 +67,5 @@ func GetLocation(location string) (*Location, error) { return &existingLocation, nil } - var availableLocations bytes.Buffer - for _, existingLocation := range locations.Locations { - availableLocations.WriteString(existingLocation.Name + ", ") - } - - return nil, errors.New(fmt.Sprintf(invalidLocationError, location, strings.Trim(availableLocations.String(), ", "))) + return nil, errors.New(fmt.Sprintf(invalidLocationError, location, locations.String())) } From 9a65c0c5e4f81851f021ecb25ea286a967f3620d Mon Sep 17 00:00:00 2001 From: James Nugent Date: Sun, 1 Feb 2015 12:24:48 -0500 Subject: [PATCH 2/2] Remove unnecessary calls to .String() --- clients/locationClient/locationClient.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clients/locationClient/locationClient.go b/clients/locationClient/locationClient.go index 566a54a530ac..3b58bfc09779 100644 --- a/clients/locationClient/locationClient.go +++ b/clients/locationClient/locationClient.go @@ -30,7 +30,7 @@ func ResolveLocation(location string) error { return nil } - return errors.New(fmt.Sprintf(invalidLocationError, location, locations.String())) + return errors.New(fmt.Sprintf(invalidLocationError, location, locations)) } func GetLocationList() (LocationList, error) { @@ -67,5 +67,5 @@ func GetLocation(location string) (*Location, error) { return &existingLocation, nil } - return nil, errors.New(fmt.Sprintf(invalidLocationError, location, locations.String())) + return nil, errors.New(fmt.Sprintf(invalidLocationError, location, locations)) }