Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

yanllllk
Copy link

  • With pull requests:
    • Open your pull request against master
    • Your pull request should have no more than two commits, if not you should squash them.
    • It should pass all tests in the available continuous integration systems such as TravisCI.
    • You should add/modify tests to cover your proposed code changes.
    • If your pull request contains a new feature, please document it on the README.

@codecov
Copy link

codecov bot commented Mar 29, 2021

Codecov Report

Merging #2666 (0c40e2d) into master (a331dc6) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2666   +/-   ##
=======================================
  Coverage   98.64%   98.64%           
=======================================
  Files          41       41           
  Lines        1989     1989           
=======================================
  Hits         1962     1962           
  Misses         15       15           
  Partials       12       12           
Impacted Files Coverage Δ
binding/binding.go 100.00% <ø> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a331dc6...0c40e2d. Read the comment docs.

MsgPack BindingBody = msgpackBinding{}
YAML BindingBody = yamlBinding{}
Uri BindingUri = uriBinding{}
Header Binding = headerBinding{}
)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why add these code?

Copy link
Author

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.

Copy link
Author

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

Copy link

@ssoroka ssoroka left a 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

@TNED
Copy link

TNED commented Aug 10, 2023

Is it possible to merge the code here, I want to replace the binding, how to do it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants