-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdoc_test.go
83 lines (72 loc) · 1.75 KB
/
doc_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package qstring_test
import (
"fmt"
"net/url"
"os"
"time"
"github.com/dyninc/qstring"
)
func ExampleUnmarshal() {
// Query is the http request query struct.
type Query struct {
Names []string
Limit int
Page int
}
query := &Query{}
qValues, _ := url.ParseQuery("names=foo&names=bar&limit=50&page=1")
err := qstring.Unmarshal(qValues, query)
if err != nil {
panic("Unable to Parse Query String")
}
os.Stdout.Write([]byte(fmt.Sprintf("%+v", query)))
// Output: &{Names:[foo bar] Limit:50 Page:1}
}
func ExampleMarshalString() {
// Query is the http request query struct.
type Query struct {
Names []string
Limit int
Page int
}
query := &Query{
Names: []string{"foo", "bar"},
Limit: 50,
Page: 1,
}
q, _ := qstring.MarshalString(query)
os.Stdout.Write([]byte(q))
// Output: limit=50&names=foo&names=bar&page=1
}
func ExampleUnmarshal_complex() {
// PagingParams represents common pagination information for query strings
type PagingParams struct {
Page int `qstring:"page"`
Limit int `qstring:"limit"`
}
// Query is the http request query struct.
type Query struct {
Names []string
IDs []int
PageInfo *PagingParams
Created time.Time
}
query := &Query{}
qValues, _ := url.ParseQuery("names=foo&names=bar&limit=50&page=1&ids=1&ids=2&created=2006-01-02T15:04:05Z")
err := qstring.Unmarshal(qValues, query)
if err != nil {
panic("Unable to Parse Query String")
}
}
func ExampleComparativeTime() {
type DateQuery struct {
Created qstring.ComparativeTime
Modified qstring.ComparativeTime
}
var query DateQuery
qValues, _ := url.ParseQuery("created=>=2006-01-02T15:04:05Z&modified=<=2016-01-01T15:04Z")
err := qstring.Unmarshal(qValues, &query)
if err != nil {
panic("Unable to Parse Query String")
}
}