Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 8803f83

Browse files
author
Jamie Hannaford
committed
Fixing minor issues such as weakly typed decoding
1 parent 1110840 commit 8803f83

File tree

8 files changed

+30
-22
lines changed

8 files changed

+30
-22
lines changed

openstack/db/v1/flavors/fixtures.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package flavors
22

33
import (
44
"fmt"
5-
"strconv"
65
"testing"
76

87
"github.com/rackspace/gophercloud/testhelper/fixture"
@@ -27,9 +26,9 @@ const flavor = `
2726
`
2827

2928
var (
30-
flavorID = 1
29+
flavorID = "{flavorID}"
3130
_baseURL = "/flavors"
32-
resURL = "/flavors/" + strconv.Itoa(flavorID)
31+
resURL = "/flavors/" + flavorID
3332
)
3433

3534
var (

openstack/db/v1/flavors/requests.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func List(client *gophercloud.ServiceClient) pagination.Pager {
1717
}
1818

1919
// Get will retrieve information for a specified hardware flavor.
20-
func Get(client *gophercloud.ServiceClient, id int) GetResult {
20+
func Get(client *gophercloud.ServiceClient, id string) GetResult {
2121
var gr GetResult
2222

2323
_, gr.Err = client.Request("GET", getURL(client, id), gophercloud.RequestOpts{

openstack/db/v1/flavors/results.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ func (gr GetResult) Extract() (*Flavor, error) {
2121
Flavor Flavor `mapstructure:"flavor"`
2222
}
2323

24-
err := mapstructure.Decode(gr.Body, &result)
24+
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
25+
WeaklyTypedInput: true,
26+
Result: &result,
27+
})
28+
29+
err = decoder.Decode(gr.Body)
2530
return &result.Flavor, err
2631
}
2732

@@ -76,6 +81,12 @@ func ExtractFlavors(page pagination.Page) ([]Flavor, error) {
7681
Flavors []Flavor `mapstructure:"flavors"`
7782
}
7883

79-
err := mapstructure.Decode(casted, &container)
84+
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
85+
WeaklyTypedInput: true,
86+
Result: &container,
87+
})
88+
89+
err = decoder.Decode(casted)
90+
8091
return container.Flavors, err
8192
}

openstack/db/v1/flavors/urls.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
package flavors
22

3-
import (
4-
"strconv"
3+
import "github.com/rackspace/gophercloud"
54

6-
"github.com/rackspace/gophercloud"
7-
)
8-
9-
func getURL(client *gophercloud.ServiceClient, id int) string {
10-
return client.ServiceURL("flavors", strconv.Itoa(id))
5+
func getURL(client *gophercloud.ServiceClient, id string) string {
6+
return client.ServiceURL("flavors", id)
117
}
128

139
func listURL(client *gophercloud.ServiceClient) string {

rackspace/db/v1/flavors/delegate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ func List(client *gophercloud.ServiceClient) pagination.Pager {
1212
}
1313

1414
// Get retrieves the details for a particular flavor.
15-
func Get(client *gophercloud.ServiceClient, flavorID int) os.GetResult {
15+
func Get(client *gophercloud.ServiceClient, flavorID string) os.GetResult {
1616
return os.Get(client, flavorID)
1717
}

rackspace/db/v1/flavors/delegate_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestListFlavors(t *testing.T) {
2626

2727
expected := []os.Flavor{
2828
os.Flavor{
29-
ID: 1,
29+
ID: "1",
3030
Name: "m1.tiny",
3131
RAM: 512,
3232
Links: []gophercloud.Link{
@@ -35,7 +35,7 @@ func TestListFlavors(t *testing.T) {
3535
},
3636
},
3737
os.Flavor{
38-
ID: 2,
38+
ID: "2",
3939
Name: "m1.small",
4040
RAM: 1024,
4141
Links: []gophercloud.Link{
@@ -44,7 +44,7 @@ func TestListFlavors(t *testing.T) {
4444
},
4545
},
4646
os.Flavor{
47-
ID: 3,
47+
ID: "3",
4848
Name: "m1.medium",
4949
RAM: 2048,
5050
Links: []gophercloud.Link{
@@ -53,7 +53,7 @@ func TestListFlavors(t *testing.T) {
5353
},
5454
},
5555
os.Flavor{
56-
ID: 4,
56+
ID: "4",
5757
Name: "m1.large",
5858
RAM: 4096,
5959
Links: []gophercloud.Link{
@@ -83,7 +83,7 @@ func TestGetFlavor(t *testing.T) {
8383
th.AssertNoErr(t, err)
8484

8585
expected := &os.Flavor{
86-
ID: 1,
86+
ID: "1",
8787
Name: "m1.tiny",
8888
RAM: 512,
8989
Links: []gophercloud.Link{

rackspace/db/v1/instances/delegate_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/rackspace/gophercloud"
77
osDBs "github.com/rackspace/gophercloud/openstack/db/v1/databases"
8+
"github.com/rackspace/gophercloud/openstack/db/v1/flavors"
89
os "github.com/rackspace/gophercloud/openstack/db/v1/instances"
910
osUsers "github.com/rackspace/gophercloud/openstack/db/v1/users"
1011
"github.com/rackspace/gophercloud/rackspace/db/v1/datastores"
@@ -23,7 +24,7 @@ var expectedInstance = &Instance{
2324
Created: "2014-02-13T21:47:13",
2425
Updated: "2014-02-13T21:47:13",
2526
Datastore: datastores.DatastorePartial{Type: "mysql", Version: "5.6"},
26-
Flavor: os.Flavor{
27+
Flavor: flavors.Flavor{
2728
ID: "1",
2829
Links: []gophercloud.Link{
2930
gophercloud.Link{Href: "https://ord.databases.api.rackspacecloud.com/v1.0/1234/flavors/1", Rel: "self"},

rackspace/db/v1/instances/requests_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/rackspace/gophercloud"
7+
"github.com/rackspace/gophercloud/openstack/db/v1/flavors"
78
os "github.com/rackspace/gophercloud/openstack/db/v1/instances"
89
"github.com/rackspace/gophercloud/pagination"
910
"github.com/rackspace/gophercloud/rackspace/db/v1/backups"
@@ -156,7 +157,7 @@ func TestListReplicas(t *testing.T) {
156157
ID: "3c691f06-bf9a-4618-b7ec-2817ce0cf254",
157158
IP: []string{"10.0.0.3"},
158159
Volume: os.Volume{Size: 1},
159-
Flavor: os.Flavor{ID: "9"},
160+
Flavor: flavors.Flavor{ID: "9"},
160161
Datastore: datastores.DatastorePartial{Version: "5.6", Type: "mysql"},
161162
ReplicaOf: &Instance{
162163
ID: "8b499b45-52d6-402d-b398-f9d8f279c69a",
@@ -197,7 +198,7 @@ func TestGetReplica(t *testing.T) {
197198
Used: 0.54,
198199
Size: 1,
199200
},
200-
Flavor: os.Flavor{ID: "9"},
201+
Flavor: flavors.Flavor{ID: "9"},
201202
Datastore: datastores.DatastorePartial{
202203
Version: "5.6",
203204
Type: "mysql",

0 commit comments

Comments
 (0)