Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoder namespace bug #1

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 59 additions & 11 deletions zapcore/json_encoder_impl_test.go
Expand Up @@ -220,10 +220,28 @@ func TestJSONEncoderObjectFields(t *testing.T) {
e.OpenNamespace("innermost")
},
},
{
desc: "object (no nested namespace)",
expected: `"obj":{"obj-out":"obj-outside-namespace"},"not-obj":"should-be-outside-obj"`,
f: func(e Encoder) {
e.AddObject("obj", maybeNamespace{false})
e.AddString("not-obj", "should-be-outside-obj")
},
},
{
desc: "object (with nested namespace)",
expected: `"obj":{"obj-out":"obj-outside-namespace","obj-namespace":{"obj-in":"obj-inside-namespace"}},"not-obj":"should-be-outside-obj"`,
f: func(e Encoder) {
e.AddObject("obj", maybeNamespace{true})
e.AddString("not-obj", "should-be-outside-obj")
},
},
}

for _, tt := range tests {
assertOutput(t, tt.desc, tt.expected, tt.f)
t.Run(tt.desc, func(t *testing.T) {
assertOutput(t, tt.desc, tt.expected, tt.f)
})
}
}

Expand Down Expand Up @@ -311,19 +329,37 @@ func TestJSONEncoderArrays(t *testing.T) {
)
},
},
{
desc: "object (no nested namespace) then string",
expected: `[{"obj-out":"obj-outside-namespace"},"should-be-outside-obj",{"obj-out":"obj-outside-namespace"},"should-be-outside-obj"]`,
f: func(arr ArrayEncoder) {
arr.AppendObject(maybeNamespace{false})
arr.AppendString("should-be-outside-obj")
},
},
{
desc: "object (with nested namespace) then string",
expected: `[{"obj-out":"obj-outside-namespace","obj-namespace":{"obj-in":"obj-inside-namespace"}},"should-be-outside-obj",{"obj-out":"obj-outside-namespace","obj-namespace":{"obj-in":"obj-inside-namespace"}},"should-be-outside-obj"]`,
f: func(arr ArrayEncoder) {
arr.AppendObject(maybeNamespace{true})
arr.AppendString("should-be-outside-obj")
},
},
}

for _, tt := range tests {
f := func(enc Encoder) error {
return enc.AddArray("array", ArrayMarshalerFunc(func(arr ArrayEncoder) error {
tt.f(arr)
tt.f(arr)
return nil
}))
}
assertOutput(t, tt.desc, `"array":`+tt.expected, func(enc Encoder) {
err := f(enc)
assert.NoError(t, err, "Unexpected error adding array to JSON encoder.")
t.Run(tt.desc, func(t *testing.T) {
f := func(enc Encoder) error {
return enc.AddArray("array", ArrayMarshalerFunc(func(arr ArrayEncoder) error {
tt.f(arr)
tt.f(arr)
return nil
}))
}
assertOutput(t, tt.desc, `"array":`+tt.expected, func(enc Encoder) {
err := f(enc)
assert.NoError(t, err, "Unexpected error adding array to JSON encoder.")
})
})
}
}
Expand Down Expand Up @@ -396,6 +432,18 @@ func (l loggable) MarshalLogArray(enc ArrayEncoder) error {
return nil
}

// maybeNamespace is an ObjectMarshaler that sometimes opens a namespace
type maybeNamespace struct{ bool }

func (m maybeNamespace) MarshalLogObject(enc ObjectEncoder) error {
enc.AddString("obj-out", "obj-outside-namespace")
if m.bool {
enc.OpenNamespace("obj-namespace")
enc.AddString("obj-in", "obj-inside-namespace")
}
return nil
}

type noJSON struct{}

func (nj noJSON) MarshalJSON() ([]byte, error) {
Expand Down
98 changes: 83 additions & 15 deletions zapcore/memory_encoder_test.go
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMapObjectEncoderAdd(t *testing.T) {
Expand Down Expand Up @@ -201,12 +202,45 @@ func TestMapObjectEncoderAdd(t *testing.T) {
},
},
},
{
desc: "object (no nested namespace) then string",
f: func(e ObjectEncoder) {
e.OpenNamespace("k")
e.AddObject("obj", maybeNamespace{false})
e.AddString("not-obj", "should-be-outside-obj")
},
expected: map[string]interface{}{
"obj": map[string]interface{}{
"obj-out": "obj-outside-namespace",
},
"not-obj": "should-be-outside-obj",
},
},
{
desc: "object (with nested namespace) then string",
f: func(e ObjectEncoder) {
e.OpenNamespace("k")
e.AddObject("obj", maybeNamespace{true})
e.AddString("not-obj", "should-be-outside-obj")
},
expected: map[string]interface{}{
"obj": map[string]interface{}{
"obj-out": "obj-outside-namespace",
"obj-namespace": map[string]interface{}{
"obj-in": "obj-inside-namespace",
},
},
"not-obj": "should-be-outside-obj",
},
},
}

