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

Commit 87704ba

Browse files
author
Jamie Hannaford
committed
use better type assertions
1 parent c70720d commit 87704ba

File tree

4 files changed

+58
-26
lines changed

4 files changed

+58
-26
lines changed

openstack/db/v1/configurations/results.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package configurations
22

33
import (
4+
"fmt"
5+
"reflect"
46
"time"
57

68
"github.com/mitchellh/mapstructure"
@@ -43,12 +45,18 @@ func ExtractConfigs(page pagination.Page) ([]Config, error) {
4345
Configs []Config `mapstructure:"configurations" json:"configurations"`
4446
}
4547

46-
err := mapstructure.Decode(casted, &resp)
48+
if err := mapstructure.Decode(casted, &resp); err != nil {
49+
return nil, err
50+
}
4751

4852
var vals []interface{}
49-
switch (casted).(type) {
50-
case interface{}:
53+
switch casted.(type) {
54+
case map[string]interface{}:
5155
vals = casted.(map[string]interface{})["configurations"].([]interface{})
56+
case map[string][]interface{}:
57+
vals = casted.(map[string][]interface{})["configurations"]
58+
default:
59+
return resp.Configs, fmt.Errorf("Unknown type: %v", reflect.TypeOf(casted))
5260
}
5361

5462
for i, v := range vals {
@@ -71,7 +79,7 @@ func ExtractConfigs(page pagination.Page) ([]Config, error) {
7179
}
7280
}
7381

74-
return resp.Configs, err
82+
return resp.Configs, nil
7583
}
7684

7785
type commonResult struct {

openstack/db/v1/instances/results.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package instances
22

33
import (
4+
"fmt"
5+
"reflect"
46
"time"
57

68
"github.com/mitchellh/mapstructure"
@@ -142,16 +144,22 @@ func (page InstancePage) NextPageURL() (string, error) {
142144
func ExtractInstances(page pagination.Page) ([]Instance, error) {
143145
casted := page.(InstancePage).Body
144146

145-
var response struct {
147+
var resp struct {
146148
Instances []Instance `mapstructure:"instances"`
147149
}
148150

149-
err := mapstructure.Decode(casted, &response)
151+
if err := mapstructure.Decode(casted, &resp); err != nil {
152+
return nil, err
153+
}
150154

151155
var vals []interface{}
152-
switch (casted).(type) {
153-
case interface{}:
156+
switch casted.(type) {
157+
case map[string]interface{}:
154158
vals = casted.(map[string]interface{})["instances"].([]interface{})
159+
case map[string][]interface{}:
160+
vals = casted.(map[string][]interface{})["instances"]
161+
default:
162+
return resp.Instances, fmt.Errorf("Unknown type: %v", reflect.TypeOf(casted))
155163
}
156164

157165
for i, v := range vals {
@@ -160,21 +168,21 @@ func ExtractInstances(page pagination.Page) ([]Instance, error) {
160168
if t, ok := val["created"].(string); ok && t != "" {
161169
creationTime, err := time.Parse(time.RFC3339, t)
162170
if err != nil {
163-
return response.Instances, err
171+
return resp.Instances, err
164172
}
165-
response.Instances[i].Created = creationTime
173+
resp.Instances[i].Created = creationTime
166174
}
167175

168176
if t, ok := val["updated"].(string); ok && t != "" {
169177
updatedTime, err := time.Parse(time.RFC3339, t)
170178
if err != nil {
171-
return response.Instances, err
179+
return resp.Instances, err
172180
}
173-
response.Instances[i].Updated = updatedTime
181+
resp.Instances[i].Updated = updatedTime
174182
}
175183
}
176184

177-
return response.Instances, err
185+
return resp.Instances, nil
178186
}
179187

180188
// UserRootResult represents the result of an operation to enable the root user.

rackspace/db/v1/backups/results.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package backups
22

33
import (
4+
"fmt"
5+
"reflect"
46
"time"
57

68
"github.com/mitchellh/mapstructure"
@@ -109,12 +111,18 @@ func ExtractBackups(page pagination.Page) ([]Backup, error) {
109111
Backups []Backup `mapstructure:"backups" json:"backups"`
110112
}
111113

112-
err := mapstructure.Decode(casted, &resp)
114+
if err := mapstructure.Decode(casted, &resp); err != nil {
115+
return nil, err
116+
}
113117

114118
var vals []interface{}
115-
switch (casted).(type) {
116-
case interface{}:
119+
switch casted.(type) {
120+
case map[string]interface{}:
117121
vals = casted.(map[string]interface{})["backups"].([]interface{})
122+
case map[string][]interface{}:
123+
vals = casted.(map[string][]interface{})["backups"]
124+
default:
125+
return resp.Backups, fmt.Errorf("Unknown type: %v", reflect.TypeOf(casted))
118126
}
119127

120128
for i, v := range vals {
@@ -137,5 +145,5 @@ func ExtractBackups(page pagination.Page) ([]Backup, error) {
137145
}
138146
}
139147

140-
return resp.Backups, err
148+
return resp.Backups, nil
141149
}

rackspace/db/v1/instances/results.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package instances
22

33
import (
4+
"fmt"
5+
"reflect"
46
"time"
57

68
"github.com/mitchellh/mapstructure"
@@ -147,16 +149,22 @@ type UpdateResult struct {
147149
func ExtractInstances(page pagination.Page) ([]Instance, error) {
148150
casted := page.(os.InstancePage).Body
149151

150-
var response struct {
152+
var resp struct {
151153
Instances []Instance `mapstructure:"instances"`
152154
}
153155

154-
err := mapstructure.Decode(casted, &response)
156+
if err := mapstructure.Decode(casted, &resp); err != nil {
157+
return nil, err
158+
}
155159

156160
var vals []interface{}
157-
switch (casted).(type) {
158-
case interface{}:
161+
switch casted.(type) {
162+
case map[string]interface{}:
159163
vals = casted.(map[string]interface{})["instances"].([]interface{})
164+
case map[string][]interface{}:
165+
vals = casted.(map[string][]interface{})["instances"]
166+
default:
167+
return resp.Instances, fmt.Errorf("Unknown type: %v", reflect.TypeOf(casted))
160168
}
161169

162170
for i, v := range vals {
@@ -165,19 +173,19 @@ func ExtractInstances(page pagination.Page) ([]Instance, error) {
165173
if t, ok := val["created"].(string); ok && t != "" {
166174
creationTime, err := time.Parse(time.RFC3339, t)
167175
if err != nil {
168-
return response.Instances, err
176+
return resp.Instances, err
169177
}
170-
response.Instances[i].Created = creationTime
178+
resp.Instances[i].Created = creationTime
171179
}
172180

173181
if t, ok := val["updated"].(string); ok && t != "" {
174182
updatedTime, err := time.Parse(time.RFC3339, t)
175183
if err != nil {
176-
return response.Instances, err
184+
return resp.Instances, err
177185
}
178-
response.Instances[i].Updated = updatedTime
186+
resp.Instances[i].Updated = updatedTime
179187
}
180188
}
181189

182-
return response.Instances, err
190+
return resp.Instances, nil
183191
}

0 commit comments

Comments
 (0)