Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ import "cattlecloud.net/go/forms"

```go
var (
name string
age int
name string
age int
aliases []string
worth float64
password *conceal.Text
)

err := forms.Parse(request, forms.Schema{
"NAME": forms.String(&name),
"AGE": forms.Int(&age),
"name": forms.String(&name),
"age": forms.Int(&age),
"aliases": forms.Strings(&aliases),
"worth": forms.Float64(&worth),
"password": forms.Secret(&pword),
})
```

Expand Down
26 changes: 26 additions & 0 deletions forms.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strconv"

"github.com/shoenig/go-conceal"
"github.com/shoenig/lang"
)

var (
Expand Down Expand Up @@ -104,6 +105,31 @@ func StringOr[T StringType](s *T, alt T) Parser {
}
}

type stringsParser[T StringType] struct {
required bool
destination *[]T
}

func (p *stringsParser[T]) Parse(values []string) error {
switch {
case len(values) == 0 && p.required:
return ErrNoValue
case len(values) == 0:
return nil
default:
*p.destination = lang.Map(values, func(s string) T { return T(s) })
}
return nil
}

// Strings is used to extract a form data value into a slice of Go strings.
func Strings[T StringType](s *[]T) Parser {
return &stringsParser[T]{
required: true,
destination: s,
}
}

// Secret is used to extract a form data value into a Go conceal.Text. If the
// value is missing then an error is returned during parsing.
func Secret(s **conceal.Text) Parser {
Expand Down
16 changes: 16 additions & 0 deletions forms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,19 @@ func Test_Parse_IntType_IntOr(t *testing.T) {
must.NoError(t, err)
must.Eq(t, 100, age)
}

func Test_Parse_StringType_Strings(t *testing.T) {
t.Parallel()

data := url.Values{
"names": []string{"alice", "bob", "carol"},
}

var names []string

err := ParseValues(data, Schema{
"names": Strings(&names),
})
must.NoError(t, err)
must.Eq(t, []string{"alice", "bob", "carol"}, names)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.23

require (
github.com/shoenig/go-conceal v0.5.4
github.com/shoenig/lang v0.0.6
github.com/shoenig/test v1.12.2
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/shoenig/go-conceal v0.5.4 h1:xLzarDUw3vUJjz+DirzO58yijkX4I9F1KA+RPZMLGLY=
github.com/shoenig/go-conceal v0.5.4/go.mod h1:LXmjZn/bO1Nrtvfex4VNbKViVE+aMhVvskZx8o7HBfs=
github.com/shoenig/lang v0.0.6 h1:i8NVe+fNQLUGXk+KPP6lB6Jcfmcrmc9nCEYknHsBbVQ=
github.com/shoenig/lang v0.0.6/go.mod h1:DStvcG5yPYr/xBBcTEaousm+Pqjn9ozAKfyqWwfhj34=
github.com/shoenig/test v1.12.2 h1:ZVT8NeIUwGWpZcKaepPmFMoNQ3sVpxvqUh/MAqwFiJI=
github.com/shoenig/test v1.12.2/go.mod h1:UxJ6u/x2v/TNs/LoLxBNJRV9DiwBBKYxXSyczsBHFoI=
Loading