-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
48 lines (36 loc) · 1.22 KB
/
option.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
package into
import "slices"
// Option is a configuration parameter.
// Use the With... functions to specify options.
type Option interface{ isOption() }
func should(options []Option, opt Option) bool {
return slices.Contains[[]Option, Option](options, opt)
}
type flags int
func (flags) isOption() {}
const (
convertStrings flags = iota
skipReflect
skipMarshalCheck
)
type fallbackValue struct{ x any }
func (fallbackValue) isOption() {}
// WithFallback specifies a fallback value when coercing nil input.
// By default, the zero value is returned.
func WithFallback(fallback any) Option {
return fallbackValue{fallback}
}
// WithConvertStrings enables conversion of strings during type coercion.
func WithConvertStrings() Option {
return convertStrings
}
// WithoutReflection will skip using reflection to coerce values.
// Using this disables support for nonstandard types (e.g. custom subtypes of int or string).
func WithoutReflection() Option {
return skipReflect
}
// WithoutMarshalerCheck is an option that skips a check in [CanString] and similar
// that runs [encoding.TextMarshaler]'s marshal or [strconv] conversions to ensure they don't return errors.
func WithoutMarshalerCheck() Option {
return skipMarshalCheck
}