Skip to content

Commit

Permalink
Merge pull request #2 from alexsn/zap-types
Browse files Browse the repository at this point in the history
Add support for zap.Stringer and zap.Duration types
  • Loading branch information
bvandewalle authored Feb 14, 2019
2 parents 65aa7fc + 113b907 commit 6415450
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions utils/fields.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package utils

import (
"fmt"
"math"
"time"

opentracinglog "github.com/opentracing/opentracing-go/log"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -40,10 +42,14 @@ func ZapFieldToOpentracing(zapField zapcore.Field) opentracinglog.Field {
return opentracinglog.Int32(zapField.Key, int32(zapField.Integer))
case zapcore.StringType:
return opentracinglog.String(zapField.Key, zapField.String)
case zapcore.StringerType:
return opentracinglog.String(zapField.Key, zapField.Interface.(fmt.Stringer).String())
case zapcore.Uint64Type:
return opentracinglog.Uint64(zapField.Key, uint64(zapField.Integer))
case zapcore.Uint32Type:
return opentracinglog.Uint32(zapField.Key, uint32(zapField.Integer))
case zapcore.DurationType:
return opentracinglog.String(zapField.Key, time.Duration(zapField.Integer).String())
case zapcore.ErrorType:
return opentracinglog.Error(zapField.Interface.(error))
default:
Expand Down
21 changes: 21 additions & 0 deletions utils/fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ package utils

import (
"testing"
"time"

opentracinglog "github.com/opentracing/opentracing-go/log"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

type stringer struct {
string
}

func (s stringer) String() string {
return s.string
}

func TestFieldsConversion(t *testing.T) {

TestData := []struct {
Expand All @@ -26,6 +35,14 @@ func TestFieldsConversion(t *testing.T) {
zap.String("", "123"),
opentracinglog.String("", "123"),
},
{
zap.Stringer("namespace", stringer{""}),
opentracinglog.String("namespace", ""),
},
{
zap.Stringer("", stringer{"123"}),
opentracinglog.String("", "123"),
},
{
zap.Int("namespace", 1),
opentracinglog.Int64("namespace", 1),
Expand All @@ -46,6 +63,10 @@ func TestFieldsConversion(t *testing.T) {
zap.Uint64("namespace", 1),
opentracinglog.Uint64("namespace", 1),
},
{
zap.Duration("namespace", time.Second),
opentracinglog.String("namespace", "1s"),
},
{
zap.Float32("namespace", 1),
opentracinglog.Float32("namespace", 1),
Expand Down

0 comments on commit 6415450

Please sign in to comment.