From 28a15c8a1eb6842664fe411cbb55bc82bee64639 Mon Sep 17 00:00:00 2001 From: instabledesign Date: Mon, 27 Feb 2023 18:30:58 +0100 Subject: [PATCH] Bugfix Cast nil field (#84) --- field.go | 6 ++++++ field_test.go | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/field.go b/field.go index 92881aa..a448045 100644 --- a/field.go +++ b/field.go @@ -54,6 +54,9 @@ type Field struct { // String will return Field as string func (f *Field) String() string { + if f.Value == nil { + return "" + } switch f.Type { case SkipType: return "" @@ -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 diff --git a/field_test.go b/field_test.go index b5a33ba..6c561cb 100644 --- a/field_test.go +++ b/field_test.go @@ -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}, } @@ -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: ""}, {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: ""}, {field: logger.Reflect("Reflect field", struct{}{}), expectedString: "{}"}, } @@ -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: "{}"}, }