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 | 6abf928 | 2014-09-24 10:54:13 +0200 | [diff] [blame] | 12 | // MaybeString takes a string that might be a zero-value, and either returns a |
| 13 | // pointer to its address or a nil value (i.e. empty pointer). This is useful |
| 14 | // for converting zero values in options structs when the end-user hasn't |
| 15 | // defined values. Those zero values need to be nil in order for the JSON |
| 16 | // serialization to ignore them. |
| 17 | func MaybeString(original string) *string { |
| 18 | if original != "" { |
| 19 | return &original |
| 20 | } |
| 21 | return nil |
| 22 | } |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 23 | |
Jamie Hannaford | cb12ee6 | 2014-10-06 15:35:36 +0200 | [diff] [blame] | 24 | // MaybeInt takes an int that might be a zero-value, and either returns a |
| 25 | // pointer to its address or a nil value (i.e. empty pointer). |
Jon Perritt | 8d26258 | 2014-10-03 11:11:46 -0500 | [diff] [blame] | 26 | func MaybeInt(original int) *int { |
| 27 | if original != 0 { |
| 28 | return &original |
| 29 | } |
| 30 | return nil |
| 31 | } |
| 32 | |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 33 | var t time.Time |
| 34 | |
| 35 | func isZero(v reflect.Value) bool { |
| 36 | switch v.Kind() { |
| 37 | case reflect.Func, reflect.Map, reflect.Slice: |
| 38 | return v.IsNil() |
| 39 | case reflect.Array: |
| 40 | z := true |
| 41 | for i := 0; i < v.Len(); i++ { |
| 42 | z = z && isZero(v.Index(i)) |
| 43 | } |
| 44 | return z |
| 45 | case reflect.Struct: |
| 46 | if v.Type() == reflect.TypeOf(t) { |
| 47 | if v.Interface().(time.Time).IsZero() { |
| 48 | return true |
| 49 | } |
| 50 | return false |
| 51 | } |
| 52 | z := true |
| 53 | for i := 0; i < v.NumField(); i++ { |
| 54 | z = z && isZero(v.Field(i)) |
| 55 | } |
| 56 | return z |
| 57 | } |
| 58 | // Compare other types directly: |
| 59 | z := reflect.Zero(v.Type()) |
| 60 | return v.Interface() == z.Interface() |
| 61 | } |
| 62 | |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 63 | /* |
| 64 | BuildQueryString accepts a generic structure and parses it URL struct. It |
| 65 | converts field names into query names based on "q" tags. So for example, this |
| 66 | type: |
| 67 | |
| 68 | struct { |
| 69 | Bar string `q:"x_bar"` |
| 70 | Baz int `q:"lorem_ipsum"` |
| 71 | }{ |
| 72 | Bar: "XXX", |
| 73 | Baz: "YYY", |
| 74 | } |
| 75 | |
| 76 | will be converted into ?x_bar=XXX&lorem_ipsum=YYYY |
| 77 | */ |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 78 | func BuildQueryString(opts interface{}) (*url.URL, error) { |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 79 | optsValue := reflect.ValueOf(opts) |
| 80 | if optsValue.Kind() == reflect.Ptr { |
| 81 | optsValue = optsValue.Elem() |
| 82 | } |
| 83 | |
| 84 | optsType := reflect.TypeOf(opts) |
| 85 | if optsType.Kind() == reflect.Ptr { |
| 86 | optsType = optsType.Elem() |
| 87 | } |
| 88 | |
Jamie Hannaford | cb12ee6 | 2014-10-06 15:35:36 +0200 | [diff] [blame] | 89 | var optsSlice []string |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 90 | if optsValue.Kind() == reflect.Struct { |
| 91 | for i := 0; i < optsValue.NumField(); i++ { |
| 92 | v := optsValue.Field(i) |
| 93 | f := optsType.Field(i) |
| 94 | qTag := f.Tag.Get("q") |
| 95 | |
| 96 | // if the field has a 'q' tag, it goes in the query string |
| 97 | if qTag != "" { |
| 98 | tags := strings.Split(qTag, ",") |
| 99 | |
| 100 | // if the field is set, add it to the slice of query pieces |
| 101 | if !isZero(v) { |
| 102 | switch v.Kind() { |
| 103 | case reflect.String: |
| 104 | optsSlice = append(optsSlice, tags[0]+"="+v.String()) |
| 105 | case reflect.Int: |
| 106 | optsSlice = append(optsSlice, tags[0]+"="+strconv.FormatInt(v.Int(), 10)) |
| 107 | case reflect.Bool: |
| 108 | optsSlice = append(optsSlice, tags[0]+"="+strconv.FormatBool(v.Bool())) |
| 109 | } |
| 110 | } else { |
| 111 | // Otherwise, the field is not set. |
| 112 | if len(tags) == 2 && tags[1] == "required" { |
| 113 | // And the field is required. Return an error. |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 114 | return nil, fmt.Errorf("Required query parameter [%s] not set.", f.Name) |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | } |
| 120 | // URL encode the string for safety. |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 121 | s := strings.Join(optsSlice, "&") |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 122 | if s != "" { |
| 123 | s = "?" + s |
| 124 | } |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 125 | u, err := url.Parse(s) |
| 126 | if err != nil { |
| 127 | return nil, err |
| 128 | } |
| 129 | return u, nil |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 130 | } |
| 131 | // 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] | 132 | return nil, fmt.Errorf("Options type is not a struct.") |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 133 | } |
| 134 | |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 135 | // BuildHeaders accepts a generic structure and parses it into a string map. It |
Jamie Hannaford | cb12ee6 | 2014-10-06 15:35:36 +0200 | [diff] [blame] | 136 | // converts field names into header names based on "h" tags, and field values |
| 137 | // into header values by a simple one-to-one mapping. |
Jon Perritt | f90a43c | 2014-09-28 20:09:46 -0500 | [diff] [blame] | 138 | func BuildHeaders(opts interface{}) (map[string]string, error) { |
| 139 | optsValue := reflect.ValueOf(opts) |
| 140 | if optsValue.Kind() == reflect.Ptr { |
| 141 | optsValue = optsValue.Elem() |
| 142 | } |
| 143 | |
| 144 | optsType := reflect.TypeOf(opts) |
| 145 | if optsType.Kind() == reflect.Ptr { |
| 146 | optsType = optsType.Elem() |
| 147 | } |
| 148 | |
| 149 | optsMap := make(map[string]string) |
| 150 | if optsValue.Kind() == reflect.Struct { |
| 151 | for i := 0; i < optsValue.NumField(); i++ { |
| 152 | v := optsValue.Field(i) |
| 153 | f := optsType.Field(i) |
| 154 | hTag := f.Tag.Get("h") |
| 155 | |
| 156 | // if the field has a 'h' tag, it goes in the header |
| 157 | if hTag != "" { |
| 158 | tags := strings.Split(hTag, ",") |
| 159 | |
| 160 | // if the field is set, add it to the slice of query pieces |
| 161 | if !isZero(v) { |
| 162 | switch v.Kind() { |
| 163 | case reflect.String: |
| 164 | optsMap[tags[0]] = v.String() |
| 165 | case reflect.Int: |
| 166 | optsMap[tags[0]] = strconv.FormatInt(v.Int(), 10) |
| 167 | case reflect.Bool: |
| 168 | optsMap[tags[0]] = strconv.FormatBool(v.Bool()) |
| 169 | } |
| 170 | } else { |
| 171 | // Otherwise, the field is not set. |
| 172 | if len(tags) == 2 && tags[1] == "required" { |
| 173 | // And the field is required. Return an error. |
| 174 | return optsMap, fmt.Errorf("Required header not set.") |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | } |
| 180 | return optsMap, nil |
| 181 | } |
| 182 | // Return an error if the underlying type of 'opts' isn't a struct. |
| 183 | return optsMap, fmt.Errorf("Options type is not a struct.") |
| 184 | } |