-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
55 lines (43 loc) · 1.12 KB
/
example_test.go
File metadata and controls
55 lines (43 loc) · 1.12 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
package request_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"go.expect.digital/request"
)
func ExampleDecode() {
r := httptest.NewRequestWithContext(
context.Background(),
http.MethodPost,
"/?filterType=pending,approved&clientId=4&filterClientIds=1|2|3",
strings.NewReader(`{"id":1}`),
)
var req struct {
// query params
FilterType []string `oas:"filterType,query,implode"`
FilterClientIDs []int `oas:"filterClientIds,query,pipeDelimited,implode"`
ClientID int `oas:"clientId,query"`
// body
Client struct {
ID int `json:"id"`
} `oas:",body,json"`
}
_ = request.Decode(r, &req)
fmt.Printf("%+v\n", req)
// Output:
// {FilterType:[pending approved] FilterClientIDs:[1 2 3] ClientID:4 Client:{ID:1}}
}
func ExampleDecoder_Decode() {
r := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/?ids=1,2,3", nil)
var req struct {
IDs []int
}
// set query values imploded "?ids=1,2,3" by default
dec := request.NewDecoder(request.QueryImplode())
_ = dec.Decode(r, &req)
fmt.Printf("%+v\n", req)
// Output:
// {IDs:[1 2 3]}
}