Skip to content

Commit 6ff650e

Browse files
authored
strings: add StringsOr for optional slice of strings (#19)
1 parent 89a2021 commit 6ff650e

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

forms.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,26 @@ func (p *stringsParser[T]) Parse(values []string) error {
123123
}
124124

125125
// Strings is used to extract a form data value into a slice of Go strings.
126+
//
127+
// If the form value is missing, then an error is returned during parsing.
126128
func Strings[T StringType](s *[]T) Parser {
127129
return &stringsParser[T]{
128130
required: true,
129131
destination: s,
130132
}
131133
}
132134

135+
// StringsOr is used to extract multiple form values for a given key into a slice of Go strings.
136+
//
137+
// If the form value is missing, then the given alt value is used instead.
138+
func StringsOr[T StringType](s *[]T, alt []T) Parser {
139+
*s = alt
140+
return &stringsParser[T]{
141+
required: false,
142+
destination: s,
143+
}
144+
}
145+
133146
// Secret is used to extract a form data value into a Go conceal.Text. If the
134147
// value is missing then an error is returned during parsing.
135148
func Secret(s **conceal.Text) Parser {

forms_test.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ func Test_Parse_IntType_IntOr(t *testing.T) {
364364
must.Eq(t, 100, age)
365365
}
366366

367-
func Test_Parse_StringType_Strings(t *testing.T) {
367+
func Test_Parse_StringType_Strings_present(t *testing.T) {
368368
t.Parallel()
369369

370370
data := url.Values{
@@ -379,3 +379,50 @@ func Test_Parse_StringType_Strings(t *testing.T) {
379379
must.NoError(t, err)
380380
must.Eq(t, []string{"alice", "bob", "carol"}, names)
381381
}
382+
383+
func Test_Parse_StringType_Strings_missing(t *testing.T) {
384+
t.Parallel()
385+
386+
data := url.Values{
387+
"names": []string{"alice", "bob", "carol"},
388+
}
389+
390+
var jobs []string
391+
392+
err := ParseValues(data, Schema{
393+
"jobs": Strings(&jobs),
394+
})
395+
must.Error(t, err)
396+
}
397+
398+
func Test_Parse_StringType_StringsOr_present(t *testing.T) {
399+
t.Parallel()
400+
401+
data := url.Values{
402+
"names": []string{"alice", "bob", "carol"},
403+
}
404+
405+
var names []string
406+
407+
err := ParseValues(data, Schema{
408+
"names": StringsOr(&names, []string{"zed", "yulia"}),
409+
})
410+
must.NoError(t, err)
411+
must.Eq(t, []string{"alice", "bob", "carol"}, names)
412+
}
413+
414+
func Test_Parse_StringType_StringsOr_missing(t *testing.T) {
415+
t.Parallel()
416+
417+
data := url.Values{
418+
"names": []string{"alice", "bob", "carol"},
419+
}
420+
421+
var jobs []string
422+
423+
err := ParseValues(data, Schema{
424+
"jobs": StringsOr(&jobs, []string{"janitor", "cashier"}),
425+
})
426+
must.NoError(t, err)
427+
must.Eq(t, []string{"janitor", "cashier"}, jobs)
428+
}

0 commit comments

Comments
 (0)