func TestSliceConversion(t *testing.T) { actual := new(SomeOperationParams) val := reflect.ValueOf(actual).Elem() // prefsField := val.FieldByName("Prefs") // cData := "yada,2,3" // _, _, err := readFormattedSliceFieldValue("Prefs", prefsField, cData, "csv", nil) // assert.Error(t, err) sliced := []string{"some", "string", "values"} seps := map[string]string{"ssv": " ", "tsv": "\t", "pipes": "|", "csv": ",", "": ","} tagsField := val.FieldByName("Tags") for k, sep := range seps { binder := &untypedParamBinder{ Name: "Tags", parameter: spec.QueryParam("tags").CollectionOf(stringItems, k), } actual.Tags = nil cData := strings.Join(sliced, sep) tags, _, err := binder.readFormattedSliceFieldValue(cData, tagsField) assert.NoError(t, err) assert.Equal(t, sliced, tags) cData = strings.Join(sliced, " "+sep+" ") tags, _, err = binder.readFormattedSliceFieldValue(cData, tagsField) assert.NoError(t, err) assert.Equal(t, sliced, tags) tags, _, err = binder.readFormattedSliceFieldValue("", tagsField) assert.NoError(t, err) assert.Empty(t, tags) } assert.Nil(t, swag.SplitByFormat("yada", "multi")) assert.Nil(t, swag.SplitByFormat("", "")) categoriesField := val.FieldByName("Categories") binder := &untypedParamBinder{ Name: "Categories", parameter: spec.QueryParam("categories").CollectionOf(stringItems, "csv"), } cData := strings.Join(sliced, ",") categories, custom, err := binder.readFormattedSliceFieldValue(cData, categoriesField) assert.NoError(t, err) assert.EqualValues(t, sliced, actual.Categories) assert.True(t, custom) assert.Empty(t, categories) categories, custom, err = binder.readFormattedSliceFieldValue("", categoriesField) assert.Error(t, err) assert.True(t, custom) assert.Empty(t, categories) }
func (o *FindLearningResourcesParams) bindTypes(rawData []string, hasKey bool, formats strfmt.Registry) error { var qvTypes string if len(rawData) > 0 { qvTypes = rawData[len(rawData)-1] } raw := swag.SplitByFormat(qvTypes, "csv") size := len(raw) if size == 0 { return nil } ic := raw isz := size var ir []string iValidateElement := func(i int, typesI string) *errors.Validation { return nil } for i := 0; i < isz; i++ { if err := iValidateElement(i, ic[i]); err != nil { return err } ir = append(ir, ic[i]) } o.Types = ir return nil }
func (p *untypedParamBinder) readFormattedSliceFieldValue(data string, target reflect.Value) ([]string, bool, error) { ok, err := p.tryUnmarshaler(target, p.parameter.Default, data) if err != nil { return nil, true, err } if ok { return nil, true, nil } return swag.SplitByFormat(data, p.parameter.CollectionFormat), false, nil }
// BindRequest both binds and validates a request, it assumes that complex things implement a Validatable(strfmt.Registry) error interface // for simple values it will use straight method calls func (o *FindPetsByStatusParams) BindRequest(r *http.Request, route *middleware.MatchedRoute) error { var res []error qs := r.URL.Query() if err := o.bindStatus(swag.SplitByFormat(qs.Get("status"), "multi"), route.Formats); err != nil { res = append(res, err) } if len(res) > 0 { return errors.CompositeValidationError(res...) } return nil }
func (o *FindPetsByStatusParams) bindStatus(rawData []string, hasKey bool, formats strfmt.Registry) error { if !hasKey { return errors.Required("status", "query") } var qvStatus string if len(rawData) > 0 { qvStatus = rawData[len(rawData)-1] } raw := swag.SplitByFormat(qvStatus, "csv") size := len(raw) if size == 0 { return errors.Required("status", "query") } if size == 0 { return nil } ic := raw isz := size var ir []string iValidateElement := func(i int, statusI string) *errors.Validation { if err := validate.Enum(fmt.Sprintf("%s.%v", "status", i), "query", o.Status[i], []interface{}{"available", "pending", "sold"}); err != nil { return err } return nil } for i := 0; i < isz; i++ { if err := iValidateElement(i, ic[i]); err != nil { return err } ir = append(ir, ic[i]) } o.Status = ir return nil }
func (o *ListTasksParams) bindStatus(rawData []string, hasKey bool, formats strfmt.Registry) error { var qvStatus string if len(rawData) > 0 { qvStatus = rawData[len(rawData)-1] } raw := swag.SplitByFormat(qvStatus, "pipes") size := len(raw) if size == 0 { return nil } ic := raw isz := size var ir []string iValidateElement := func(i int, statusI string) *errors.Validation { if err := validate.Enum(fmt.Sprintf("%s.%v", "status", i), "query", o.Status[i], []interface{}{"open", "closed", "ignored", "rejected"}); err != nil { return err } return nil } for i := 0; i < isz; i++ { if err := iValidateElement(i, ic[i]); err != nil { return err } ir = append(ir, ic[i]) } o.Status = ir if err := o.validateStatus(formats); err != nil { return err } return nil }
func (o *FindPetsByTagsParams) bindTags(rawData []string, hasKey bool, formats strfmt.Registry) error { if !hasKey { return errors.Required("tags", "query") } var qvTags string if len(rawData) > 0 { qvTags = rawData[len(rawData)-1] } raw := swag.SplitByFormat(qvTags, "csv") size := len(raw) if size == 0 { return errors.Required("tags", "query") } if size == 0 { return nil } ic := raw isz := size var ir []string iValidateElement := func(i int, tagsI string) *errors.Validation { return nil } for i := 0; i < isz; i++ { if err := iValidateElement(i, ic[i]); err != nil { return err } ir = append(ir, ic[i]) } o.Tags = ir return nil }
func (o *ListTasksParams) bindTags(rawData []string, hasKey bool, formats strfmt.Registry) error { var qvTags string if len(rawData) > 0 { qvTags = rawData[len(rawData)-1] } raw := swag.SplitByFormat(qvTags, "") size := len(raw) if size == 0 { return nil } ic := raw isz := size var ir []string iValidateElement := func(i int, tagsI string) *errors.Validation { return nil } for i := 0; i < isz; i++ { if err := iValidateElement(i, ic[i]); err != nil { return err } ir = append(ir, ic[i]) } o.Tags = ir if err := o.validateTags(formats); err != nil { return err } return nil }
// ReadCollectionValue reads a collection value from a string data source func ReadCollectionValue(values Gettable, name, collectionFormat string) []string { v := ReadSingleValue(values, name) return swag.SplitByFormat(v, collectionFormat) }