| 1 |
// Copyright (c) 2021, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package tdhttp |
| 8 |
|
| 9 |
import ( |
| 10 |
"errors" |
| 11 |
"fmt" |
| 12 |
"net/url" |
| 13 |
"reflect" |
| 14 |
"strconv" |
| 15 |
|
| 16 |
"github.com/maxatome/go-testdeep/internal/color" |
| 17 |
) |
| 18 |
|
| 19 |
// Q allows to easily declare query parameters for use in [NewRequest] |
| 20 |
// and related [http.Request] builders, as [Get] for example: |
| 21 |
// |
| 22 |
// req := tdhttp.Get("/path", tdhttp.Q{ |
| 23 |
// "id": []int64{1234, 4567}, |
| 24 |
// "dryrun": true, |
| 25 |
// }) |
| 26 |
// |
| 27 |
// See [NewRequest] for several examples of use. |
| 28 |
// |
| 29 |
// Accepted types as values are: |
| 30 |
// - [fmt.Stringer] |
| 31 |
// - string |
| 32 |
// - int, int8, int16, int32, int64 |
| 33 |
// - uint, uint8, uint16, uint32, uint64 |
| 34 |
// - float32, float64 |
| 35 |
// - bool |
| 36 |
// - slice or array of any type above, plus any |
| 37 |
// - pointer on any type above, plus any or any other pointer |
| 38 |
type Q map[string]any |
| 39 |
|
| 40 |
var _ URLValuesEncoder = Q(nil) |
| 41 |
|
| 42 |
// AddTo adds the q contents to qp. |
| 43 |
func (q Q) AddTo(qp url.Values) error { |
| 44 |
for param, value := range q { |
| 45 |
// Ignore nil values |
| 46 |
if value == nil { |
| 47 |
continue |
| 48 |
} |
| 49 |
err := q.addParamTo(param, reflect.ValueOf(value), true, qp) |
| 50 |
if err != nil { |
| 51 |
return err |
| 52 |
} |
| 53 |
} |
| 54 |
return nil |
| 55 |
} |
| 56 |
|
| 57 |
// Values returns a [url.Values] instance corresponding to q. It panics |
| 58 |
// if a value cannot be converted. |
| 59 |
func (q Q) Values() url.Values { |
| 60 |
qp := make(url.Values, len(q)) |
| 61 |
err := q.AddTo(qp) |
| 62 |
if err != nil { |
| 63 |
panic(errors.New(color.Bad(err.Error()))) |
| 64 |
} |
| 65 |
return qp |
| 66 |
} |
| 67 |
|
| 68 |
// Encode does the same as [url.Values.Encode] does. So quoting its doc, |
| 69 |
// it encodes the values into “URL encoded” form ("bar=baz&foo=quux") |
| 70 |
// sorted by key. |
| 71 |
// |
| 72 |
// It panics if a value cannot be converted. |
| 73 |
func (q Q) Encode() string { |
| 74 |
return q.Values().Encode() |
| 75 |
} |
| 76 |
|
| 77 |
func (q Q) addParamTo(param string, v reflect.Value, allowArray bool, qp url.Values) error { |
| 78 |
var str string |
| 79 |
for { |
| 80 |
if s, ok := v.Interface().(fmt.Stringer); ok { |
| 81 |
qp.Add(param, s.String()) |
| 82 |
return nil |
| 83 |
} |
| 84 |
|
| 85 |
switch v.Kind() { |
| 86 |
case reflect.Slice, reflect.Array: |
| 87 |
if !allowArray { |
| 88 |
return fmt.Errorf("%s is only allowed at the root level for param %q", |
| 89 |
v.Kind(), param) |
| 90 |
} |
| 91 |
for i, l := 0, v.Len(); i < l; i++ { |
| 92 |
err := q.addParamTo(param, v.Index(i), false, qp) |
| 93 |
if err != nil { |
| 94 |
return err |
| 95 |
} |
| 96 |
} |
| 97 |
return nil |
| 98 |
|
| 99 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 100 |
str = strconv.FormatInt(v.Int(), 10) |
| 101 |
|
| 102 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 103 |
str = strconv.FormatUint(v.Uint(), 10) |
| 104 |
|
| 105 |
case reflect.Float32, reflect.Float64: |
| 106 |
str = strconv.FormatFloat(v.Float(), 'g', -1, 64) |
| 107 |
|
| 108 |
case reflect.String: |
| 109 |
str = v.String() |
| 110 |
|
| 111 |
case reflect.Bool: |
| 112 |
str = strconv.FormatBool(v.Bool()) |
| 113 |
|
| 114 |
case reflect.Ptr, reflect.Interface: |
| 115 |
if !v.IsNil() { |
| 116 |
v = v.Elem() |
| 117 |
continue |
| 118 |
} |
| 119 |
return nil // mimic url.Values behavior ⇒ ignore |
| 120 |
|
| 121 |
default: |
| 122 |
return fmt.Errorf("don't know how to add type %s (%s) to param %q", |
| 123 |
v.Type(), v.Kind(), param) |
| 124 |
} |
| 125 |
|
| 126 |
qp.Add(param, str) |
| 127 |
return nil |
| 128 |
} |
| 129 |
} |