Skip to content
Myr

feat(form): add custom string slice for form tag unmarshal (#3970) (#3971)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by bruceNu1l · committed by GitHub · parent e0d46de · Browse files · 2 files+98 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Bruce Lee <admin@ifocusad.com>

Diff stats

2 files+98 added−0 removed

  • other:+98 added−0 removed

2 files+98 added−0 removed

modified:binding/form_mapping_test.go+87 added
3 unmodified lines
45package binding67import (8 "encoding/hex"9 "fmt"10 "mime/multipart"11 "reflect"12 "strconv"
409 unmodified lines
422 assert.EqualValues(t, "file", s.FileData.Protocol)423 assert.EqualValues(t, "/foo", s.FileData.Path)424 assert.EqualValues(t, "happiness", s.FileData.Name)425}426427type customPath []string428429func (p *customPath) UnmarshalParam(param string) error {430 elems := strings.Split(param, "/")431 n := len(elems)432 if n < 2 {433 return fmt.Errorf("invalid format")434 }435436 *p = elems437 return nil438}439440func TestMappingCustomSliceUri(t *testing.T) {441 var s struct {442 FileData customPath `uri:"path"`443 }444 err := mappingByPtr(&s, formSource{"path": {`bar/foo`}}, "uri")445 assert.NoError(t, err)446447 assert.EqualValues(t, "bar", s.FileData[0])448 assert.EqualValues(t, "foo", s.FileData[1])449}450451func TestMappingCustomSliceForm(t *testing.T) {452 var s struct {453 FileData customPath `form:"path"`454 }455 err := mappingByPtr(&s, formSource{"path": {`bar/foo`}}, "form")456 assert.NoError(t, err)457458 assert.EqualValues(t, "bar", s.FileData[0])459 assert.EqualValues(t, "foo", s.FileData[1])460}461462type objectID [12]byte463464func (o *objectID) UnmarshalParam(param string) error {465 oid, err := convertTo(param)466 if err != nil {467 return err468 }469470 *o = oid471 return nil472}473474func convertTo(s string) (objectID, error) {475 var nilObjectID objectID476 if len(s) != 24 {477 return nilObjectID, fmt.Errorf("invalid format")478 }479480 var oid [12]byte481 _, err := hex.Decode(oid[:], []byte(s))482 if err != nil {483 return nilObjectID, err484 }485486 return oid, nil487}488489func TestMappingCustomArrayUri(t *testing.T) {490 var s struct {491 FileData objectID `uri:"id"`492 }493 val := `664a062ac74a8ad104e0e80f`494 err := mappingByPtr(&s, formSource{"id": {val}}, "uri")495 assert.NoError(t, err)496497 expected, _ := convertTo(val)498 assert.EqualValues(t, expected, s.FileData)499}500501func TestMappingCustomArrayForm(t *testing.T) {502 var s struct {503 FileData objectID `form:"id"`504 }505 val := `664a062ac74a8ad104e0e80f`506 err := mappingByPtr(&s, formSource{"id": {val}}, "form")507 assert.NoError(t, err)508509 expected, _ := convertTo(val)510 assert.EqualValues(t, expected, s.FileData)511}
modified:binding/form_mapping.go+11 added
191 unmodified lines
192 case reflect.Slice:193 if !ok {194 vs = []string{opt.defaultValue}195 }196197 if ok, err = trySetCustom(vs[0], value); ok {198 return ok, err199 }200201 return true, setSlice(vs, value, field)202 case reflect.Array:203 if !ok {204 vs = []string{opt.defaultValue}205 }206207 if ok, err = trySetCustom(vs[0], value); ok {208 return ok, err209 }210211 if len(vs) != value.Len() {212 return false, fmt.Errorf("%q is not valid value for %s", vs, value.Type().String())213 }214215 return true, setArray(vs, value, field)216 default:217 var val string218 if !ok {
221 unmodified lines