for _, tt := range tests {
enc := NewMapObjectEncoder()
tt.f(enc)
assert.Equal(t, tt.expected, enc.Fields["k"], "Unexpected encoder output.")
t.Run(tt.desc, func(t *testing.T) {
enc := NewMapObjectEncoder()
tt.f(enc)
assert.Equal(t, tt.expected, enc.Fields["k"], "Unexpected encoder output.")
})
}
}
func TestSliceArrayEncoderAppend(t *testing.T) {
Expand Down Expand Up @@ -252,22 +286,56 @@ func TestSliceArrayEncoderAppend(t *testing.T) {
},
expected: []interface{}{true, false},
},
{
desc: "object (no nested namespace) then string",
f: func(e ArrayEncoder) {
e.AppendArray(ArrayMarshalerFunc(func(inner ArrayEncoder) error {
inner.AppendObject(maybeNamespace{false})
inner.AppendString("should-be-outside-obj")
return nil
}))
},
expected: []interface{}{
map[string]interface{}{
"obj-out": "obj-outside-namespace",
},
"should-be-outside-obj",
},
},
{
desc: "object (with nested namespace) then string",
f: func(e ArrayEncoder) {
e.AppendArray(ArrayMarshalerFunc(func(inner ArrayEncoder) error {
inner.AppendObject(maybeNamespace{true})
inner.AppendString("should-be-outside-obj")
return nil
}))
},
expected: []interface{}{
map[string]interface{}{
"obj-out": "obj-outside-namespace",
"obj-namespace": map[string]interface{}{
"obj-in": "obj-inside-namespace",
},
},
"should-be-outside-obj",
},
},
}

for _, tt := range tests {
enc := NewMapObjectEncoder()
assert.NoError(t, enc.AddArray("k", ArrayMarshalerFunc(func(arr ArrayEncoder) error {
tt.f(arr)
tt.f(arr)
return nil
})), "Expected AddArray to succeed.")
t.Run(tt.desc, func(t *testing.T) {
enc := NewMapObjectEncoder()
assert.NoError(t, enc.AddArray("k", ArrayMarshalerFunc(func(arr ArrayEncoder) error {
tt.f(arr)
tt.f(arr)
return nil
})), "Expected AddArray to succeed.")

arr, ok := enc.Fields["k"].([]interface{})
if !ok {
t.Errorf("Test case %s didn't encode an array.", tt.desc)
continue
}
assert.Equal(t, []interface{}{tt.expected, tt.expected}, arr, "Unexpected encoder output.")
arr, ok := enc.Fields["k"].([]interface{})
require.True(t, ok, "Test case %s didn't encode an array.", tt.desc)
assert.Equal(t, []interface{}{tt.expected, tt.expected}, arr, "Unexpected encoder output.")
})
}
}

Expand Down