-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
[extend #1191] Unnecessary alloc for XML, JSON, JSONP #1199
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1199 +/- ##
==========================================
+ Coverage 81.61% 81.86% +0.25%
==========================================
Files 27 27
Lines 1931 1991 +60
==========================================
+ Hits 1576 1630 +54
- Misses 248 249 +1
- Partials 107 112 +5
Continue to review full report at Codecov.
|
@alexaandru Hey, what do you think about it? |
I think in this particular case it would be overkill to have separate functions. The idea with "bool params is a code smell" is that it introduces complexity and makes code harder to understand, but it's not something set in stone, context always matter. Here we have very small functions, the "complexity" added is negligible and definitely smaller than the complexity of adding separate functions. I did look into stdlib briefly and I did have a hard time finding functions that use bools as params, but I did find a couple (in the few minutes that I spent on it): https://golang.org/pkg/compress/gzip/#Reader.Multistream and https://golang.org/pkg/net/http/#Server.SetKeepAlivesEnabled That being said... we could "cheat" a little ;) We could eliminate the bool param entirely and do the indent decision based on |
Hm.. okay.. I’ll fix it UPD: @alexaandru fixed |
context.go
Outdated
} | ||
|
||
func (c *context) JSONPretty(code int, i interface{}, indent string) (err error) { | ||
return c.jsonBlob(code, i, true, indent) | ||
return c.jsonBlob(code, i, indent) |
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.
This looks risky. It works fine in JSON()
where we control the indent ourselves, here however it messes things up :( If they want it pretty but with a blank indent (which I have no idea what it would do? just put each field on a separate line with no indent?) they will get it "not pretty". Should we return an error when the indent is blank for pretty? and also update the documentation to reflect that we expect a non blank indent to be provided for this function?
Yes, just checked, that's what it currently does, with a blank indent, it prints pretty with no indent:
{
"bar": 1,
"foo": 42
}
We can either revert back to using bool
OR we can convert indent
in jsonBlob()
to be a pointer to string. That way, we don't check if it is blank, we check if it is nil. If nil == ugly json, if present (even if blank) == pretty json with that ident. That would allow us to keep the existing behavior where we accept even a blank indent.
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.
yeah, you'll right
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.
fixed with indent
pointer
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.
Looks good to me 👍
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.
Glad to hear.. waiting for @vishr review
add benchmarks
Looks good to me. Thanks for the benchmarks! |
✌️Thx |
…ary_allocs # Conflicts: # context_test.go
@alexaandru / @vishr hi there! there is any chance to be merged? |
|
@vishr hi there, it is not required and possible to be closed? there is no reaction for more than two months |
@vishr can I reopen PR? |
It was a bot thing, please do it. It might take a while to get to it. |
I just reopen, as I can see, all tests passed and has no conflicts with master.. |
@vishr ping? |
@@ -206,6 +206,8 @@ const ( | |||
indexPage = "index.html" | |||
) | |||
|
|||
var defaultIndent = " " |
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.
Can this be a const?
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.
My mistake
Don’t worry I will take care of it.
… On Jan 14, 2019, at 10:13 AM, Evgeniy Kulikov ***@***.***> wrote:
@im-kulikov commented on this pull request.
In context.go <#1199 (comment)>:
> @@ -206,6 +206,8 @@ const (
indexPage = "index.html"
)
+var defaultIndent = " "
My mistake
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub <#1199 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AATKtDrVuZJZJq3lWWMfzkDgyQgoUB4sks5vDMjlgaJpZM4XB46c>.
|
@vishr thx |
Nice 👍 |
Signed-off-by: Vishal Rana <vr@labstack.com>
Implement methods for
fixes for tests
cc @vishr @ribice
Benchmark:
Result