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

refactor(form_mapping.go): mapping multipart request #1829

Merged
merged 5 commits into from Apr 2, 2019

Conversation

vkd
Copy link
Contributor

@vkd vkd commented Mar 26, 2019

Refactor mapping to interface setter - for supporting a multipart request.

Benchmarks of a simple form mapping (with many fields and one field respectively):

⇒  benchcmp old.bch new.bch
benchmark                  old ns/op     new ns/op     delta
BenchmarkMapFormFull-4     2332          2358          +1.11%
BenchmarkMapFormName-4     218           235           +7.80%

benchmark                  old allocs     new allocs     delta
BenchmarkMapFormFull-4     12             12             +0.00%
BenchmarkMapFormName-4     1              1              +0.00%

benchmark                  old bytes     new bytes     delta
BenchmarkMapFormFull-4     200           200           +0.00%
BenchmarkMapFormName-4     8             8             +0.00%

@codecov
Copy link

codecov bot commented Mar 26, 2019

Codecov Report

Merging #1829 into master will decrease coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1829      +/-   ##
==========================================
- Coverage   98.64%   98.64%   -0.01%     
==========================================
  Files          41       41              
  Lines        2148     2144       -4     
==========================================
- Hits         2119     2115       -4     
  Misses         18       18              
  Partials       11       11
Impacted Files Coverage Δ
binding/form.go 100% <100%> (ø) ⬆️
binding/form_mapping.go 100% <100%> (ø) ⬆️

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 ce20f10...0235b7c. Read the comment docs.

binding/form.go Outdated
multipartFileHeaderStructType = reflect.TypeOf(multipart.FileHeader{})
)

func (r *multipartRequest) Set(value reflect.Value, field reflect.StructField, key string, opt setOptions) (isSetted bool, err error) {
Copy link
Member

@thinkerou thinkerou Mar 30, 2019

Choose a reason for hiding this comment

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

check: var _ setter = &multipartRequest
and add node for export method/class/interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you mean when you talk about "node"?
Btw, this method is not exported because the type multipartRequest is not exported, and I see no warnings about this from any linters.

Copy link
Member

Choose a reason for hiding this comment

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

@vkd sorry, word typo, note.
Yes, it's not exported. We should add some usage note for new and important interface/method/class even if un-exported. thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, thanks. I've added a couple of comments.


type formSource map[string][]string

func (form formSource) Set(value reflect.Value, field reflect.StructField, tagValue string, opt setOptions) (isSetted bool, err error) {
Copy link
Member

@thinkerou thinkerou Mar 30, 2019

Choose a reason for hiding this comment

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

check: var _ setter = &formSource
and add note for export method/class/interface.

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

type setter interface {
Copy link
Member

@thinkerou thinkerou Mar 30, 2019

Choose a reason for hiding this comment

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

add some notes?

Copy link
Member

Choose a reason for hiding this comment

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

please not use // setter - style, thanks!

binding/form.go Outdated
@@ -72,7 +72,8 @@ var (
multipartFileHeaderStructType = reflect.TypeOf(multipart.FileHeader{})
)

func (r *multipartRequest) Set(value reflect.Value, field reflect.StructField, key string, opt setOptions) (isSetted bool, err error) {
// TrySet - try to set a value by the multipart request with the binding a form file
Copy link
Member

Choose a reason for hiding this comment

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

please not use TrySet - style, thanks! like: TrySet tries to set ...

}

type formSource map[string][]string

var _ setter = formSource(nil)

func (form formSource) Set(value reflect.Value, field reflect.StructField, tagValue string, opt setOptions) (isSetted bool, err error) {
// TrySet - try to set a value by request's form source (like map[string][]string)
func (form formSource) TrySet(value reflect.Value, field reflect.StructField, tagValue string, opt setOptions) (isSetted bool, err error) {
Copy link
Member

Choose a reason for hiding this comment

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

the same above, thanks!

@thinkerou thinkerou merged commit 2e915f4 into gin-gonic:master Apr 2, 2019
@vkd vkd deleted the refactor_mapping_multipart branch April 2, 2019 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants