Skip to content

Commit a717475

Browse files
authored
fix: sorting resources without locality when listing multiple localities (#1691)
1 parent ac10fe9 commit a717475

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

internal/generic/fields.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package generic
2+
3+
import "reflect"
4+
5+
// HasField returns true if given struct has a field with given name
6+
// Also allow a slice, it will use the underlying type
7+
func HasField(i interface{}, fieldName string) bool {
8+
value := reflect.Indirect(reflect.ValueOf(i))
9+
typ := value.Type()
10+
11+
if value.Kind() == reflect.Slice {
12+
typ = indirectType(typ.Elem())
13+
}
14+
15+
_, fieldExists := typ.FieldByName(fieldName)
16+
return fieldExists
17+
}

internal/generic/fields_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package generic
2+
3+
import "testing"
4+
5+
func TestHasField(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
i interface{}
9+
fieldName string
10+
want bool
11+
}{
12+
{
13+
"native type",
14+
struct {
15+
Zone string
16+
}{},
17+
"Zone",
18+
true,
19+
},
20+
{
21+
"ptr type",
22+
struct {
23+
Zone *string
24+
}{},
25+
"Zone",
26+
true,
27+
},
28+
{
29+
"invalid case",
30+
struct {
31+
Zone *string
32+
}{},
33+
"zone",
34+
false,
35+
},
36+
{
37+
"slice",
38+
[]struct {
39+
Zone string
40+
}{},
41+
"Zone",
42+
true,
43+
},
44+
{
45+
"slice invalid case",
46+
[]struct {
47+
Zone string
48+
}{},
49+
"zone",
50+
false,
51+
},
52+
}
53+
for _, tt := range tests {
54+
t.Run(tt.name, func(t *testing.T) {
55+
if got := HasField(tt.i, tt.fieldName); got != tt.want {
56+
t.Errorf("HasField() = %v, want %v", got, tt.want)
57+
}
58+
})
59+
}
60+
}

internal/generic/ptr.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package generic
2+
3+
import "reflect"
4+
5+
func indirectType(typ reflect.Type) reflect.Type {
6+
if typ.Kind() == reflect.Ptr {
7+
return typ.Elem()
8+
}
9+
10+
return typ
11+
}

scw/client.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,10 @@ func (c *Client) doListRegions(req *ScalewayRequest, res interface{}, regions []
472472

473473
// sortSliceByZones sorts a slice of struct using a Zone field that should exist
474474
func sortSliceByZones(list interface{}, zones []Zone) {
475+
if !generic.HasField(list, "Zone") {
476+
return
477+
}
478+
475479
zoneMap := map[Zone]int{}
476480
for i, zone := range zones {
477481
zoneMap[zone] = i
@@ -483,6 +487,10 @@ func sortSliceByZones(list interface{}, zones []Zone) {
483487

484488
// sortSliceByRegions sorts a slice of struct using a Region field that should exist
485489
func sortSliceByRegions(list interface{}, regions []Region) {
490+
if !generic.HasField(list, "Region") {
491+
return
492+
}
493+
486494
regionMap := map[Region]int{}
487495
for i, region := range regions {
488496
regionMap[region] = i

0 commit comments

Comments
 (0)