Skip to content

Commit

Permalink
Bugfix Cast nil field (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
instabledesign authored Feb 27, 2023
1 parent 4892eb1 commit 28a15c8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ type Field struct {

// String will return Field as string
func (f *Field) String() string {
if f.Value == nil {
return "<nil>"
}
switch f.Type {
case SkipType:
return "<skipped>"
Expand Down Expand Up @@ -113,6 +116,9 @@ func (f *Field) MarshalJSON() ([]byte, error) {
if marshallable, ok := f.Value.(json.Marshaler); ok {
return marshallable.MarshalJSON()
}
if f.Type == ErrorType && f.Value == nil {
return []byte("null"), nil
}
switch f.Type {
case BoolType, Int8Type, Int16Type, Int32Type, Int64Type, Uint8Type, Uint16Type, Uint32Type, Uint64Type, UintptrType, Float32Type, Float64Type:
return []byte(f.String()), nil
Expand Down
13 changes: 13 additions & 0 deletions field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,17 @@ func TestField(t *testing.T) {
{field: logger.Complex64("Complex64 field", 123), expectedValue: complex64(123), expectedType: logger.Complex64Type},
{field: logger.Complex128("Complex128 field", 123), expectedValue: complex128(123), expectedType: logger.Complex128Type},
{field: logger.String("String field", "my_value"), expectedValue: "my_value", expectedType: logger.StringType},
{field: logger.Binary("Binary field null", nil), expectedValue: []byte(nil), expectedType: logger.BinaryType},
{field: logger.Binary("Binary field", []byte("my_value")), expectedValue: []byte("my_value"), expectedType: logger.BinaryType},
{field: logger.ByteString("ByteString field null", nil), expectedValue: []byte(nil), expectedType: logger.ByteStringType},
{field: logger.ByteString("ByteString field", []byte("my_value")), expectedValue: []byte("my_value"), expectedType: logger.ByteStringType},
{field: logger.Error("Error nil field", nil), expectedValue: nil, expectedType: logger.ErrorType},
{field: logger.Error("Error field", err), expectedValue: err, expectedType: logger.ErrorType},
{field: logger.Time("Time field", now), expectedValue: now, expectedType: logger.TimeType},
{field: logger.Duration("Duration field", 5*time.Second), expectedValue: 5 * time.Second, expectedType: logger.DurationType},
{field: logger.Stringer("Stringer nil field", nil), expectedValue: nil, expectedType: logger.StringerType},
{field: logger.Stringer("Stringer field", MyStringer{}), expectedValue: MyStringer{}, expectedType: logger.StringerType},
{field: logger.Reflect("Reflect nil field", nil), expectedValue: nil, expectedType: logger.ReflectType},
{field: logger.Reflect("Reflect field", struct{}{}), expectedValue: struct{}{}, expectedType: logger.ReflectType},
}

Expand Down Expand Up @@ -127,12 +132,16 @@ func TestField_String(t *testing.T) {
{field: logger.Complex64("Complex64 field", 6+7i), expectedString: "(6+7i)"},
{field: logger.Complex128("Complex128 field", 6+7i), expectedString: "(6+7i)"},
{field: logger.String("String field", "my_value"), expectedString: "my_value"},
{field: logger.Binary("Binary nil field", nil), expectedString: ""},
{field: logger.Binary("Binary field", []byte{1, 2, 3}), expectedString: "\x01\x02\x03"},
{field: logger.ByteString("ByteString nil field", nil), expectedString: ""},
{field: logger.ByteString("ByteString field", []byte("my_value")), expectedString: "my_value"},
{field: logger.Error("Error nil field", nil), expectedString: "<nil>"},
{field: logger.Error("Error field", err), expectedString: "my_error_value"},
{field: logger.Time("Time field", now), expectedString: now.String()},
{field: logger.Duration("Duration field", 5*time.Second), expectedString: "5s"},
{field: logger.Stringer("Stringer field", MyStringer{}), expectedString: "my_stringer"},
{field: logger.Reflect("Reflect nil field", nil), expectedString: "<nil>"},
{field: logger.Reflect("Reflect field", struct{}{}), expectedString: "{}"},
}

Expand Down Expand Up @@ -170,12 +179,16 @@ func TestField_MarshalJSON(t *testing.T) {
{field: logger.Complex64("Complex64 field", 6+7i), expectedString: `"(6+7i)"`},
{field: logger.Complex128("Complex128 field", 6+7i), expectedString: `"(6+7i)"`},
{field: logger.String("String field", "my_value"), expectedString: `"my_value"`},
{field: logger.Binary("Binary nil field", nil), expectedString: `""`},
{field: logger.Binary("Binary field", []byte{1, 2, 3}), expectedString: `"\x01\x02\x03"`},
{field: logger.ByteString("ByteString nil field", nil), expectedString: `""`},
{field: logger.ByteString("ByteString field", []byte("my_value")), expectedString: `"my_value"`},
{field: logger.Error("Error nil field", nil), expectedString: "null"},
{field: logger.Error("Error field", err), expectedString: `"my_error_value"`},
{field: logger.Time("Time field", now), expectedString: string(nowMarshalled)},
{field: logger.Duration("Duration field", 5*time.Second), expectedString: `"5s"`},
{field: logger.Stringer("Stringer field", MyStringer{}), expectedString: `"my_stringer"`},
{field: logger.Reflect("Reflect nil field", nil), expectedString: "null"},
{field: logger.Reflect("Reflect field", struct{}{}), expectedString: "{}"},
}

Expand Down

0 comments on commit 28a15c8

Please sign in to comment.