-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_query_test.go
More file actions
77 lines (57 loc) · 1.66 KB
/
example_query_test.go
File metadata and controls
77 lines (57 loc) · 1.66 KB
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
package mplus
import (
"fmt"
"net/url"
)
func ExampleNewQuery() {
urlValues := make(url.Values)
urlValues.Add("name", "tom")
urlValues.Add("age", "15")
path := "http://localhost?" + urlValues.Encode()
fmt.Printf("%v\n", path)
fmt.Printf("%v\n", NewQuery().AddPairs("name", "tom", "age", "15").AppendToURI("http://localhost"))
// OutPut:
// http://localhost?age=15&name=tom
// http://localhost?age=15&name=tom
}
func ExampleQuery_SetIf() {
values := map[string]bool{
"name": false, // will be continue
"age": true,
}
fmt.Printf("%v\n", NewQuery().
SetIf(values["name"], "name", "tom").
SetIf(values["age"], "age", "15").
AppendToURIFormat("http://localhost/users/%v", 1))
// OutPut:
// http://localhost/users/1?age=15
}
func ExampleQuery_SetIfD() {
values := map[string]interface{}{
"name": nil, // will be continue
"age": "15",
}
query := NewQuery().
SetIfD(values["name"] != nil, "name", func() string { return values["name"].(string) }). // will not panic because callback is lazy evaluation
SetIfD(values["age"] != nil, "age", func() string { return values["age"].(string) })
fmt.Printf("%v\n", query.AppendToURI("http://localhost/users/1"))
// OutPut:
// http://localhost/users/1?age=15
}
func ExampleQuery_SetPairsD() {
query := NewQuery().SetPairsD(func() []string { // lazy dynamic
values := map[string]int{
"first": 1,
"second": 2,
"third": 3,
}
var pairs []string
for key, value := range values {
pairs = append(pairs, key, fmt.Sprint(value))
}
return pairs
})
fmt.Printf("%v\n", query.AppendToURI("http://localhost/users/1"))
// OutPut:
// http://localhost/users/1?first=1&second=2&third=3
}