Add ability to specify the format of the query parameters via json tag.

This commit is contained in:
Mark Beamer Jr 2019-12-15 01:37:44 -05:00
parent 9b6fea461c
commit 8f8b2e605b
No known key found for this signature in database
GPG key ID: 1C314FB89AD76973

View file

@ -175,21 +175,25 @@ func FormValues(r *http.Request, params interface{}, validationRules []*v.FieldR
structValue := ref.Elem() structValue := ref.Elem()
fields := map[string]bool{} fields := map[string]bool{}
for i := 0; i < structType.NumField(); i++ { for i := 0; i < structType.NumField(); i++ {
name := structType.Field(i).Name fieldName := structType.Field(i).Name
underscoredName := util.Underscore(name) formattedName := util.Underscore(fieldName)
value := strings.TrimSpace(r.FormValue(underscoredName)) jsonName, ok := structType.Field(i).Tag.Lookup("json")
if ok {
formattedName = jsonName
}
value := strings.TrimSpace(r.FormValue(formattedName))
// if param is not set at all, continue // if param is not set at all, continue
// comes after call to r.FormValue so form values get parsed internally (if they arent already) // comes after call to r.FormValue so form values get parsed internally (if they arent already)
if len(r.Form[underscoredName]) == 0 { if len(r.Form[formattedName]) == 0 {
continue continue
} }
fields[underscoredName] = true fields[formattedName] = true
isPtr := false isPtr := false
var finalValue reflect.Value var finalValue reflect.Value
structField := structValue.FieldByName(name) structField := structValue.FieldByName(fieldName)
structFieldKind := structField.Kind() structFieldKind := structField.Kind()
if structFieldKind == reflect.Ptr { if structFieldKind == reflect.Ptr {
isPtr = true isPtr = true
@ -205,7 +209,7 @@ func FormValues(r *http.Request, params interface{}, validationRules []*v.FieldR
} }
castVal, err := cast.ToInt64E(value) castVal, err := cast.ToInt64E(value)
if err != nil { if err != nil {
return errors.Err("%s: must be an integer", underscoredName) return errors.Err("%s: must be an integer", formattedName)
} }
switch structFieldKind { switch structFieldKind {
case reflect.Int: case reflect.Int:
@ -225,7 +229,7 @@ func FormValues(r *http.Request, params interface{}, validationRules []*v.FieldR
} }
castVal, err := cast.ToUint64E(value) castVal, err := cast.ToUint64E(value)
if err != nil { if err != nil {
return errors.Err("%s: must be an unsigned integer", underscoredName) return errors.Err("%s: must be an unsigned integer", formattedName)
} }
switch structFieldKind { switch structFieldKind {
case reflect.Uint: case reflect.Uint:
@ -245,7 +249,7 @@ func FormValues(r *http.Request, params interface{}, validationRules []*v.FieldR
} }
if !validator.IsBoolString(value) { if !validator.IsBoolString(value) {
return errors.Err("%s: must be one of the following values: %s", return errors.Err("%s: must be one of the following values: %s",
underscoredName, strings.Join(validator.GetBoolStringValues(), ", ")) formattedName, strings.Join(validator.GetBoolStringValues(), ", "))
} }
finalValue = reflect.ValueOf(validator.IsTruthy(value)) finalValue = reflect.ValueOf(validator.IsTruthy(value))
@ -255,7 +259,7 @@ func FormValues(r *http.Request, params interface{}, validationRules []*v.FieldR
} }
castVal, err := cast.ToFloat64E(value) castVal, err := cast.ToFloat64E(value)
if err != nil { if err != nil {
return errors.Err("%s: must be a floating point number", underscoredName) return errors.Err("%s: must be a floating point number", formattedName)
} }
switch structFieldKind { switch structFieldKind {
case reflect.Float32: case reflect.Float32:
@ -264,7 +268,7 @@ func FormValues(r *http.Request, params interface{}, validationRules []*v.FieldR
finalValue = reflect.ValueOf(float64(castVal)) finalValue = reflect.ValueOf(float64(castVal))
} }
default: default:
return errors.Err("field %s is an unsupported type", name) return errors.Err("field %s is an unsupported type", fieldName)
} }
if isPtr { if isPtr {