Example #1
0
func testCollectionFormat(t *testing.T, param *spec.Parameter, valid bool) {
	binder := &untypedParamBinder{
		parameter: param,
	}
	_, _, err := binder.readValue(url.Values(nil), reflect.ValueOf(nil))
	if valid {
		assert.NoError(t, err)
	} else {
		assert.Error(t, err)
		assert.Equal(t, errors.InvalidCollectionFormat(param.Name, param.In, param.CollectionFormat), err)
	}
}
Example #2
0
func (p *untypedParamBinder) readValue(values interface{}, target reflect.Value) ([]string, bool, error) {
	name, in, cf, tpe := p.parameter.Name, p.parameter.In, p.parameter.CollectionFormat, p.parameter.Type
	if tpe == "array" {
		if cf == "multi" {
			if !p.allowsMulti() {
				return nil, false, errors.InvalidCollectionFormat(name, in, cf)
			}
			return values.(url.Values)[name], false, nil
		}

		v := httpkit.ReadSingleValue(values.(httpkit.Gettable), name)
		return p.readFormattedSliceFieldValue(v, target)
	}

	v := httpkit.ReadSingleValue(values.(httpkit.Gettable), name)
	if v == "" {
		return nil, false, nil
	}
	return []string{v}, false, nil
}
Example #3
0
func (p *untypedParamBinder) readValue(values httpkit.Gettable, target reflect.Value) ([]string, bool, bool, error) {
	name, in, cf, tpe := p.parameter.Name, p.parameter.In, p.parameter.CollectionFormat, p.parameter.Type
	if tpe == "array" {
		if cf == "multi" {
			if !p.allowsMulti() {
				return nil, false, false, errors.InvalidCollectionFormat(name, in, cf)
			}
			vv, hasKey, _ := values.GetOK(name)
			return vv, false, hasKey, nil
		}

		v, hk, hv := values.GetOK(name)
		if !hv {
			return nil, false, hk, nil
		}
		d, c, e := p.readFormattedSliceFieldValue(v[len(v)-1], target)
		return d, c, hk, e
	}

	vv, hk, _ := values.GetOK(name)
	return vv, false, hk, nil
}