Skip to content

Commit

Permalink
Clean up initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshay Shah committed Mar 14, 2017
1 parent 166e149 commit 03e7655
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 29 deletions.
4 changes: 2 additions & 2 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func Bools(key string, bs []bool) zapcore.Field {
return Array(key, bools(bs))
}

// ByteStrings constructs a field that carries a slice of []byte
// that assumed to be UTF-8 encoded.
// ByteStrings constructs a field that carries a slice of []byte, each of which
// must be UTF-8 encoded text.
func ByteStrings(key string, bss [][]byte) zapcore.Field {
return Array(key, byteStringsArray(bss))
}
Expand Down
10 changes: 5 additions & 5 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func Skip() zapcore.Field {
// Binary constructs a field that carries an opaque binary blob.
//
// Binary data is serialized in an encoding-appropriate format. For example,
// zap's JSON encoder base64-encodes binary blobs.
// zap's JSON encoder base64-encodes binary blobs. To log UTF-8 encoded text,
// use ByteString.
func Binary(key string, val []byte) zapcore.Field {
return zapcore.Field{Key: key, Type: zapcore.BinaryType, Interface: val}
}
Expand All @@ -50,10 +51,9 @@ func Bool(key string, val bool) zapcore.Field {
return zapcore.Field{Key: key, Type: zapcore.BoolType, Integer: ival}
}

// ByteString constructs a field that carries a []byte that assumed to be UTF-8 encoded.
//
// Saves on []byte to string cast copy and allocation, but costs smaller allocation to convert
// the []byte to interface{}.
// ByteString constructs a field that carries UTF-8 encoded text as a []byte.
// To log opaque binary blobs (which aren't necessarily valid UTF-8), use
// Binary.
func ByteString(key string, val []byte) zapcore.Field {
return zapcore.Field{Key: key, Type: zapcore.ByteStringType, Interface: val}
}
Expand Down
11 changes: 3 additions & 8 deletions zapcore/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,12 @@ type EncoderConfig struct {
// aren't safe for concurrent use (though typical use shouldn't require locks).
type ObjectEncoder interface {
// Logging-specific marshalers.

AddArray(key string, marshaler ArrayMarshaler) error
AddObject(key string, marshaler ObjectMarshaler) error

// Built-in types.

// AddBinary adds raw blob of binary data.
AddBinary(key string, value []byte)
// AddByteString adds bytes as UTF-8 string.
// No-alloc equivalent of AddString(string(value)) for []byte values.
AddByteString(key string, value []byte)
AddBinary(key string, value []byte) // for arbitrary bytes
AddByteString(key string, value []byte) // for UTF-8 encoded bytes
AddBool(key string, value bool)
AddComplex128(key string, value complex128)
AddComplex64(key string, value complex64)
Expand Down Expand Up @@ -283,7 +278,7 @@ type ArrayEncoder interface {
type PrimitiveArrayEncoder interface {
// Built-in types.
AppendBool(bool)
AppendByteString([]byte)
AppendByteString([]byte) // for UTF-8 encoded bytes
AppendComplex128(complex128)
AppendComplex64(complex64)
AppendFloat64(float64)
Expand Down
2 changes: 1 addition & 1 deletion zapcore/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const (
BinaryType
// BoolType indicates that the field carries a bool.
BoolType
// ByteStringType indicates that the field carries a []byte that assumed to be UTF-8 encoded.
// ByteStringType indicates that the field carries a UTF-8 encoded bytes.
ByteStringType
// Complex128Type indicates that the field carries a complex128.
Complex128Type
Expand Down
19 changes: 9 additions & 10 deletions zapcore/json_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,14 +423,13 @@ func (enc *jsonEncoder) safeAddByteString(s []byte) {
}

// tryAddRuneSelf appends b if it is valid UTF-8 character represented in a single byte.
func (enc *jsonEncoder) tryAddRuneSelf(b byte) (ok bool) {
ok = b < utf8.RuneSelf
if !ok {
return
func (enc *jsonEncoder) tryAddRuneSelf(b byte) bool {
if b >= utf8.RuneSelf {
return false
}
if 0x20 <= b && b != '\\' && b != '"' {
enc.buf.AppendByte(b)
return
return true
}
switch b {
case '\\', '"':
Expand All @@ -451,13 +450,13 @@ func (enc *jsonEncoder) tryAddRuneSelf(b byte) (ok bool) {
enc.buf.AppendByte(_hex[b>>4])
enc.buf.AppendByte(_hex[b&0xF])
}
return
return true
}

func (enc *jsonEncoder) tryAddRuneError(r rune, size int) (ok bool) {
ok = r == utf8.RuneError && size == 1
if ok {
func (enc *jsonEncoder) tryAddRuneError(r rune, size int) bool {
if r == utf8.RuneError && size == 1 {
enc.buf.AppendString(`\ufffd`)
return true
}
return
return false
}
4 changes: 1 addition & 3 deletions zapcore/json_encoder_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,11 +467,9 @@ func TestJSONQuick(t *testing.T) {
}
// Test the full range of UTF-8 strings.
check(roundTripsCorrectlyString)
check(roundTripsCorrectlyByteString)

// Focus on ASCII strings.
check(asciiRoundTripsCorrectlyString)

// Same for adding string as []byte.
check(roundTripsCorrectlyByteString)
check(asciiRoundTripsCorrectlyByteString)
}

0 comments on commit 03e7655

Please sign in to comment.