MCPcopy
hub / github.com/daptin/daptin / ToURLQuery

Function ToURLQuery

form_test.go:53–74  ·  view source on GitHub ↗

ToURLQuery converts a Go object into a x-www-form-urlencoded query string

(input interface{})

Source from the content-addressed store, hash-verified

51
52// ToURLQuery converts a Go object into a x-www-form-urlencoded query string
53func ToURLQuery(input interface{}) string {
54 v := url.Values{}
55 rv := reflect.ValueOf(input)
56
57 if rv.Kind() == reflect.Map {
58 for _, k := range rv.MapKeys() {
59 mapKey := fmt.Sprintf("%v", k)
60 encodeQuery(mapKey, rv.MapIndex(k).Interface(), v)
61 }
62 } else if rv.Kind() == reflect.Struct {
63 for i := 0; i < rv.NumField(); i++ {
64 field := rv.Type().Field(i)
65 fieldName := field.Name
66 fieldValue := rv.Field(i).Interface()
67 encodeQuery(fieldName, fieldValue, v)
68 }
69 } else {
70 encodeQuery("", input, v)
71 }
72
73 return v.Encode()
74}

Callers 1

TestFormFunction · 0.70

Calls 3

TypeMethod · 0.80
EncodeMethod · 0.80
encodeQueryFunction · 0.70

Tested by

no test coverage detected