generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuilder_unsigned.go
108 lines (91 loc) · 3.2 KB
/
builder_unsigned.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package ferrite
import (
"fmt"
"strconv"
"github.com/dogmatiq/ferrite/internal/maybe"
"github.com/dogmatiq/ferrite/internal/reflectx"
"github.com/dogmatiq/ferrite/internal/variable"
"golang.org/x/exp/constraints"
)
// Unsigned configures an environment variable as a unsigned integer.
//
// name is the name of the environment variable to read. desc is a
// human-readable description of the environment variable.
func Unsigned[T constraints.Unsigned](name, desc string) *UnsignedBuilder[T] {
b := &UnsignedBuilder[T]{
schema: variable.TypedNumeric[T]{
Marshaler: unsignedMarshaler[T]{},
},
}
b.builder.Name(name)
b.builder.Description(desc)
b.builder.Documentation().
Summary("Unsigned integer syntax").
Paragraph(
"Unsigned integers can only be specified using decimal (base-10) notation.",
"A leading sign (`+` or `-`) is not supported and **MUST NOT** be specified.",
).
Format().
Paragraph(
"Internally, the `%s` variable is represented using an unsigned %d-bit integer type (`%s`);",
"any value that overflows this data-type is invalid.",
).
Format(
name,
reflectx.BitSize[T](),
reflectx.KindOf[T](),
).
Done()
return b
}
// UnsignedBuilder builds a specification for an unsigned integer value.
type UnsignedBuilder[T constraints.Unsigned] struct {
schema variable.TypedNumeric[T]
builder variable.TypedSpecBuilder[T]
}
var _ isBuilderOf[uint, *UnsignedBuilder[uint]]
// WithDefault sets the default value of the variable.
//
// It is used when the environment variable is undefined or empty.
func (b *UnsignedBuilder[T]) WithDefault(v T) *UnsignedBuilder[T] {
b.builder.Default(v)
return b
}
// WithMinimum sets the minimum acceptable value of the variable.
func (b *UnsignedBuilder[T]) WithMinimum(v T) *UnsignedBuilder[T] {
b.schema.NativeMin = maybe.Some(v)
return b
}
// WithMaximum sets the maximum acceptable value of the variable.
func (b *UnsignedBuilder[T]) WithMaximum(v T) *UnsignedBuilder[T] {
b.schema.NativeMax = maybe.Some(v)
return b
}
// Required completes the build process and registers a required variable with
// Ferrite's validation system.
func (b *UnsignedBuilder[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 *UnsignedBuilder[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 *UnsignedBuilder[T]) Deprecated(options ...DeprecatedOption) Deprecated[T] {
return deprecated(b.schema, &b.builder, options...)
}
type unsignedMarshaler[T constraints.Unsigned] struct{}
func (unsignedMarshaler[T]) Marshal(v T) (variable.Literal, error) {
return variable.Literal{
String: formatUnsigned(v),
}, nil
}
func (unsignedMarshaler[T]) Unmarshal(v variable.Literal) (T, error) {
n, err := strconv.ParseUint(v.String, 10, reflectx.BitSize[T]())
return T(n), variable.UnwrapNumericParseError(err, formatUnsigned[T])
}
func formatUnsigned[T constraints.Unsigned](v T) string {
return fmt.Sprintf("%d", v)
}