-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
example_test.go
178 lines (163 loc) · 4.08 KB
/
example_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package httpheader_test
import (
"fmt"
"net/http"
"sort"
"time"
"github.com/mozillazg/go-httpheader"
)
func ExampleHeader() {
type Options struct {
ContentType string `header:"Content-Type"`
Length int
Bool bool
BoolInt bool `header:"Bool-Int,int"`
XArray []string `header:"X-Array"`
TestHide string `header:"-"`
IgnoreEmpty string `header:"X-Empty,omitempty"`
IgnoreEmptyN string `header:"X-Empty-N,omitempty"`
CreatedAt time.Time `header:"Created-At"`
UpdatedAt time.Time `header:"Update-At,unix"`
CustomHeader http.Header
}
opt := Options{
ContentType: "application/json",
Length: 2,
Bool: true,
BoolInt: true,
XArray: []string{"test1", "test2"},
TestHide: "hide",
IgnoreEmptyN: "n",
CreatedAt: time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC),
UpdatedAt: time.Date(2001, 1, 1, 12, 34, 56, 0, time.UTC),
CustomHeader: http.Header{
"X-Test-1": []string{"233"},
"X-Test-2": []string{"666"},
},
}
h, err := httpheader.Header(opt)
fmt.Println(err)
printHeader(h)
// Output:
// <nil>
// Bool: []string{"true"}
// Bool-Int: []string{"1"}
// Content-Type: []string{"application/json"}
// Created-At: []string{"Sat, 01 Jan 2000 12:34:56 GMT"}
// Length: []string{"2"}
// Update-At: []string{"978352496"}
// X-Array: []string{"test1", "test2"}
// X-Empty-N: []string{"n"}
// X-Test-1: []string{"233"}
// X-Test-2: []string{"666"}
}
func printHeader(h http.Header) {
var keys []string
for k := range h {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
fmt.Printf("%s: %#v\n", k, h[k])
}
}
func ExampleDecode() {
type Options struct {
ContentType string `header:"Content-Type"`
Length int
Bool bool
BoolInt bool `header:"Bool-Int,int"`
XArray []string `header:"X-Array"`
TestHide string `header:"-"`
IgnoreEmpty string `header:"X-Empty,omitempty"`
IgnoreEmptyN string `header:"X-Empty-N,omitempty"`
CreatedAt time.Time `header:"Created-At"`
UpdatedAt time.Time `header:"Update-At,unix"`
CustomHeader http.Header
}
h := http.Header{
"Bool": []string{"true"},
"Bool-Int": []string{"1"},
"X-Test-1": []string{"233"},
"X-Test-2": []string{"666"},
"Content-Type": []string{"application/json"},
"Length": []string{"2"},
"X-Array": []string{"test1", "test2"},
"X-Empty-N": []string{"n"},
"Update-At": []string{"978352496"},
"Created-At": []string{"Sat, 01 Jan 2000 12:34:56 GMT"},
}
var opt Options
err := httpheader.Decode(h, &opt)
fmt.Println(err)
fmt.Println(opt.ContentType)
fmt.Println(opt.Length)
fmt.Println(opt.BoolInt)
fmt.Println(opt.XArray)
fmt.Println(opt.UpdatedAt)
// Output:
// <nil>
// application/json
// 2
// true
// [test1 test2]
// 2001-01-01 12:34:56 +0000 UTC
}
type EncodedArgs []string
func (e EncodedArgs) EncodeHeader(key string, v *http.Header) error {
for i, arg := range e {
v.Set(fmt.Sprintf("%s.%d", key, i), arg)
}
return nil
}
type DecodeArg struct {
arg string
}
func (d *DecodeArg) DecodeHeader(header http.Header, key string) error {
value := header.Get(key)
d.arg = value
return nil
}
func ExampleEncoder() {
// type EncodedArgs []string
//
// func (e EncodedArgs) EncodeHeader(key string, v *http.Header) error {
// for i, arg := range e {
// v.Set(fmt.Sprintf("%s.%d", key, i), arg)
// }
// return nil
// }
s := struct {
Args EncodedArgs `header:"Args"`
}{Args: EncodedArgs{"a", "b", "c"}}
h, err := httpheader.Header(s)
fmt.Println(err)
printHeader(h)
// Output:
// <nil>
// Args.0: []string{"a"}
// Args.1: []string{"b"}
// Args.2: []string{"c"}
}
func ExampleDecoder() {
// type DecodeArg struct {
// arg string
// }
//
// func (d *DecodeArg) DecodeHeader(header http.Header, key string) error {
// value := header.Get(key)
// d.arg = value
// return nil
// }
var s struct {
Arg DecodeArg
}
h := http.Header{}
h.Set("Arg", "foobar")
err := httpheader.Decode(h, &s)
fmt.Println(err)
fmt.Println(s.Arg.arg)
// Output:
// <nil>
// foobar
}