forked from uber-go/zap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support for arrays (uber-go#239)
* Add an internal multierror package We'll need a multierror implementation in both zap and zapcore to support arrays. Clean up the existing implementation a bit and promote it to its own internal package. * Add support for logging arrays Add basic support for logging arrays. To limit the scope of this commit, it introduces only arrays of ObjectMarshalers, arrays of arrays, and arrays of bools. It should, however, clarify how I intend to support arrays of other concrete types. Of note, I opted not to have `zap.Bools` return a `Field`. Instead, it returns a `zapcore.ArrayMarshaler`. This lets us build arrays of heterogenous arrays. For most users, this will be papered over by the upcoming `zap.Any(key string, val interface{}) zapcore.Field` function, which will basically be a huge type switch. * Simpler struct literals in tests * Update some comments * Add some simple benchmarks ``` BenchmarkBoolsArrayMarshaler-4 1000000 1028 ns/op 1088 B/op 3 allocs/op BenchmarkBoolsReflect-4 500000 3293 ns/op 2280 B/op 8 allocs/op ``` * Cleanups from CR
- Loading branch information
1 parent
f370abc
commit 228bdd6
Showing
18 changed files
with
532 additions
and
95 deletions.
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,67 @@ | ||
// Copyright (c) 2016 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 zap | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
func BenchmarkBoolsArrayMarshaler(b *testing.B) { | ||
// Keep this benchmark here to capture the overhead of the ArrayMarshaler | ||
// wrapper. | ||
bs := make([]bool, 50) | ||
enc := zapcore.NewJSONEncoder(zapcore.JSONConfig{}) | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
Bools("array", bs).AddTo(enc.Clone()) | ||
} | ||
} | ||
|
||
func BenchmarkBoolsReflect(b *testing.B) { | ||
bs := make([]bool, 50) | ||
enc := zapcore.NewJSONEncoder(zapcore.JSONConfig{}) | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
Reflect("array", bs).AddTo(enc.Clone()) | ||
} | ||
} | ||
|
||
func TestArrayWrappers(t *testing.T) { | ||
tests := []struct { | ||
desc string | ||
field zapcore.Field | ||
expected []interface{} | ||
}{ | ||
{"empty bools", Bools("", []bool{}), []interface{}(nil)}, | ||
{"bools", Bools("", []bool{true, false}), []interface{}{true, false}}, | ||
} | ||
|
||
for _, tt := range tests { | ||
enc := make(zapcore.MapObjectEncoder) | ||
tt.field.Key = "k" | ||
tt.field.AddTo(enc) | ||
assert.Equal(t, tt.expected, enc["k"], "%s: unexpected map contents.", tt.desc) | ||
assert.Equal(t, 1, len(enc), "%s: found extra keys in map: %v", tt.desc, enc) | ||
} | ||
} |
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,71 @@ | ||
// Copyright (c) 2016 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 multierror provides a simple way to treat a collection of errors as | ||
// a single error. | ||
package multierror | ||
|
||
import "go.uber.org/zap/internal/buffers" | ||
|
||
// implement the standard lib's error interface on a private type so that we | ||
// can't forget to call Error.AsError(). | ||
type errSlice []error | ||
|
||
func (es errSlice) Error() string { | ||
b := buffers.Get() | ||
for i, err := range es { | ||
if i > 0 { | ||
b = append(b, ';', ' ') | ||
} | ||
b = append(b, err.Error()...) | ||
} | ||
ret := string(b) | ||
buffers.Put(b) | ||
return ret | ||
} | ||
|
||
// Error wraps a []error to implement the error interface. | ||
type Error struct { | ||
errs errSlice | ||
} | ||
|
||
// AsError converts the collection to a single error value. | ||
// | ||
// Note that failing to use AsError will almost certainly lead to bugs with | ||
// non-nil interfaces containing nil concrete values. | ||
func (e Error) AsError() error { | ||
switch len(e.errs) { | ||
case 0: | ||
return nil | ||
case 1: | ||
return e.errs[0] | ||
default: | ||
return e.errs | ||
} | ||
} | ||
|
||
// Append adds an error to the collection. Adding a nil error is a no-op. | ||
func (e Error) Append(err error) Error { | ||
if err == nil { | ||
return e | ||
} | ||
e.errs = append(e.errs, err) | ||
return e | ||
} |
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,74 @@ | ||
// Copyright (c) 2016 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 multierror | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestErrorSliceString(t *testing.T) { | ||
tests := []struct { | ||
errs errSlice | ||
expected string | ||
}{ | ||
{nil, ""}, | ||
{errSlice{}, ""}, | ||
{errSlice{errors.New("foo")}, "foo"}, | ||
{errSlice{errors.New("foo"), errors.New("bar")}, "foo; bar"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
assert.Equal(t, tt.expected, tt.errs.Error(), "Unexpected output from Error method.") | ||
} | ||
} | ||
|
||
func TestMultiErrorIsntAnError(t *testing.T) { | ||
// Ensure that Error doesn't satisfy the standard lib's error interface, so | ||
// that we're forced to always call AsError. | ||
var errs interface{} = Error{} | ||
_, ok := errs.(error) | ||
assert.False(t, ok, "Error unexpectedly satisfies standard lib's error interface.") | ||
} | ||
|
||
func TestMultiErrorAsError(t *testing.T) { | ||
assert.Nil(t, (Error{}).AsError(), "Expected calling AsError with no accumulated errors to return nil.") | ||
|
||
e := errors.New("foo") | ||
assert.Equal( | ||
t, | ||
e, | ||
(Error{errSlice{e}}).AsError(), | ||
"Expected AsError with single error to return the original error.", | ||
) | ||
|
||
m := Error{errSlice{errors.New("foo"), errors.New("bar")}} | ||
assert.Equal(t, m.errs, m.AsError(), "Unexpected AsError output with multiple errors.") | ||
} | ||
|
||
func TestErrorAppend(t *testing.T) { | ||
foo := errors.New("foo") | ||
bar := errors.New("bar") | ||
multi := (Error{}).Append(nil).Append(foo).Append(nil).Append(bar) | ||
assert.Equal(t, errSlice{foo, bar}, multi.errs, "Collected errors don't match expectations.") | ||
} |
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
Oops, something went wrong.