Skip to content

Commit 89a2021

Browse files
authored
strings: support for parsing multiple string values (#18)
1 parent aa39488 commit 89a2021

5 files changed

Lines changed: 55 additions & 4 deletions

File tree

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,19 @@ import "cattlecloud.net/go/forms"
2424

2525
```go
2626
var (
27-
name string
28-
age int
27+
name string
28+
age int
29+
aliases []string
30+
worth float64
31+
password *conceal.Text
2932
)
3033

3134
err := forms.Parse(request, forms.Schema{
32-
"NAME": forms.String(&name),
33-
"AGE": forms.Int(&age),
35+
"name": forms.String(&name),
36+
"age": forms.Int(&age),
37+
"aliases": forms.Strings(&aliases),
38+
"worth": forms.Float64(&worth),
39+
"password": forms.Secret(&pword),
3440
})
3541
```
3642

forms.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"strconv"
1414

1515
"github.com/shoenig/go-conceal"
16+
"github.com/shoenig/lang"
1617
)
1718

1819
var (
@@ -104,6 +105,31 @@ func StringOr[T StringType](s *T, alt T) Parser {
104105
}
105106
}
106107

108+
type stringsParser[T StringType] struct {
109+
required bool
110+
destination *[]T
111+
}
112+
113+
func (p *stringsParser[T]) Parse(values []string) error {
114+
switch {
115+
case len(values) == 0 && p.required:
116+
return ErrNoValue
117+
case len(values) == 0:
118+
return nil
119+
default:
120+
*p.destination = lang.Map(values, func(s string) T { return T(s) })
121+
}
122+
return nil
123+
}
124+
125+
// Strings is used to extract a form data value into a slice of Go strings.
126+
func Strings[T StringType](s *[]T) Parser {
127+
return &stringsParser[T]{
128+
required: true,
129+
destination: s,
130+
}
131+
}
132+
107133
// Secret is used to extract a form data value into a Go conceal.Text. If the
108134
// value is missing then an error is returned during parsing.
109135
func Secret(s **conceal.Text) Parser {

forms_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,19 @@ func Test_Parse_IntType_IntOr(t *testing.T) {
363363
must.NoError(t, err)
364364
must.Eq(t, 100, age)
365365
}
366+
367+
func Test_Parse_StringType_Strings(t *testing.T) {
368+
t.Parallel()
369+
370+
data := url.Values{
371+
"names": []string{"alice", "bob", "carol"},
372+
}
373+
374+
var names []string
375+
376+
err := ParseValues(data, Schema{
377+
"names": Strings(&names),
378+
})
379+
must.NoError(t, err)
380+
must.Eq(t, []string{"alice", "bob", "carol"}, names)
381+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.23
44

55
require (
66
github.com/shoenig/go-conceal v0.5.4
7+
github.com/shoenig/lang v0.0.6
78
github.com/shoenig/test v1.12.2
89
)
910

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
22
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
33
github.com/shoenig/go-conceal v0.5.4 h1:xLzarDUw3vUJjz+DirzO58yijkX4I9F1KA+RPZMLGLY=
44
github.com/shoenig/go-conceal v0.5.4/go.mod h1:LXmjZn/bO1Nrtvfex4VNbKViVE+aMhVvskZx8o7HBfs=
5+
github.com/shoenig/lang v0.0.6 h1:i8NVe+fNQLUGXk+KPP6lB6Jcfmcrmc9nCEYknHsBbVQ=
6+
github.com/shoenig/lang v0.0.6/go.mod h1:DStvcG5yPYr/xBBcTEaousm+Pqjn9ozAKfyqWwfhj34=
57
github.com/shoenig/test v1.12.2 h1:ZVT8NeIUwGWpZcKaepPmFMoNQ3sVpxvqUh/MAqwFiJI=
68
github.com/shoenig/test v1.12.2/go.mod h1:UxJ6u/x2v/TNs/LoLxBNJRV9DiwBBKYxXSyczsBHFoI=

0 commit comments

Comments
 (0)