-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
default.go
45 lines (39 loc) · 981 Bytes
/
default.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
// directive: "default"
// https://ggicci.github.io/httpin/directives/default
package core
import (
"mime/multipart"
)
type DirectiveDefault struct{}
func (*DirectiveDefault) Decode(rtm *DirectiveRuntime) error {
if rtm.IsFieldSet() {
return nil // noop, the field was set by a former executor
}
// Transform:
// 1. ctx.Argv -> input values
// 2. ["default"] -> keys
extractor := &FormExtractor{
Runtime: rtm,
Form: multipart.Form{
Value: map[string][]string{
"default": rtm.Directive.Argv,
},
},
}
return extractor.Extract("default")
}
func (*DirectiveDefault) Encode(rtm *DirectiveRuntime) error {
if !rtm.Value.IsZero() {
return nil // skip if the field is not empty
}
var adapt AnyStringableAdaptor
coder := rtm.GetCustomCoder()
if coder != nil {
adapt = coder.Adapt
}
if stringSlicable, err := NewStringSlicable(rtm.Value, adapt); err != nil {
return err
} else {
return stringSlicable.FromStringSlice(rtm.Directive.Argv)
}
}