generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuilder_bool.go
88 lines (74 loc) · 2.46 KB
/
builder_bool.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
package ferrite
import (
"fmt"
"github.com/dogmatiq/ferrite/internal/variable"
)
// Bool configures an environment variable as a boolean.
//
// name is the name of the environment variable to read. desc is a
// human-readable description of the environment variable.
func Bool(name, desc string) *BoolBuilder[bool] {
return BoolAs[bool](name, desc)
}
// BoolAs configures an environment variable as a boolean using a user-defined
// type.
//
// name is the name of the environment variable to read. desc is a
// human-readable description of the environment variable.
func BoolAs[T ~bool](name, desc string) *BoolBuilder[T] {
b := &BoolBuilder[T]{
schema: variable.TypedSet[T]{
Members: []variable.SetMember[T]{
{Value: true},
{Value: false},
},
ToLiteral: func(v T) variable.Literal {
return variable.Literal{String: fmt.Sprint(v)}
},
},
}
b.builder.Name(name)
b.builder.Description(desc)
return b
}
// BoolBuilder builds a specification for a boolean value.
type BoolBuilder[T ~bool] struct {
schema variable.TypedSet[T]
builder variable.TypedSpecBuilder[T]
}
var _ isBuilderOf[bool, *BoolBuilder[bool]]
// WithLiterals overrides the default literals used to represent true and false.
//
// The default literals "true" and "false" are no longer valid values when using
// custom literals.
func (b *BoolBuilder[T]) WithLiterals(t, f string) *BoolBuilder[T] {
b.schema.ToLiteral = func(v T) variable.Literal {
if v {
return variable.Literal{String: t}
}
return variable.Literal{String: f}
}
return b
}
// WithDefault sets the default value of the variable.
//
// It is used when the environment variable is undefined or empty.
func (b *BoolBuilder[T]) WithDefault(v T) *BoolBuilder[T] {
b.builder.Default(v)
return b
}
// Required completes the build process and registers a required variable with
// Ferrite's validation system.
func (b *BoolBuilder[T]) Required(options ...RequiredOption) Required[T] {
return required(b.schema, &b.builder, options...)
}
// Optional completes the build process and registers an optional variable with
// Ferrite's validation system.
func (b *BoolBuilder[T]) Optional(options ...OptionalOption) Optional[T] {
return optional(b.schema, &b.builder, options...)
}
// Deprecated completes the build process and registers a deprecated variable
// with Ferrite's validation system.
func (b *BoolBuilder[T]) Deprecated(options ...DeprecatedOption) Deprecated[T] {
return deprecated(b.schema, &b.builder, options...)
}