Jamie Hannaford | 6abf928 | 2014-09-24 10:54:13 +0200 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 3 | import ( |
Jon Perritt | 01618ee | 2016-03-09 03:04:06 -0600 | [diff] [blame] | 4 | "encoding/json" |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 5 | "fmt" |
| 6 | "net/url" |
| 7 | "reflect" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "time" |
| 11 | ) |
| 12 | |
Jon Perritt | 01618ee | 2016-03-09 03:04:06 -0600 | [diff] [blame] | 13 | // BuildRequestBody builds a map[string]interface from the given `struct`. |
| 14 | // |
| 15 | // |
| 16 | func BuildRequestBody(opts interface{}) (map[string]interface{}, error) { |
| 17 | optsValue := reflect.ValueOf(opts) |
| 18 | if optsValue.Kind() == reflect.Ptr { |
| 19 | optsValue = optsValue.Elem() |
| 20 | } |
| 21 | |
| 22 | optsType := reflect.TypeOf(opts) |
| 23 | if optsType.Kind() == reflect.Ptr { |
| 24 | optsType = optsType.Elem() |
| 25 | } |
| 26 | |
| 27 | optsMap := make(map[string]interface{}) |
| 28 | if optsValue.Kind() == reflect.Struct { |
| 29 | |
| 30 | for i := 0; i < optsValue.NumField(); i++ { |
| 31 | v := optsValue.Field(i) |
| 32 | f := optsType.Field(i) |
| 33 | requiredTag := f.Tag.Get("required") |
| 34 | |
| 35 | // if the field has a 'required' tag, it can't have a zero-value |
| 36 | if requiredTag == "true" && isZero(v) { |
| 37 | err := ErrMissingInput{} |
| 38 | err.Argument = f.Name |
| 39 | return nil, err |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | b, err := json.Marshal(opts) |
| 44 | if err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | |
| 48 | err = json.Unmarshal(b, &optsMap) |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | |
| 53 | return optsMap, nil |
| 54 | } |
| 55 | // Return an error if the underlying type of 'opts' isn't a struct. |
| 56 | return nil, fmt.Errorf("Options type is not a struct.") |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 57 | } |
| 58 | |
Jamie Hannaford | d1e6e76 | 2014-11-06 14:26:31 +0100 | [diff] [blame] | 59 | // EnabledState is a convenience type, mostly used in Create and Update |
| 60 | // operations. Because the zero value of a bool is FALSE, we need to use a |
| 61 | // pointer instead to indicate zero-ness. |
| 62 | type EnabledState *bool |
| 63 | |
| 64 | // Convenience vars for EnabledState values. |
| 65 | var ( |
| 66 | iTrue = true |
| 67 | iFalse = false |
| 68 | |
| 69 | Enabled EnabledState = &iTrue |
| 70 | Disabled EnabledState = &iFalse |
| 71 | ) |
| 72 | |
Jamie Hannaford | 7fa2789 | 2014-11-07 15:11:20 +0100 | [diff] [blame] | 73 | // IntToPointer is a function for converting integers into integer pointers. |
| 74 | // This is useful when passing in options to operations. |
| 75 | func IntToPointer(i int) *int { |
| 76 | return &i |
| 77 | } |
| 78 | |
Ash Wilson | 0735acb | 2014-10-31 14:18:00 -0400 | [diff] [blame] | 79 | /* |
| 80 | MaybeString is an internal function to be used by request methods in individual |
| 81 | resource packages. |
| 82 | |
| 83 | It takes a string that might be a zero value and returns either a pointer to its |
| 84 | address or nil. This is useful for allowing users to conveniently omit values |
Ash Wilson | 07d1cfb | 2014-10-31 14:21:26 -0400 | [diff] [blame] | 85 | from an options struct by leaving them zeroed, but still pass nil to the JSON |
| 86 | serializer so they'll be omitted from the request body. |
Ash Wilson | 0735acb | 2014-10-31 14:18:00 -0400 | [diff] [blame] | 87 | */ |
Jamie Hannaford | 6abf928 | 2014-09-24 10:54:13 +0200 | [diff] [blame] | 88 | func MaybeString(original string) *string { |
| 89 | if original != "" { |
| 90 | return &original |
| 91 | } |
| 92 | return nil |
| 93 | } |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 94 | |
Ash Wilson | 0735acb | 2014-10-31 14:18:00 -0400 | [diff] [blame] | 95 | /* |
| 96 | MaybeInt is an internal function to be used by request methods in individual |
| 97 | resource packages. |
| 98 | |
Ash Wilson | 07d1cfb | 2014-10-31 14:21:26 -0400 | [diff] [blame] | 99 | Like MaybeString, it accepts an int that may or may not be a zero value, and |
| 100 | returns either a pointer to its address or nil. It's intended to hint that the |
| 101 | JSON serializer should omit its field. |
Ash Wilson | 0735acb | 2014-10-31 14:18:00 -0400 | [diff] [blame] | 102 | */ |
Jon Perritt | 8d26258 | 2014-10-03 11:11:46 -0500 | [diff] [blame] | 103 | func MaybeInt(original int) *int { |
| 104 | if original != 0 { |
| 105 | return &original |
| 106 | } |
| 107 | return nil |
| 108 | } |
| 109 | |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 110 | var t time.Time |
| 111 | |
| 112 | func isZero(v reflect.Value) bool { |
| 113 | switch v.Kind() { |
| 114 | case reflect.Func, reflect.Map, reflect.Slice: |
| 115 | return v.IsNil() |
| 116 | case reflect.Array: |
| 117 | z := true |
| 118 | for i := 0; i < v.Len(); i++ { |
| 119 | z = z && isZero(v.Index(i)) |
| 120 | } |
| 121 | return z |
| 122 | case reflect.Struct: |
| 123 | if v.Type() == reflect.TypeOf(t) { |
| 124 | if v.Interface().(time.Time).IsZero() { |
| 125 | return true |
| 126 | } |
| 127 | return false |
| 128 | } |
| 129 | z := true |
| 130 | for i := 0; i < v.NumField(); i++ { |
| 131 | z = z && isZero(v.Field(i)) |
| 132 | } |
| 133 | return z |
| 134 | } |
| 135 | // Compare other types directly: |
| 136 | z := reflect.Zero(v.Type()) |
| 137 | return v.Interface() == z.Interface() |
| 138 | } |
| 139 | |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 140 | /* |
Ash Wilson | 0735acb | 2014-10-31 14:18:00 -0400 | [diff] [blame] | 141 | BuildQueryString is an internal function to be used by request methods in |
| 142 | individual resource packages. |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 143 | |
Ash Wilson | 0735acb | 2014-10-31 14:18:00 -0400 | [diff] [blame] | 144 | It accepts a tagged structure and expands it into a URL struct. Field names are |
| 145 | converted into query parameters based on a "q" tag. For example: |
| 146 | |
| 147 | type struct Something { |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 148 | Bar string `q:"x_bar"` |
| 149 | Baz int `q:"lorem_ipsum"` |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 150 | } |
| 151 | |
Ash Wilson | 0735acb | 2014-10-31 14:18:00 -0400 | [diff] [blame] | 152 | instance := Something{ |
| 153 | Bar: "AAA", |
| 154 | Baz: "BBB", |
| 155 | } |
| 156 | |
| 157 | will be converted into "?x_bar=AAA&lorem_ipsum=BBB". |
| 158 | |
| 159 | The struct's fields may be strings, integers, or boolean values. Fields left at |
Alex Gaynor | c6cc18f | 2014-10-31 13:48:58 -0700 | [diff] [blame] | 160 | their type's zero value will be omitted from the query. |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 161 | */ |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 162 | func BuildQueryString(opts interface{}) (*url.URL, error) { |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 163 | optsValue := reflect.ValueOf(opts) |
| 164 | if optsValue.Kind() == reflect.Ptr { |
| 165 | optsValue = optsValue.Elem() |
| 166 | } |
| 167 | |
| 168 | optsType := reflect.TypeOf(opts) |
| 169 | if optsType.Kind() == reflect.Ptr { |
| 170 | optsType = optsType.Elem() |
| 171 | } |
| 172 | |
Jamie Hannaford | f68c3e4 | 2014-11-18 13:02:09 +0100 | [diff] [blame] | 173 | params := url.Values{} |
| 174 | |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 175 | if optsValue.Kind() == reflect.Struct { |
| 176 | for i := 0; i < optsValue.NumField(); i++ { |
| 177 | v := optsValue.Field(i) |
| 178 | f := optsType.Field(i) |
| 179 | qTag := f.Tag.Get("q") |
| 180 | |
| 181 | // if the field has a 'q' tag, it goes in the query string |
| 182 | if qTag != "" { |
| 183 | tags := strings.Split(qTag, ",") |
| 184 | |
| 185 | // if the field is set, add it to the slice of query pieces |
| 186 | if !isZero(v) { |
| 187 | switch v.Kind() { |
| 188 | case reflect.String: |
Jamie Hannaford | f68c3e4 | 2014-11-18 13:02:09 +0100 | [diff] [blame] | 189 | params.Add(tags[0], v.String()) |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 190 | case reflect.Int: |
Jamie Hannaford | f68c3e4 | 2014-11-18 13:02:09 +0100 | [diff] [blame] | 191 | params.Add(tags[0], strconv.FormatInt(v.Int(), 10)) |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 192 | case reflect.Bool: |
Jamie Hannaford | f68c3e4 | 2014-11-18 13:02:09 +0100 | [diff] [blame] | 193 | params.Add(tags[0], strconv.FormatBool(v.Bool())) |
Jon Perritt | dc56190 | 2015-02-08 15:22:02 -0700 | [diff] [blame] | 194 | case reflect.Slice: |
Jon Perritt | e43f3de | 2015-02-12 11:45:34 -0700 | [diff] [blame] | 195 | switch v.Type().Elem() { |
| 196 | case reflect.TypeOf(0): |
| 197 | for i := 0; i < v.Len(); i++ { |
| 198 | params.Add(tags[0], strconv.FormatInt(v.Index(i).Int(), 10)) |
| 199 | } |
| 200 | default: |
Jon Perritt | dc56190 | 2015-02-08 15:22:02 -0700 | [diff] [blame] | 201 | for i := 0; i < v.Len(); i++ { |
| 202 | params.Add(tags[0], v.Index(i).String()) |
| 203 | } |
| 204 | } |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 205 | } |
| 206 | } else { |
| 207 | // Otherwise, the field is not set. |
| 208 | if len(tags) == 2 && tags[1] == "required" { |
| 209 | // And the field is required. Return an error. |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 210 | return nil, fmt.Errorf("Required query parameter [%s] not set.", f.Name) |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | } |
Jamie Hannaford | f68c3e4 | 2014-11-18 13:02:09 +0100 | [diff] [blame] | 214 | } |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 215 | |
Jamie Hannaford | f68c3e4 | 2014-11-18 13:02:09 +0100 | [diff] [blame] | 216 | return &url.URL{RawQuery: params.Encode()}, nil |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 217 | } |
| 218 | // Return an error if the underlying type of 'opts' isn't a struct. |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 219 | return nil, fmt.Errorf("Options type is not a struct.") |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 220 | } |
| 221 | |
Ash Wilson | 0735acb | 2014-10-31 14:18:00 -0400 | [diff] [blame] | 222 | /* |
| 223 | BuildHeaders is an internal function to be used by request methods in |
| 224 | individual resource packages. |
| 225 | |
Alex Gaynor | c6cc18f | 2014-10-31 13:48:58 -0700 | [diff] [blame] | 226 | It accepts an arbitrary tagged structure and produces a string map that's |
Ash Wilson | 0735acb | 2014-10-31 14:18:00 -0400 | [diff] [blame] | 227 | suitable for use as the HTTP headers of an outgoing request. Field names are |
| 228 | mapped to header names based in "h" tags. |
| 229 | |
| 230 | type struct Something { |
| 231 | Bar string `h:"x_bar"` |
| 232 | Baz int `h:"lorem_ipsum"` |
| 233 | } |
| 234 | |
| 235 | instance := Something{ |
| 236 | Bar: "AAA", |
| 237 | Baz: "BBB", |
| 238 | } |
| 239 | |
| 240 | will be converted into: |
| 241 | |
| 242 | map[string]string{ |
| 243 | "x_bar": "AAA", |
| 244 | "lorem_ipsum": "BBB", |
| 245 | } |
| 246 | |
| 247 | Untagged fields and fields left at their zero values are skipped. Integers, |
| 248 | booleans and string values are supported. |
| 249 | */ |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 250 | func BuildHeaders(opts interface{}) (map[string]string, error) { |
| 251 | optsValue := reflect.ValueOf(opts) |
| 252 | if optsValue.Kind() == reflect.Ptr { |
| 253 | optsValue = optsValue.Elem() |
| 254 | } |
| 255 | |
| 256 | optsType := reflect.TypeOf(opts) |
| 257 | if optsType.Kind() == reflect.Ptr { |
| 258 | optsType = optsType.Elem() |
| 259 | } |
| 260 | |
| 261 | optsMap := make(map[string]string) |
| 262 | if optsValue.Kind() == reflect.Struct { |
| 263 | for i := 0; i < optsValue.NumField(); i++ { |
| 264 | v := optsValue.Field(i) |
| 265 | f := optsType.Field(i) |
| 266 | hTag := f.Tag.Get("h") |
| 267 | |
| 268 | // if the field has a 'h' tag, it goes in the header |
| 269 | if hTag != "" { |
| 270 | tags := strings.Split(hTag, ",") |
| 271 | |
| 272 | // if the field is set, add it to the slice of query pieces |
| 273 | if !isZero(v) { |
| 274 | switch v.Kind() { |
| 275 | case reflect.String: |
| 276 | optsMap[tags[0]] = v.String() |
| 277 | case reflect.Int: |
| 278 | optsMap[tags[0]] = strconv.FormatInt(v.Int(), 10) |
| 279 | case reflect.Bool: |
| 280 | optsMap[tags[0]] = strconv.FormatBool(v.Bool()) |
| 281 | } |
| 282 | } else { |
| 283 | // Otherwise, the field is not set. |
| 284 | if len(tags) == 2 && tags[1] == "required" { |
| 285 | // And the field is required. Return an error. |
| 286 | return optsMap, fmt.Errorf("Required header not set.") |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | } |
| 292 | return optsMap, nil |
| 293 | } |
| 294 | // Return an error if the underlying type of 'opts' isn't a struct. |
| 295 | return optsMap, fmt.Errorf("Options type is not a struct.") |
| 296 | } |
Jamie Hannaford | 950561c | 2014-11-12 11:12:20 +0100 | [diff] [blame] | 297 | |
| 298 | // IDSliceToQueryString takes a slice of elements and converts them into a query |
| 299 | // string. For example, if name=foo and slice=[]int{20, 40, 60}, then the |
| 300 | // result would be `?name=20&name=40&name=60' |
| 301 | func IDSliceToQueryString(name string, ids []int) string { |
| 302 | str := "" |
| 303 | for k, v := range ids { |
| 304 | if k == 0 { |
| 305 | str += "?" |
| 306 | } else { |
| 307 | str += "&" |
| 308 | } |
| 309 | str += fmt.Sprintf("%s=%s", name, strconv.Itoa(v)) |
| 310 | } |
| 311 | return str |
| 312 | } |
| 313 | |
| 314 | // IntWithinRange returns TRUE if an integer falls within a defined range, and |
| 315 | // FALSE if not. |
| 316 | func IntWithinRange(val, min, max int) bool { |
| 317 | return val > min && val < max |
| 318 | } |