-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpushstate.go
89 lines (74 loc) · 2.09 KB
/
pushstate.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
package web
import (
"encoding/json"
"net/url"
)
func Location(query url.Values) (r *LocationBuilder) {
r = &LocationBuilder{
MyQuery: make(map[string]json.Marshaler),
}
r.Query(query)
return
}
func (b *LocationBuilder) URL(url string) (r *LocationBuilder) {
b.MyURL = url
return b
}
func (b *LocationBuilder) MergeQuery(v bool) (r *LocationBuilder) {
b.MyMergeQuery = v
return b
}
func (b *LocationBuilder) MergeWithAppend(key string, values []string) (r *LocationBuilder) {
b.MergeQuery(true)
b.MyQuery[key] = valueOp{
Values: values,
Add: true,
}
return b
}
func (b *LocationBuilder) MergeWithRemove(key string, values []string) (r *LocationBuilder) {
b.MergeQuery(true)
b.MyQuery[key] = valueOp{
Values: values,
Remove: true,
}
return b
}
func (b *LocationBuilder) Query(query url.Values) (r *LocationBuilder) {
for k, vs := range query {
b.PutQuery(k, vs)
}
return b
}
func (b *LocationBuilder) PutQuery(key string, values []string) (r *LocationBuilder) {
b.MyQuery[key] = valuesMarshaller(values)
return b
}
func (b *LocationBuilder) StringQuery(v string) (r *LocationBuilder) {
b.MyStringQuery = v
return b
}
func (b *LocationBuilder) ClearMergeQuery(clearKeys []string) (r *LocationBuilder) {
b.MyClearMergeQueryKeys = clearKeys
return b
}
type valuesMarshaller []string
func (vm valuesMarshaller) MarshalJSON() ([]byte, error) {
return json.Marshal([]string(vm))
}
// LocationBuilder mapping to type.ts Location interface
type LocationBuilder struct {
MyMergeQuery bool `json:"mergeQuery,omitempty"`
MyURL string `json:"url,omitempty"`
MyStringQuery string `json:"stringQuery,omitempty"`
MyClearMergeQueryKeys []string `json:"clearMergeQueryKeys,omitempty"`
MyQuery map[string]json.Marshaler `json:"query,omitempty"`
}
type valueOp struct {
Values []string `json:"values,omitempty"`
Add bool `json:"add,omitempty"`
Remove bool `json:"remove,omitempty"`
}
func (vm valueOp) MarshalJSON() ([]byte, error) {
return json.Marshal(vm)
}