gin-gonic / gin

Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
https://gin-gonic.com/
MIT License
78.94k stars 8.02k forks source link

support custom form-mapping and json-encode/decode #3547

Open lj19950508 opened 1 year ago

lj19950508 commented 1 year ago

i need a feture to support custom form-mapping and json-mapping like this

type A struct { field form:"field" json:"field" }

i need custom this fuction that i can bind camel key name without tag form:"field"

r.UseFormMapping(defaultFormBindMapping) r.UseJsonEncodeMapping(defaultEncodingMaping) r.UseJsonDecodeMapping(defaultDecodeMapping)

The final result is that you don't need to use tags

type A struct { field }

this source file can be custom simply gin/binding/form_mapping.go

func mapFormByTag(ptr interface{}, form map[string][]string, tag string) error { // Check if ptr is a map ptrVal := reflect.ValueOf(ptr) var pointed interface{} if ptrVal.Kind() == reflect.Ptr { ptrVal = ptrVal.Elem() pointed = ptrVal.Interface() } if ptrVal.Kind() == reflect.Map && ptrVal.Type().Key().Kind() == reflect.String { if pointed != nil { ptr = pointed } return setFormMap(ptr, form) }

return mappingByPtr(ptr, formSource(form), tag)

}

aohanhongzhi commented 1 year ago

I agree