Skip to content

Commit e328714

Browse files
bufdevprashantv
authored andcommitted
Add RegisterEncoder functionality (uber-go#348)
1 parent 14d7845 commit e328714

File tree

3 files changed

+162
-8
lines changed

3 files changed

+162
-8
lines changed

config.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
package zap
2222

2323
import (
24-
"fmt"
2524
"sort"
2625
"time"
2726

@@ -229,11 +228,5 @@ func (cfg Config) openSinks() (zapcore.WriteSyncer, zapcore.WriteSyncer, error)
229228
}
230229

231230
func (cfg Config) buildEncoder() (zapcore.Encoder, error) {
232-
switch cfg.Encoding {
233-
case "json":
234-
return zapcore.NewJSONEncoder(cfg.EncoderConfig), nil
235-
case "console":
236-
return zapcore.NewConsoleEncoder(cfg.EncoderConfig), nil
237-
}
238-
return nil, fmt.Errorf("unknown encoding %q", cfg.Encoding)
231+
return newEncoder(cfg.Encoding, cfg.EncoderConfig)
239232
}

encoder.go

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (c) 2016 Uber Technologies, Inc.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
package zap
22+
23+
import (
24+
"errors"
25+
"fmt"
26+
"sync"
27+
28+
"go.uber.org/zap/zapcore"
29+
)
30+
31+
var (
32+
errNoEncoderNameSpecified = errors.New("no encoder name specified")
33+
34+
_encoderNameToConstructor = map[string]func(zapcore.EncoderConfig) (zapcore.Encoder, error){
35+
"console": func(encoderConfig zapcore.EncoderConfig) (zapcore.Encoder, error) {
36+
return zapcore.NewConsoleEncoder(encoderConfig), nil
37+
},
38+
"json": func(encoderConfig zapcore.EncoderConfig) (zapcore.Encoder, error) {
39+
return zapcore.NewJSONEncoder(encoderConfig), nil
40+
},
41+
}
42+
_encoderMutex sync.RWMutex
43+
)
44+
45+
// RegisterEncoder registers an encoder constructor for the given name.
46+
//
47+
// If an encoder with the same name already exists, this will return an error.
48+
// By default, the encoders "json" and "console" are registered.
49+
func RegisterEncoder(name string, constructor func(zapcore.EncoderConfig) (zapcore.Encoder, error)) error {
50+
_encoderMutex.Lock()
51+
defer _encoderMutex.Unlock()
52+
if name == "" {
53+
return errNoEncoderNameSpecified
54+
}
55+
if _, ok := _encoderNameToConstructor[name]; ok {
56+
return fmt.Errorf("encoder already registered for name %q", name)
57+
}
58+
_encoderNameToConstructor[name] = constructor
59+
return nil
60+
}
61+
62+
func newEncoder(name string, encoderConfig zapcore.EncoderConfig) (zapcore.Encoder, error) {
63+
_encoderMutex.RLock()
64+
defer _encoderMutex.RUnlock()
65+
if name == "" {
66+
return nil, errNoEncoderNameSpecified
67+
}
68+
constructor, ok := _encoderNameToConstructor[name]
69+
if !ok {
70+
return nil, fmt.Errorf("no encoder registered for name %q", name)
71+
}
72+
return constructor(encoderConfig)
73+
}

encoder_test.go

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright (c) 2016 Uber Technologies, Inc.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
package zap
22+
23+
import (
24+
"testing"
25+
26+
"go.uber.org/zap/zapcore"
27+
28+
"github.com/stretchr/testify/assert"
29+
)
30+
31+
func TestRegisterDefaultEncoders(t *testing.T) {
32+
testEncodersRegistered(t, "console", "json")
33+
}
34+
35+
func TestRegisterEncoder(t *testing.T) {
36+
testEncoders(func() {
37+
assert.NoError(t, RegisterEncoder("foo", newNilEncoder), "expected to be able to register the encoder foo")
38+
testEncodersRegistered(t, "foo")
39+
})
40+
}
41+
42+
func TestDuplicateRegisterEncoder(t *testing.T) {
43+
testEncoders(func() {
44+
RegisterEncoder("foo", newNilEncoder)
45+
assert.Error(t, RegisterEncoder("foo", newNilEncoder), "expected an error when registering an encoder with the same name twice")
46+
})
47+
}
48+
49+
func TestRegisterEncoderNoName(t *testing.T) {
50+
assert.Equal(t, errNoEncoderNameSpecified, RegisterEncoder("", newNilEncoder), "expected an error when registering an encoder with no name")
51+
}
52+
53+
func TestNewEncoder(t *testing.T) {
54+
testEncoders(func() {
55+
RegisterEncoder("foo", newNilEncoder)
56+
encoder, err := newEncoder("foo", zapcore.EncoderConfig{})
57+
assert.NoError(t, err, "could not create an encoder for the registered name foo")
58+
assert.Nil(t, encoder, "the encoder from newNilEncoder is not nil")
59+
})
60+
}
61+
62+
func TestNewEncoderNotRegistered(t *testing.T) {
63+
_, err := newEncoder("foo", zapcore.EncoderConfig{})
64+
assert.Error(t, err, "expected an error when trying to create an encoder of an unregistered name")
65+
}
66+
67+
func TestNewEncoderNoName(t *testing.T) {
68+
_, err := newEncoder("", zapcore.EncoderConfig{})
69+
assert.Equal(t, errNoEncoderNameSpecified, err, "expected an error when creating an encoder with no name")
70+
}
71+
72+
func testEncoders(f func()) {
73+
existing := _encoderNameToConstructor
74+
_encoderNameToConstructor = make(map[string]func(zapcore.EncoderConfig) (zapcore.Encoder, error))
75+
defer func() { _encoderNameToConstructor = existing }()
76+
f()
77+
}
78+
79+
func testEncodersRegistered(t *testing.T, names ...string) {
80+
assert.Len(t, _encoderNameToConstructor, len(names), "the expected number of registered encoders does not match the actual number")
81+
for _, name := range names {
82+
assert.NotNil(t, _encoderNameToConstructor[name], "no encoder is registered for name %s", name)
83+
}
84+
}
85+
86+
func newNilEncoder(_ zapcore.EncoderConfig) (zapcore.Encoder, error) {
87+
return nil, nil
88+
}

0 commit comments

Comments
 (0)