-
Notifications
You must be signed in to change notification settings - Fork 7.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
now you can replace binding.Form parser #2666
base: master
Are you sure you want to change the base?
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2666 +/- ##
=======================================
Coverage 98.64% 98.64%
=======================================
Files 41 41
Lines 1989 1989
=======================================
Hits 1962 1962
Misses 15 15
Partials 12 12
Continue to review full report at Codecov.
|
MsgPack BindingBody = msgpackBinding{} | ||
YAML BindingBody = yamlBinding{} | ||
Uri BindingUri = uriBinding{} | ||
Header Binding = headerBinding{} | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why add these code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import (
"github.com/gin-gonic/gin/binding"
"q9admin/framework/gbinding"
"q9admin/framework/validator"
)
func init() {
//配置gin的验证器
binding.Validator = new(validator.DefaultValidator)
//配置自定义binding解析器,用于自定义类型的binding
// binding.Form 's type must be binding.Binding, and you can replace default Form parser
binding.Form = gbinding.New()
}
pakage gbinding
type formBinding struct{}
const defaultMemory = 32 << 20
func (formBinding) Name() string {
return "form"
}
func New() formBinding {
return formBinding{}
}
.......
func setWithProperType(val string, value reflect.Value, field reflect.StructField) error {
....
case reflect.Struct:
switch value.Interface().(type) {
case time.Time:
return setTimeField(val, field, value)
case gtime.Time: //加入自定义类型的解析, add new type
return setGTimeField(val, field, value)
}
return json.Unmarshal([]byte(val), value.Addr().Interface())
...
}
you can replace default Form parser.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or you can add your own type parser..like []int64
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like a harmless change that improves a real problem: how to control marshalling for custom non-struct types
Is it possible to merge the code here, I want to replace the binding, how to do it |
master