Debian Codegraph @

example_test.go

raw symbols SCIP
1 package httpheader_test
2
3 import (
4 "fmt"
5 "net/http"
6 "sort"
7 "time"
8
9 "github.com/mozillazg/go-httpheader"
10 )
11
12 func ExampleHeader() {
13 type Options struct {
14 ContentType string `header:"Content-Type"`
15 Length int
16 Bool bool
17 BoolInt bool `header:"Bool-Int,int"`
18 XArray []string `header:"X-Array"`
19 TestHide string `header:"-"`
20 IgnoreEmpty string `header:"X-Empty,omitempty"`
21 IgnoreEmptyN string `header:"X-Empty-N,omitempty"`
22 CreatedAt time.Time `header:"Created-At"`
23 UpdatedAt time.Time `header:"Update-At,unix"`
24 CustomHeader http.Header
25 }
26
27 opt := Options{
28 ContentType: "application/json",
29 Length: 2,
30 Bool: true,
31 BoolInt: true,
32 XArray: []string{"test1", "test2"},
33 TestHide: "hide",
34 IgnoreEmptyN: "n",
35 CreatedAt: time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC),
36 UpdatedAt: time.Date(2001, 1, 1, 12, 34, 56, 0, time.UTC),
37 CustomHeader: http.Header{
38 "X-Test-1": []string{"233"},
39 "X-Test-2": []string{"666"},
40 },
41 }
42 h, err := httpheader.Header(opt)
43 fmt.Println(err)
44 printHeader(h)
45 // Output:
46 // <nil>
47 // Bool: []string{"true"}
48 // Bool-Int: []string{"1"}
49 // Content-Type: []string{"application/json"}
50 // Created-At: []string{"Sat, 01 Jan 2000 12:34:56 GMT"}
51 // Length: []string{"2"}
52 // Update-At: []string{"978352496"}
53 // X-Array: []string{"test1", "test2"}
54 // X-Empty-N: []string{"n"}
55 // X-Test-1: []string{"233"}
56 // X-Test-2: []string{"666"}
57 }
58
59 func printHeader(h http.Header) {
60 var keys []string
61 for k := range h {
62 keys = append(keys, k)
63 }
64 sort.Strings(keys)
65 for _, k := range keys {
66 fmt.Printf("%s: %#v\n", k, h[k])
67 }
68 }
69
70 func ExampleDecode() {
71 type Options struct {
72 ContentType string `header:"Content-Type"`
73 Length int
74 Bool bool
75 BoolInt bool `header:"Bool-Int,int"`
76 XArray []string `header:"X-Array"`
77 TestHide string `header:"-"`
78 IgnoreEmpty string `header:"X-Empty,omitempty"`
79 IgnoreEmptyN string `header:"X-Empty-N,omitempty"`
80 CreatedAt time.Time `header:"Created-At"`
81 UpdatedAt time.Time `header:"Update-At,unix"`
82 CustomHeader http.Header
83 }
84 h := http.Header{
85 "Bool": []string{"true"},
86 "Bool-Int": []string{"1"},
87 "X-Test-1": []string{"233"},
88 "X-Test-2": []string{"666"},
89 "Content-Type": []string{"application/json"},
90 "Length": []string{"2"},
91 "X-Array": []string{"test1", "test2"},
92 "X-Empty-N": []string{"n"},
93 "Update-At": []string{"978352496"},
94 "Created-At": []string{"Sat, 01 Jan 2000 12:34:56 GMT"},
95 }
96 var opt Options
97 err := httpheader.Decode(h, &opt)
98 fmt.Println(err)
99 fmt.Println(opt.ContentType)
100 fmt.Println(opt.Length)
101 fmt.Println(opt.BoolInt)
102 fmt.Println(opt.XArray)
103 fmt.Println(opt.UpdatedAt)
104 // Output:
105 // <nil>
106 // application/json
107 // 2
108 // true
109 // [test1 test2]
110 // 2001-01-01 12:34:56 +0000 UTC
111 }
112
113 type EncodedArgs []string
114
115 func (e EncodedArgs) EncodeHeader(key string, v *http.Header) error {
116 for i, arg := range e {
117 v.Set(fmt.Sprintf("%s.%d", key, i), arg)
118 }
119 return nil
120 }
121
122 type DecodeArg struct {
123 arg string
124 }
125
126 func (d *DecodeArg) DecodeHeader(header http.Header, key string) error {
127 value := header.Get(key)
128 d.arg = value
129 return nil
130 }
131
132 func ExampleEncoder() {
133 // type EncodedArgs []string
134 //
135 // func (e EncodedArgs) EncodeHeader(key string, v *http.Header) error {
136 // for i, arg := range e {
137 // v.Set(fmt.Sprintf("%s.%d", key, i), arg)
138 // }
139 // return nil
140 // }
141
142 s := struct {
143 Args EncodedArgs `header:"Args"`
144 }{Args: EncodedArgs{"a", "b", "c"}}
145
146 h, err := httpheader.Header(s)
147 fmt.Println(err)
148 printHeader(h)
149 // Output:
150 // <nil>
151 // Args.0: []string{"a"}
152 // Args.1: []string{"b"}
153 // Args.2: []string{"c"}
154 }
155
156 func ExampleDecoder() {
157 // type DecodeArg struct {
158 // arg string
159 // }
160 //
161 // func (d *DecodeArg) DecodeHeader(header http.Header, key string) error {
162 // value := header.Get(key)
163 // d.arg = value
164 // return nil
165 // }
166
167 var s struct {
168 Arg DecodeArg
169 }
170 h := http.Header{}
171 h.Set("Arg", "foobar")
172 err := httpheader.Decode(h, &s)
173 fmt.Println(err)
174 fmt.Println(s.Arg.arg)
175 // Output:
176 // <nil>
177 // foobar
178 }