Skip to content

Commit

Permalink
Add Base64 field for encoding byte slices (uber-go#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
prashantv authored Jul 26, 2016
1 parent f9a34fd commit be5696d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package zap

import (
"encoding/base64"
"fmt"
"math"
"time"
Expand Down Expand Up @@ -57,6 +58,13 @@ func Skip() Field {
return Field{fieldType: skipType}
}

// Base64 constructs a field that encodes the given value as a
// padded base64 string. The byte slice is converted to a base64
// string immediately.
func Base64(key string, val []byte) Field {
return String(key, base64.StdEncoding.EncodeToString(val))
}

// Bool constructs a Field with the given key and value.
func Bool(key string, val bool) Field {
var ival int64
Expand Down
7 changes: 7 additions & 0 deletions field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ func TestNestField(t *testing.T) {
assertCanBeReused(t, nest)
}

func TestBase64Field(t *testing.T) {
assertFieldJSON(t, `"foo":"YWIxMg=="`,
Base64("foo", []byte("ab12")),
)
assertCanBeReused(t, Base64("foo", []byte("bar")))
}

func TestLogMarshalerFunc(t *testing.T) {
assertFieldJSON(t, `"foo":{"name":"phil"}`,
Marshaler("foo", LogMarshalerFunc(fakeUser{"phil"}.MarshalLog)))
Expand Down

0 comments on commit be5696d

Please sign in to comment.