@@ -11,9 +11,11 @@ import (
11
11
)
12
12
13
13
/*
14
- BuildRequestBody builds a map[string]interface from the given `struct`. If
15
- parent is not an empty string, the final map[string]interface returned will
16
- encapsulate the built one. For example:
14
+ BuildRequestBody builds a map[string]interface from the given `struct`, or
15
+ collection of `structs`. If parent is not an empty string, the final
16
+ map[string]interface returned will encapsulate the built one. Parent is
17
+ required when passing a list of `structs`.
18
+ For example:
17
19
18
20
disk := 1
19
21
createOpts := flavors.CreateOpts{
@@ -27,7 +29,29 @@ encapsulate the built one. For example:
27
29
28
30
body, err := gophercloud.BuildRequestBody(createOpts, "flavor")
29
31
30
- The above example can be run as-is, however it is recommended to look at how
32
+
33
+ opts := []rules.CreateOpts{
34
+ {
35
+ Direction: "ingress",
36
+ PortRangeMin: 80,
37
+ EtherType: rules.EtherType4,
38
+ PortRangeMax: 80,
39
+ Protocol: "tcp",
40
+ SecGroupID: "a7734e61-b545-452d-a3cd-0189cbd9747a",
41
+ },
42
+ {
43
+ Direction: "ingress",
44
+ PortRangeMin: 443,
45
+ EtherType: rules.EtherType4,
46
+ PortRangeMax: 443,
47
+ Protocol: "tcp",
48
+ SecGroupID: "a7734e61-b545-452d-a3cd-0189cbd9747a",
49
+ },
50
+ }
51
+
52
+ body, err := gophercloud.BuildRequestBody(opts, "security_group_rules")
53
+
54
+ The above examples can be run as-is, however it is recommended to look at how
31
55
BuildRequestBody is used within Gophercloud to more fully understand how it
32
56
fits within the request process as a whole rather than use it directly as shown
33
57
above.
@@ -44,7 +68,8 @@ func BuildRequestBody(opts any, parent string) (map[string]any, error) {
44
68
}
45
69
46
70
optsMap := make (map [string ]any )
47
- if optsValue .Kind () == reflect .Struct {
71
+ switch optsValue .Kind () {
72
+ case reflect .Struct :
48
73
//fmt.Printf("optsValue.Kind() is a reflect.Struct: %+v\n", optsValue.Kind())
49
74
for i := 0 ; i < optsValue .NumField (); i ++ {
50
75
v := optsValue .Field (i )
@@ -184,9 +209,22 @@ func BuildRequestBody(opts any, parent string) (map[string]any, error) {
184
209
}
185
210
//fmt.Printf("optsMap after parent added: %+v\n", optsMap)
186
211
return optsMap , nil
212
+ case reflect .Slice , reflect .Array :
213
+ optsMaps := make ([]map [string ]any , optsValue .Len ())
214
+ for i := 0 ; i < optsValue .Len (); i ++ {
215
+ b , err := BuildRequestBody (optsValue .Index (i ).Interface (), "" )
216
+ if err != nil {
217
+ return nil , err
218
+ }
219
+ optsMaps [i ] = b
220
+ }
221
+ if parent == "" {
222
+ return nil , fmt .Errorf ("Parent is required when passing an array or a slice." )
223
+ }
224
+ return map [string ]any {parent : optsMaps }, nil
187
225
}
188
- // Return an error if the underlying type of 'opts' isn't a struct.
189
- return nil , fmt .Errorf ("Options type is not a struct." )
226
+ // Return an error if we can't work with the underlying type of 'opts'
227
+ return nil , fmt .Errorf ("Options type is not a struct, a slice, or an array ." )
190
228
}
191
229
192
230
// EnabledState is a convenience type, mostly used in Create and Update
0 commit comments