-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexamples_test.go
155 lines (134 loc) · 2.67 KB
/
examples_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package qs
import (
"fmt"
"net/url"
"reflect"
"sort"
)
func ExampleMarshal() {
type Query struct {
Search string
Page int
PageSize int
}
queryString, err := Marshal(&Query{
Search: "my search",
Page: 2,
PageSize: 50,
})
if err != nil {
fmt.Println(err)
} else {
fmt.Println(queryString)
}
// Output:
// page=2&page_size=50&search=my+search
}
func ExampleMarshalValues() {
type Query struct {
Search string
Page int
PageSize int
}
// values is a url.Values which is a map[string][]string
values, err := MarshalValues(&Query{
Search: "my search",
Page: 2,
PageSize: 50,
})
if err != nil {
fmt.Println(err)
} else {
// printing the values map after sorting its keys
var keys []string
for key := range values {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
fmt.Printf("%v: %v\n", key, values[key])
}
}
// Output:
// page: [2]
// page_size: [50]
// search: [my search]
}
func ExampleCheckMarshal() {
m := map[string]int{}
fmt.Println(CheckMarshal(0))
fmt.Println(CheckMarshal(m))
fmt.Println(CheckMarshal(&m))
// Output:
// unhandled type: int
// <nil>
// <nil>
}
func ExampleCheckMarshalType() {
intType := reflect.TypeOf(0)
mapType := reflect.TypeOf((map[string]int)(nil))
fmt.Println(CheckMarshalType(intType))
fmt.Println(CheckMarshalType(mapType))
fmt.Println(CheckMarshalType(reflect.PtrTo(mapType)))
// Output:
// unhandled type: int
// <nil>
// <nil>
}
func ExampleCheckUnmarshal() {
m := map[string]int{}
fmt.Println(CheckUnmarshal(0))
fmt.Println(CheckUnmarshal(m))
fmt.Println(CheckUnmarshal(&m))
// Output:
// expected a pointer, got int
// expected a pointer, got map[string]int
// <nil>
}
func ExampleCheckUnmarshalType() {
intType := reflect.TypeOf(0)
mapType := reflect.TypeOf((map[string]int)(nil))
fmt.Println(CheckUnmarshalType(intType))
fmt.Println(CheckUnmarshalType(mapType))
fmt.Println(CheckUnmarshalType(reflect.PtrTo(mapType)))
// Output:
// expected a pointer, got int
// expected a pointer, got map[string]int
// <nil>
}
func ExampleUnmarshal() {
type Query struct {
Search string
Page int
PageSize int
}
var q Query
err := Unmarshal(&q, "page=2&page_size=50&search=my+search")
if err != nil {
fmt.Println(err)
} else {
fmt.Println(q)
}
// Output:
// {my search 2 50}
}
func ExampleUnmarshalValues() {
type Query struct {
Search string
Page int
PageSize int
}
var q Query
err := UnmarshalValues(&q, url.Values{
"search": {"my search"},
"page": {"2"},
"page_size": {"50"},
})
if err != nil {
fmt.Println(err)
} else {
fmt.Println(q)
}
// Output:
// {my search 2 50}
}