Skip to content

Commit 0d72f00

Browse files
committed
add user documentation
1 parent 5428a56 commit 0d72f00

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

query/encode.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// Package query implements encoding of structs into URL query parameters.
2+
//
3+
// As a simple example:
4+
//
5+
// type Options struct {
6+
// Query string `url:"q"`
7+
// ShowAll bool `url:"all"`
8+
// Page int `url:"page"`
9+
// }
10+
//
11+
// opt := Options{ "foo", true, 2 }
12+
// v, _ := query.Values(opt)
13+
// fmt.Print(v.Encode()) // will output: "q=foo&all=true&page=2"
14+
//
15+
// The exact mapping between Go values and url.Values is described in the
16+
// documentation for the Values() function.
117
package query
218

319
import (
@@ -12,6 +28,56 @@ import (
1228
var timeType = reflect.TypeOf(time.Time{})
1329

1430
// Values returns the url.Values encoding of v.
31+
//
32+
// Values expects to be passed a struct, and traverses it recursively using the
33+
// following encoding rules.
34+
//
35+
// The URL parameter name defaults to the struct field name but can be
36+
// specified in the struct field's tag value. The "url" key in the struct
37+
// field's tag value is the key name, followed by an optional comma and
38+
// options. For example:
39+
//
40+
// // Field is ignored by this package.
41+
// Field int `url:"-"`
42+
//
43+
// // Field appears as URL parameter "myName".
44+
// Field int `url:"myName"`
45+
//
46+
// // Field appears as URL parameter "myName" and the field is omitted if
47+
// // its value is empty
48+
// Field int `url:"myName,omitempty"`
49+
//
50+
// // Field appears as URL parameter "Field" (the default), but the field
51+
// // is skipped if empty. Note the leading comma.
52+
// Field int `url:",omitempty"`
53+
//
54+
// For encoding individual field values, the following type-dependent rules
55+
// apply:
56+
//
57+
// Boolean values default to encoding as the strings "true" or "false".
58+
// Including the "int" option signals that the field should be encoded as the
59+
// strings "1" or "0".
60+
//
61+
// time.Time values default to encoding as RFC3339 timestamps. Including the
62+
// "unix" option signals that the field should be encoded as a Unix time (see
63+
// time.Unix())
64+
//
65+
// Slice and Array values default to encoding as multiple URL values of the
66+
// same name. Including the "comma" option signals that the field should be
67+
// encoded as a single comma-delimited value. Including the "space" option
68+
// similarly encodes the value as a single space-delimited string.
69+
//
70+
// Anonymous struct fields are usually encoded as if their inner exported
71+
// fields were fields in the outer struct, subject to the standard Go
72+
// visibility rules. An anonymous struct field with a name given in its URL
73+
// tag is treated as having that name, rather than being anonymous.
74+
//
75+
// Non-nil pointer values are encoded as the value pointed to.
76+
//
77+
// All other values are encoded using their default string representation.
78+
//
79+
// Multiple fields that encode to the same URL parameter name will be included
80+
// as multiple URL values of the same name.
1581
func Values(v interface{}) (url.Values, error) {
1682
values := &url.Values{}
1783

0 commit comments

Comments
 (0)