-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
89 lines (77 loc) · 2 KB
/
options.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
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 jsont
import "fmt"
type Option interface {
Apply(on any) error
}
var (
_OptionChecked = &optionChecked{true}
_OptionUnChecked = &optionChecked{false}
_OptionStrict = &optionStrict{true}
_OptionNonStrict = &optionStrict{false}
_OptionDefaultArgValue = func(name string, value interface{}) Option {
return &optionDefaultArgValue{
name: name,
value: value,
}
}
_OptionDefaultArgValues = func(defaults map[string]interface{}) Option {
return &optionDefaultArgValues{
defaults: defaults,
}
}
)
var (
OptionChecked Option = _OptionChecked
OptionUnChecked Option = _OptionUnChecked
OptionStrict Option = _OptionStrict
OptionNonStrict Option = _OptionNonStrict
OptionDefaultArgValue = _OptionDefaultArgValue
OptionDefaultArgValues = _OptionDefaultArgValues
)
type optionChecked struct {
checkReqd bool
}
func (o *optionChecked) Apply(on any) error {
switch ont := on.(type) {
case *jsonTemplate:
ont.checkReqd = o.checkReqd
case *jsonNamedTemplate:
ont.checkReqd = o.checkReqd
}
return nil
}
type optionStrict struct {
strict bool
}
func (o *optionStrict) Apply(on any) error {
switch ont := on.(type) {
case *jsonNamedTemplate:
ont.strict = o.strict
return nil
case *jsonTemplate:
ont.strict = o.strict
return nil
}
return fmt.Errorf("option Strict cannot be applied to type '%T'", on)
}
type optionDefaultArgValue struct {
name string
value interface{}
}
func (o *optionDefaultArgValue) Apply(on any) error {
if ont, ok := on.(NamedTemplate); ok {
ont.DefaultArgValue(o.name, o.value)
return nil
}
return fmt.Errorf("option OptionDefaultArgValue cannot be applied to type '%T'", on)
}
type optionDefaultArgValues struct {
defaults map[string]interface{}
}
func (o *optionDefaultArgValues) Apply(on any) error {
if ont, ok := on.(NamedTemplate); ok {
ont.DefaultArgValues(o.defaults)
return nil
}
return fmt.Errorf("option OptionDefaultArgValues cannot be applied to type '%T'", on)
}