forked from ggicci/httpin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoders.go
More file actions
89 lines (71 loc) · 2.62 KB
/
decoders.go
File metadata and controls
89 lines (71 loc) · 2.62 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
package httpin
import (
"fmt"
"reflect"
"github.com/ggicci/httpin/internal"
)
// ValueTypeDecoder is the interface implemented by types that can decode a
// string to themselves.
type ValueTypeDecoder = internal.ValueTypeDecoder
// FileTypeDecoder is the interface implemented by types that can decode a
// *multipart.FileHeader to themselves.
type FileTypeDecoder = internal.FileTypeDecoder
// ValueTypeDecoderFunc is an adaptor to allow the use of ordinary functions as
// httpin `ValueTypeDecoder`s.
type ValueTypeDecoderFunc = internal.ValueTypeDecoderFunc
// FileTypeDecoderFunc is an adaptor to allow the use of ordinary functions as
// httpin `FileTypeDecoder`s.
type FileTypeDecoderFunc = internal.FileTypeDecoderFunc
var (
decoders = make(map[reflect.Type]interface{}) // custom decoders
namedDecoders = make(map[string]interface{}) // custom decoders (registered by name)
)
func isTypeDecoder(decoder interface{}) bool {
_, isValueTypeDecoder := decoder.(ValueTypeDecoder)
_, isFileTypeDecoder := decoder.(FileTypeDecoder)
return isValueTypeDecoder || isFileTypeDecoder
}
// RegisterTypeDecoder registers a specific type decoder. Panics on conflicts.
func RegisterTypeDecoder(typ reflect.Type, decoder interface{}) {
if _, ok := decoders[typ]; ok {
panic(fmt.Errorf("httpin: %w: %q", ErrDuplicateTypeDecoder, typ))
}
ReplaceTypeDecoder(typ, decoder)
}
// ReplaceTypeDecoder replaces a specific type decoder.
func ReplaceTypeDecoder(typ reflect.Type, decoder interface{}) {
ensureValidDecoder(typ, decoder)
decoders[typ] = decoder
}
// RegisterNamedDecoder registers a decoder by name. Panics on conflicts.
func RegisterNamedDecoder(name string, decoder interface{}) {
if _, ok := namedDecoders[name]; ok {
panic(fmt.Errorf("httpin: %w: %q", ErrDuplicateNamedDecoder, name))
}
ReplaceNamedDecoder(name, decoder)
}
// ReplaceNamedDecoder replaces a decoder by name.
func ReplaceNamedDecoder(name string, decoder interface{}) {
ensureValidDecoder(nil, decoder)
namedDecoders[name] = decoder
}
func ensureValidDecoder(typ reflect.Type, decoder interface{}) {
if decoder == nil {
panic(fmt.Errorf("httpin: %w: %q", ErrNilTypeDecoder, typ))
}
if !isTypeDecoder(decoder) {
panic(fmt.Errorf("httpin: %w: %q", ErrInvalidTypeDecoder, typ))
}
}
// decoderOf retrieves a decoder by type, from the global registerred decoders.
func decoderOf(t reflect.Type) interface{} {
dec := decoders[t]
if dec != nil {
return dec
}
return internal.DecoderOf(t)
}
// decoderByName retrieves a decoder by name, from the global registerred named decoders.
func decoderByName(name string) interface{} {
return namedDecoders[name]
}