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