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 gin code logic #2790

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

daheige
Copy link
Contributor

@daheige daheige commented Jul 14, 2021

1.rename validateHeader to getIPFromHeader
2.fix:shadow: declaration of "err" shadows declaration at line 304
3.lazyInit initialize the validate Settings, no need to use sync.once
4.ginS engine adjust
5.code style adjust

@daheige
Copy link
Contributor Author

daheige commented Jul 14, 2021

@@appleboy

@codecov
Copy link

codecov bot commented Jul 14, 2021

Codecov Report

Merging #2790 (068942b) into master (caf2802) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2790   +/-   ##
=======================================
  Coverage   98.70%   98.70%           
=======================================
  Files          41       41           
  Lines        2085     2088    +3     
=======================================
+ Hits         2058     2061    +3     
  Misses         15       15           
  Partials       12       12           
Impacted Files Coverage Δ
binding/default_validator.go 100.00% <100.00%> (ø)
binding/form_mapping.go 100.00% <100.00%> (ø)
binding/multipart_form_mapping.go 100.00% <100.00%> (ø)
context.go 97.71% <100.00%> (+0.02%) ⬆️

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 caf2802...068942b. Read the comment docs.

func init() {
if engine == nil {
engine = gin.Default()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

How is this different from just writing:

var engine = gin.Default()

? I don't think you can change the value of the engine variable before init is executed, so the if condition will always be true.

v.once.Do(func() {
// lazyInit initialize the validate Settings, no need to use sync.once
func (v *defaultValidator) lazyInit() {
if v.validate == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if this is an improvement. I believe sync.Once was used to avoid race conditions. This version seems kind of safe, too, but it might call validator.New() multiple times unnecessarily. I don't see the point of eliminating sync.Once.

@daheige
Copy link
Contributor Author

daheige commented Jul 18, 2021 via email

@daheige
Copy link
Contributor Author

daheige commented Jul 18, 2021 via email

@daheige
Copy link
Contributor Author

daheige commented Jul 19, 2021 via email

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.

2 participants