forked from ggicci/httpin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbody.go
More file actions
124 lines (103 loc) · 2.94 KB
/
body.go
File metadata and controls
124 lines (103 loc) · 2.94 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
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
// directive: "body"
// https://ggicci.github.io/httpin/directives/body
package httpin
import (
"encoding/json"
"encoding/xml"
"fmt"
"io"
"reflect"
"strings"
)
const (
bodyTypeJSON = "json"
bodyTypeXML = "xml"
)
type (
// JSONBody is the annotation for JSON body.
JSONBody struct{}
// XMLBody is the annotation for XML body.
XMLBody struct{}
// BodyDecoder decodes the request body into the specified object. Common body types are:
// json, xml, yaml, and others.
BodyDecoder interface {
Decode(src io.Reader, dst interface{}) error
}
)
var (
bodyTypeAnnotationJSON = reflect.TypeOf(JSONBody{})
bodyTypeAnnotationXML = reflect.TypeOf(XMLBody{})
bodyDecoders = map[string]BodyDecoder{
bodyTypeJSON: &defaultJSONBodyDecoder{},
bodyTypeXML: &defaultXMLBodyDecoder{},
}
)
// RegisterBodyDecoder registers a new body decoder. Panic if the body type is already registered.
//
// func init() {
// RegisterBodyDecoder("yaml", &myYAMLBodyDecoder{})
// }
func RegisterBodyDecoder(bodyType string, decoder BodyDecoder) {
if _, ok := bodyDecoders[bodyType]; ok {
panic(fmt.Errorf("httpin: %w: %q", ErrDuplicateBodyDecoder, bodyType))
}
ReplaceBodyDecoder(bodyType, decoder)
}
// ReplaceBodyDecoder replaces or add the body decoder of the specified type.
// Which is useful when you want to override the default body decoder. For example,
// the default JSON decoder is borrowed from encoding/json. You can replace it with
// your own implementation, e.g. json-iterator/go.
//
// func init() {
// ReplaceBodyDecoder("json", &myJSONBodyDecoder{})
// }
func ReplaceBodyDecoder(bodyType string, decoder BodyDecoder) {
if bodyType == "" {
panic("httpin: body type cannot be empty")
}
bodyDecoders[bodyType] = decoder
}
func bodyDirectiveNormalizer(dir *Directive) error {
if len(dir.Argv) == 0 {
dir.Argv = []string{bodyTypeJSON}
}
dir.Argv[0] = strings.ToLower(dir.Argv[0])
var bodyType = dir.Argv[0]
if _, ok := bodyDecoders[bodyType]; !ok {
return fmt.Errorf("%w: %q", ErrUnknownBodyType, bodyType)
}
return nil
}
func bodyTypeString(bodyType reflect.Type) string {
switch bodyType {
case bodyTypeAnnotationJSON:
return bodyTypeJSON
case bodyTypeAnnotationXML:
return bodyTypeXML
default:
panic(fmt.Errorf("httpin: %w: %q", ErrUnknownBodyType, bodyType))
}
}
func bodyDecoder(ctx *DirectiveContext) error {
var (
bodyType = ctx.Argv[0]
decoder = bodyDecoders[bodyType]
)
if decoder == nil {
return ErrUnknownBodyType
}
obj := ctx.Value.Interface()
if err := decoder.Decode(ctx.Request.Body, &obj); err != nil {
return err
}
ctx.DeliverContextValue(StopRecursion, true)
return nil
}
type defaultJSONBodyDecoder struct{}
func (de *defaultJSONBodyDecoder) Decode(src io.Reader, dst interface{}) error {
return json.NewDecoder(src).Decode(dst)
}
type defaultXMLBodyDecoder struct{}
func (de *defaultXMLBodyDecoder) Decode(src io.Reader, dst interface{}) error {
return xml.NewDecoder(src).Decode(dst)
}