generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
builder_binary.go
220 lines (189 loc) · 6.63 KB
/
builder_binary.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package ferrite
import (
"encoding/base64"
"encoding/hex"
"github.com/dogmatiq/ferrite/internal/maybe"
"github.com/dogmatiq/ferrite/internal/variable"
)
// Binary configures an environment variable as a raw binary value, represented
// as a byte-slice.
//
// Binary values are represented in environment variables using a suitable
// encoding schema. The default encoding is the standard "base64" encoding
// described by RFC 4648.
//
// name is the name of the environment variable to read. desc is a
// human-readable description of the environment variable.
func Binary(name, desc string) *BinaryBuilder[[]byte, byte] {
return BinaryAs[[]byte](name, desc)
}
// BinaryAs configures an environment variable as a raw binary value,
// represented as a user-defined byte-slice type.
//
// Binary values are represented in environment variables using a suitable
// encoding schema. The default encoding is the standard "base64" encoding
// described by RFC 4648.
//
// name is the name of the environment variable to read. desc is a
// human-readable description of the environment variable.
func BinaryAs[T ~[]B, B ~byte](name, desc string) *BinaryBuilder[T, B] {
b := &BinaryBuilder[T, B]{
schema: variable.TypedBinary[T, B]{},
}
b.builder.Name(name)
b.builder.Description(desc)
b.WithBase64Encoding(base64.StdEncoding)
return b
}
// BinaryBuilder builds a specification for a binary variable.
type BinaryBuilder[T ~[]B, B ~byte] struct {
schema variable.TypedBinary[T, B]
builder variable.TypedSpecBuilder[T]
}
var _ isBuilderOf[[]byte, *BinaryBuilder[[]byte, byte]]
// WithDefault sets the default value of the variable.
//
// v is the raw binary value, not the encoded representation used within the
// environment variable.
//
// It is used when the environment variable is undefined or empty.
func (b *BinaryBuilder[T, B]) WithDefault(v T) *BinaryBuilder[T, B] {
b.builder.Default(v)
return b
}
// WithMinimumLength sets the minimum permitted length of the raw binary value,
// in bytes.
func (b *BinaryBuilder[T, B]) WithMinimumLength(min int) *BinaryBuilder[T, B] {
b.schema.MinLen = maybe.Some(min)
return b
}
// WithMaximumLength sets the maximum permitted length of the raw binary value,
// in bytes.
func (b *BinaryBuilder[T, B]) WithMaximumLength(max int) *BinaryBuilder[T, B] {
b.schema.MaxLen = maybe.Some(max)
return b
}
// WithLength sets the exact permitted length of the raw binary value, in bytes.
func (b *BinaryBuilder[T, B]) WithLength(n int) *BinaryBuilder[T, B] {
l := maybe.Some(n)
b.schema.MinLen = l
b.schema.MaxLen = l
return b
}
// WithEncodedDefault sets a default value of the variable from its encoded
// string representation.
//
// v is the encoded binary value as used in the environment variable. For
// example, it may be a base64 string.
//
// It is used when the environment variable is undefined or empty.
func (b *BinaryBuilder[T, B]) WithEncodedDefault(v string) *BinaryBuilder[T, B] {
enc, err := b.schema.Marshaler.Unmarshal(
variable.Literal{
String: v,
},
)
if err != nil {
panic(err)
}
return b.WithDefault(enc)
}
// WithConstraint adds a constraint to the variable.
//
// fn is called with the environment variable value after it is parsed. If fn
// returns false the value is considered invalid.
func (b *BinaryBuilder[T, B]) WithConstraint(
desc string,
fn func(T) bool,
) *BinaryBuilder[T, B] {
b.builder.UserConstraint(desc, fn)
return b
}
// WithSensitiveContent marks the variable as containing sensitive content.
//
// Values of sensitive variables are not printed to the console or included in
// generated documentation.
func (b *BinaryBuilder[T, B]) WithSensitiveContent() *BinaryBuilder[T, B] {
b.builder.MarkSensitive()
return b
}
// WithBase64Encoding configures the variable to use base64 encoding to
// represent the binary value within the environment.
func (b *BinaryBuilder[T, B]) WithBase64Encoding(enc *base64.Encoding) *BinaryBuilder[T, B] {
switch *enc {
case *base64.StdEncoding:
b.schema.EncodingDesc = "base64"
case *base64.RawStdEncoding:
b.schema.EncodingDesc = "unpadded base64"
case *base64.URLEncoding:
b.schema.EncodingDesc = "padded base64url"
case *base64.RawURLEncoding:
// base64url is USUALLY used unpadded, because the "=" character is
// itself problematic in URLs, so we refer to it simply as "base64url".
b.schema.EncodingDesc = "base64url"
default:
b.schema.EncodingDesc = "non-canonical base64"
}
b.schema.Marshaler = base64BinaryMarshaler[T, B]{Encoding: enc}
return b
}
// WithHexEncoding configures the variable to use hexadecimal encoding to
// represent the binary value within the environment.
func (b *BinaryBuilder[T, B]) WithHexEncoding() *BinaryBuilder[T, B] {
b.schema.EncodingDesc = "hex"
b.schema.Marshaler = hexBinaryMarshaler[T, B]{}
return b
}
// Required completes the build process and registers a required variable with
// Ferrite's validation system.
func (b *BinaryBuilder[T, B]) 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 *BinaryBuilder[T, B]) 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 *BinaryBuilder[T, B]) Deprecated(options ...DeprecatedOption) Deprecated[T] {
return deprecated(b.schema, &b.builder, options...)
}
type base64BinaryMarshaler[T ~[]B, B ~byte] struct {
Encoding *base64.Encoding
}
func (m base64BinaryMarshaler[T, B]) Marshal(v T) (variable.Literal, error) {
return variable.Literal{
String: m.Encoding.EncodeToString(toByteSlice(v)),
}, nil
}
func (m base64BinaryMarshaler[T, B]) Unmarshal(v variable.Literal) (T, error) {
data, err := m.Encoding.DecodeString(v.String)
return fromByteSlice[T](data), err
}
type hexBinaryMarshaler[T ~[]B, B ~byte] struct{}
func (m hexBinaryMarshaler[T, B]) Marshal(v T) (variable.Literal, error) {
return variable.Literal{
String: hex.EncodeToString(toByteSlice(v)),
}, nil
}
func (m hexBinaryMarshaler[T, B]) Unmarshal(v variable.Literal) (T, error) {
data, err := hex.DecodeString(v.String)
return fromByteSlice[T](data), err
}
// toByteSlice converts a slice of user-defined-byte type to []byte.
func toByteSlice[T ~[]B, B ~byte](in T) []byte {
out := make([]byte, len(in))
for i, o := range in {
out[i] = byte(o)
}
return out
}
// fromByteSlice converts a []byte to a slice of user-defined-byte types.
func fromByteSlice[T ~[]B, B ~byte](in []byte) T {
out := make(T, len(in))
for i, o := range in {
out[i] = B(o)
}
return out
}