-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Pool the buffer and encoder used for generic JSON reflection #602
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a5e4208
Make Buffer.Free safe to call on nil
f461918
Add Buffer.TrimNewline
b4a606a
json_encoder: re-use a *json.Encoder and *bytes.Buffer for reflection
7ba34d3
Shift the buf free nil check back to json encoder to better appease c…
93e77b2
Add a jsonEncoder.EncodeEntry test for more coverage
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright (c) 2018 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package zapcore_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
// TestJSONEncodeEntry is an more "integrated" test that makes it easier to get | ||
// coverage on the json encoder (e.g. putJSONEncoder, let alone EncodeEntry | ||
// itself) than the tests in json_encoder_impl_test.go; it needs to be in the | ||
// zapcore_test package, so that it can import the toplevel zap package for | ||
// field constructor convenience. | ||
func TestJSONEncodeEntry(t *testing.T) { | ||
type bar struct { | ||
Key string `json:"key"` | ||
Val float64 `json:"val"` | ||
} | ||
|
||
type foo struct { | ||
A string `json:"aee"` | ||
B int `json:"bee"` | ||
C float64 `json:"cee"` | ||
D []bar `json:"dee"` | ||
} | ||
|
||
tests := []struct { | ||
desc string | ||
expected string | ||
ent zapcore.Entry | ||
fields []zapcore.Field | ||
}{ | ||
{ | ||
desc: "info entry with some fields", | ||
expected: `{ | ||
"L": "info", | ||
"T": "2018-06-19T16:33:42.000Z", | ||
"N": "bob", | ||
"M": "lob law", | ||
"so": "passes", | ||
"answer": 42, | ||
"common_pie": 3.14, | ||
"such": { | ||
"aee": "lol", | ||
"bee": 123, | ||
"cee": 0.9999, | ||
"dee": [ | ||
{"key": "pi", "val": 3.141592653589793}, | ||
{"key": "tau", "val": 6.283185307179586} | ||
] | ||
} | ||
}`, | ||
ent: zapcore.Entry{ | ||
Level: zapcore.InfoLevel, | ||
Time: time.Date(2018, 6, 19, 16, 33, 42, 99, time.UTC), | ||
LoggerName: "bob", | ||
Message: "lob law", | ||
}, | ||
fields: []zapcore.Field{ | ||
zap.String("so", "passes"), | ||
zap.Int("answer", 42), | ||
zap.Float64("common_pie", 3.14), | ||
zap.Reflect("such", foo{ | ||
A: "lol", | ||
B: 123, | ||
C: 0.9999, | ||
D: []bar{ | ||
{"pi", 3.141592653589793}, | ||
{"tau", 6.283185307179586}, | ||
}, | ||
}), | ||
}, | ||
}, | ||
} | ||
|
||
enc := zapcore.NewJSONEncoder(zapcore.EncoderConfig{ | ||
MessageKey: "M", | ||
LevelKey: "L", | ||
TimeKey: "T", | ||
NameKey: "N", | ||
CallerKey: "C", | ||
StacktraceKey: "S", | ||
EncodeLevel: zapcore.LowercaseLevelEncoder, | ||
EncodeTime: zapcore.ISO8601TimeEncoder, | ||
EncodeDuration: zapcore.SecondsDurationEncoder, | ||
EncodeCaller: zapcore.ShortCallerEncoder, | ||
}) | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.desc, func(t *testing.T) { | ||
buf, err := enc.EncodeEntry(tt.ent, tt.fields) | ||
if assert.NoError(t, err, "Unexpected JSON encoding error.") { | ||
assert.JSONEq(t, tt.expected, buf.String(), "Incorrect encoded JSON entry.") | ||
} | ||
buf.Free() | ||
}) | ||
} | ||
} | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Similar tests already exist in the top-level zap package; would we be better off changing the Makefile to pass
coverpkg=all
instead of adding a new test suite?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.
For future reviewers: this turned into a yak shave, will fix it later.