Compare commits

...

2 Commits

Author SHA1 Message Date
Maxim Lebedev 5140ec4e47
🧑‍💻 Allow unmarshlers for custom slice types 2023-08-06 05:53:40 +06:00
Maxim Lebedev 47e2700618
🧑‍💻 Added omitempty option support 2022-05-24 21:27:12 +05:00
2 changed files with 201 additions and 101 deletions

94
form.go
View File

@ -6,11 +6,10 @@ import (
"bytes"
"fmt"
"io"
"net/url"
"reflect"
"strconv"
"strings"
http "github.com/valyala/fasthttp"
)
type (
@ -21,15 +20,17 @@ type (
Unmarshaler interface {
UnmarshalForm(v []byte) error
}
Decoder struct {
args url.Values
tag string
args *http.Args
}
)
const (
tagIgnore = "-"
methodName = "UnmarshalForm"
tagIgnore = "-"
tagOmitempty = "omitempty"
methodName = "UnmarshalForm"
)
func NewDecoder(r io.Reader) *Decoder {
@ -37,9 +38,7 @@ func NewDecoder(r io.Reader) *Decoder {
defer buf.Reset()
_, _ = buf.ReadFrom(r)
args := http.AcquireArgs()
args.ParseBytes(buf.Bytes())
args, _ := url.ParseQuery(buf.String())
return &Decoder{
tag: "form",
@ -89,54 +88,89 @@ func (d Decoder) Decode(dst any) (err error) {
}
}()
return d.decode("", src)
return d.decode("", src, "")
}
func (d Decoder) decode(key string, dst reflect.Value) error {
src := http.AcquireArgs()
defer http.ReleaseArgs(src)
d.args.CopyTo(src)
func (d Decoder) decode(key string, dst reflect.Value, opts tagOptions) error {
src := d.args
if keyIndex := strings.LastIndex(key, ","); keyIndex != -1 {
if index, err := strconv.Atoi(key[keyIndex+1:]); err == nil {
key = key[:keyIndex]
src.Reset()
src.SetBytesV(key, d.args.PeekMulti(key)[index])
src = make(url.Values)
src.Set(key, d.args[key][index])
}
}
switch dst.Kind() {
case reflect.Bool:
dst.SetBool(src.GetBool(key))
out, err := strconv.ParseBool(src.Get(key))
if err != nil {
return err
}
dst.SetBool(out)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
dst.SetInt(int64(src.GetUfloatOrZero(key)))
out, err := strconv.ParseInt(src.Get(key), 10, 64)
if err != nil {
return err
}
dst.SetInt(out)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
dst.SetUint(uint64(src.GetUintOrZero(key)))
out, err := strconv.ParseUint(src.Get(key), 10, 64)
if err != nil {
return err
}
dst.SetUint(out)
case reflect.Float32, reflect.Float64:
dst.SetFloat(src.GetUfloatOrZero(key))
out, err := strconv.ParseFloat(src.Get(key), 64)
if err != nil {
return err
}
dst.SetFloat(out)
// case reflect.Array: // TODO(toby3d)
// case reflect.Interface: // TODO(toby3d)
case reflect.Slice:
// NOTE(toby3d): copy raw []byte value as is
if dst.Type().Elem().Kind() == reflect.Uint8 {
dst.SetBytes(src.Peek(key))
dst.SetBytes([]byte(src.Get(key)))
return nil
}
// NOTE(toby3d): if contains UnmarshalForm method
for i := 0; i < dst.Addr().NumMethod(); i++ {
if dst.Addr().Type().Method(i).Name != methodName {
continue
}
in := make([]reflect.Value, 1)
in[0] = reflect.ValueOf([]byte(src.Get(key)))
out := dst.Addr().Method(i).Call(in)
if len(out) > 0 && out[0].Interface() != nil && !opts.Contains(tagOmitempty) {
return out[0].Interface().(error)
}
return nil
}
if dst.IsNil() {
slice := d.args.PeekMulti(key)
slice := d.args[key]
dst.Set(reflect.MakeSlice(dst.Type(), len(slice), cap(slice)))
}
for i := 0; i < dst.Len(); i++ {
if err := d.decode(fmt.Sprintf("%s,%d", key, i), dst.Index(i)); err != nil {
if err := d.decode(fmt.Sprintf("%s,%d", key, i), dst.Index(i), ""); err != nil {
return err
}
}
case reflect.String:
dst.SetString(string(src.Peek(key)))
dst.SetString(string(src.Get(key)))
case reflect.Pointer:
if dst.IsNil() {
dst.Set(reflect.New(dst.Type().Elem()))
@ -149,17 +183,17 @@ func (d Decoder) decode(key string, dst reflect.Value) error {
}
in := make([]reflect.Value, 1)
in[0] = reflect.ValueOf(src.Peek(key))
in[0] = reflect.ValueOf([]byte(src.Get(key)))
out := dst.Method(i).Call(in)
if len(out) > 0 && out[0].Interface() != nil {
if len(out) > 0 && out[0].Interface() != nil && !opts.Contains(tagOmitempty) {
return out[0].Interface().(error)
}
return nil
}
if err := d.decode(key, dst.Elem()); err != nil {
if err := d.decode(key, dst.Elem(), ""); err != nil {
return err
}
case reflect.Struct:
@ -170,10 +204,10 @@ func (d Decoder) decode(key string, dst reflect.Value) error {
}
in := make([]reflect.Value, 1)
in[0] = reflect.ValueOf(src.Peek(key))
in[0] = reflect.ValueOf([]byte(src.Get(key)))
out := dst.Addr().Method(i).Call(in)
if len(out) > 0 && out[0].Interface() != nil {
if len(out) > 0 && out[0].Interface() != nil && !opts.Contains(tagOmitempty) {
return out[0].Interface().(error)
}
@ -181,8 +215,8 @@ func (d Decoder) decode(key string, dst reflect.Value) error {
}
for i := 0; i < dst.NumField(); i++ {
if name, _ := parseTag(string(dst.Type().Field(i).Tag.Get(d.tag))); name != tagIgnore {
if err := d.decode(name, dst.Field(i)); err != nil {
if name, opts := parseTag(string(dst.Type().Field(i).Tag.Get(d.tag))); name != tagIgnore {
if err := d.decode(name, dst.Field(i), opts); err != nil {
return err
}
}

View File

@ -2,98 +2,154 @@ package form_test
import (
"errors"
"net/url"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
http "github.com/valyala/fasthttp"
"source.toby3d.me/toby3d/form"
)
type (
TestResult struct {
ArrayStruct []Struct `form:"arrayStruct[]"`
ArrayPtrStruct []*Struct `form:"arrayPtrStruct[]"`
Bytes []byte `form:"bytes"` // TODO(toby3d)
Ints []int `form:"ints[]"`
Struct Struct `form:"struct"`
PtrStruct *Struct `form:"ptrStruct"`
Skip any `form:"-"`
// Interface any `form:"interface"` // TODO(toby3d)
Empty string `form:"empty"`
NotFormTag string `json:"notFormTag"`
String string `form:"string"`
Float float32 `form:"float"`
Uint uint `form:"uint"`
Int int `form:"int"`
Bool bool `form:"bool"`
Skip any `form:"-"`
PtrStruct *Struct `form:"ptrStruct"`
PtrStructs *Structs `form:"ptrStructs"`
NullStruct NullStruct `form:"nullstruct,omitempty"`
Struct Struct `form:"struct"`
Empty string `form:"empty"`
String string `form:"string"`
NotFormTag string `json:"notFormTag"`
ArrayStruct []Struct `form:"arrayStruct[]"`
ArrayPtrStruct []*Struct `form:"arrayPtrStruct[]"`
Ints []int `form:"ints[]"`
Structs Structs `form:"structs"`
Bytes []byte `form:"bytes"`
Uint uint `form:"uint"`
Int int `form:"int"`
Float float32 `form:"float"`
Bool bool `form:"bool"`
}
Struct struct {
uid string `form:"-"`
}
)
const testData string = `skip=dontTouchMe` +
`&bool=true` +
`&string=hello+world` +
`&int=42` +
`&uint=420` +
`&float=4.2` +
// `&interface=a1b2c3` + // TODO(toby3d)
`&struct=abc` +
`&ptrStruct=123` +
`&arrayStruct[]=abc` +
`&arrayStruct[]=123` +
`&arrayPtrStruct[]=321` +
`&arrayPtrStruct[]=bca` +
`&ints[]=240` +
`&ints[]=420` +
`&bytes=sampletext` +
`&notFormTag=dontParseMe`
Structs []Struct
NullStruct struct {
uid string `form:"-"`
}
)
func TestUnmarshal(t *testing.T) {
t.Parallel()
args := http.AcquireArgs()
args.Parse(testData)
t.Run("valid", func(t *testing.T) {
t.Parallel()
var in TestResult
if err := form.Unmarshal(args.QueryString(), &in); err != nil {
t.Fatal(err)
args, err := url.ParseQuery(`skip=dontTouchMe` +
`&bool=true` +
`&string=hello+world` +
`&int=42` +
`&uint=420` +
`&float=4.2` +
// `&interface=a1b2c3` + // TODO(toby3d)
`&struct=abc` +
`&ptrStruct=123` +
`&arrayStruct[]=abc` +
`&arrayStruct[]=123` +
`&arrayPtrStruct[]=321` +
`&arrayPtrStruct[]=bca` +
`&ints[]=240` +
`&ints[]=420` +
`&bytes=sampletext` +
`&notFormTag=dontParseMe` +
`&structs=123+abc` +
`&ptrStructs=bca+321`)
if err != nil {
t.Fatal(err)
}
in := new(TestResult)
if err := form.Unmarshal([]byte(args.Encode()), in); err != nil {
t.Fatal(err)
}
out := TestResult{
Skip: nil,
Bool: true,
Float: 4.2,
Int: 42,
// Interface: []byte("a1b2c3"), // TODO(toby3d)
PtrStruct: &Struct{uid: "123"},
String: "hello world",
Struct: Struct{uid: "abc"},
Uint: 420,
ArrayStruct: []Struct{
{uid: "abc"},
{uid: "123"},
},
ArrayPtrStruct: []*Struct{
{uid: "321"},
{uid: "bca"},
},
Ints: []int{240, 420},
Empty: "",
Bytes: []byte("sampletext"),
NotFormTag: "",
NullStruct: NullStruct{uid: ""},
Structs: Structs{
{uid: "123"},
{uid: "abc"},
},
PtrStructs: &Structs{
{uid: "bca"},
{uid: "321"},
},
}
opts := []cmp.Option{
cmp.AllowUnexported(Struct{}, NullStruct{}),
}
if !cmp.Equal(&out, in, opts...) {
t.Errorf("Unmarshal(%s, &in)\n%+s", args.Encode(), cmp.Diff(&out, in, opts...))
}
})
t.Run("invalid", func(t *testing.T) {
t.Parallel()
args, err := url.ParseQuery("arrayStruct[]=wtf")
if err != nil {
t.Fatal(err)
}
in := new(TestResult)
if err := form.Unmarshal([]byte(args.Encode()), in); err == nil {
t.Errorf("Unmarshal(%s, &in) = %#+v, want error", args.Encode(), err)
}
})
}
func (s *Structs) UnmarshalForm(v []byte) error {
for _, f := range strings.Fields(string(v)) {
*s = append(*s, Struct{uid: f})
}
out := TestResult{
Skip: nil,
Bool: true,
Float: 4.2,
Int: 42,
// Interface: []byte("a1b2c3"), // TODO(toby3d)
PtrStruct: &Struct{uid: "123"},
String: "hello world",
Struct: Struct{uid: "abc"},
Uint: 420,
ArrayStruct: []Struct{
{uid: "abc"},
{uid: "123"},
},
ArrayPtrStruct: []*Struct{
{uid: "321"},
{uid: "bca"},
},
Ints: []int{240, 420},
Empty: "",
Bytes: []byte("sampletext"),
NotFormTag: "",
return nil
}
func (s Structs) GoString() string {
out := make([]string, len(s))
for i := range s {
out[i] = s[i].uid
}
opts := []cmp.Option{
cmp.AllowUnexported(Struct{}),
}
if !cmp.Equal(out, in, opts...) {
t.Errorf("Unmarshal(%s, &in)\n%+s", args.QueryString(), cmp.Diff(out, in, opts...))
}
return "Structs(" + strings.Join(out, ", ") + ")"
}
func (s *Struct) UnmarshalForm(v []byte) error {
@ -101,11 +157,21 @@ func (s *Struct) UnmarshalForm(v []byte) error {
switch src {
case "123", "abc", "321", "bca":
s.uid = string(v)
default:
return errors.New("Struct: dough!")
return nil
}
return nil
return errors.New("Struct: dough!")
}
func (ns *NullStruct) UnmarshalForm(v []byte) error {
if src := string(v); src != "" {
ns.uid = src
return nil
}
return errors.New("NullStruct: dough!")
}
func (s Struct) GoString() string { return "Struct(" + s.uid + ")" }