From 5c29ef016d5c2526a526a903b4e8c4d0af4585f4 Mon Sep 17 00:00:00 2001 From: erezrokah Date: Mon, 22 Jul 2024 15:49:44 +0100 Subject: [PATCH 1/7] feat: Add method to get event fields --- event.go | 18 ++++++++++++++++++ event_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/event.go b/event.go index 56de6061..16181769 100644 --- a/event.go +++ b/event.go @@ -1,7 +1,9 @@ package zerolog import ( + "bytes" "context" + "encoding/json" "fmt" "net" "os" @@ -828,3 +830,19 @@ func (e *Event) MACAddr(key string, ha net.HardwareAddr) *Event { e.buf = enc.AppendMACAddr(enc.AppendKey(e.buf, key), ha) return e } + +func (e *Event) GetFields() (map[string]interface{}, error) { + if e == nil { + return nil, nil + } + + eventFields := make(map[string]interface{}) + buffer := e.buf + if !bytes.HasSuffix(e.buf, []byte("}\n")) { + buffer = append(e.buf, '}') + } + if err := json.Unmarshal(buffer, &eventFields); err != nil { + return nil, err + } + return eventFields, nil +} diff --git a/event_test.go b/event_test.go index e7cf4250..f0cc9855 100644 --- a/event_test.go +++ b/event_test.go @@ -1,3 +1,4 @@ +//go:build !binary_log // +build !binary_log package zerolog @@ -63,3 +64,27 @@ func TestEvent_EmbedObjectWithNil(t *testing.T) { t.Errorf("Event.EmbedObject() = %q, want %q", got, want) } } + +func TestEvent_GetFields(t *testing.T) { + e := newEvent(nil, DebugLevel) + e.Str("foo", "bar").Float64("n", 42).Msg("test") + + got, err := e.GetFields() + if err != nil { + t.Error(err) + } + want := map[string]interface{}{ + "foo": "bar", + "n": float64(42), + "message": "test", + } + + if len(got) != len(want) { + t.Errorf("Event.GetFields() = %v, want %v", len(got), len(want)) + } + for k, v := range want { + if got[k] != v { + t.Errorf("Event.GetFields() = %v, want %v", got[k], v) + } + } +} From b6d2efb22f8c6bbf8876d02e8a855a4cf0920961 Mon Sep 17 00:00:00 2001 From: erezrokah Date: Mon, 22 Jul 2024 18:34:13 +0100 Subject: [PATCH 2/7] fix: Add comment, more tests --- event.go | 8 +++--- event_test.go | 70 ++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 58 insertions(+), 20 deletions(-) diff --git a/event.go b/event.go index 16181769..14e32b00 100644 --- a/event.go +++ b/event.go @@ -831,15 +831,17 @@ func (e *Event) MACAddr(key string, ha net.HardwareAddr) *Event { return e } +// GetFields returns the JSON decoded fields of the event. Please note that the fields might not be mapped to their original type as JSON decoder maps numbers to float64, etc. func (e *Event) GetFields() (map[string]interface{}, error) { if e == nil { return nil, nil } eventFields := make(map[string]interface{}) - buffer := e.buf - if !bytes.HasSuffix(e.buf, []byte("}\n")) { - buffer = append(e.buf, '}') + buffer := bytes.TrimSpace(e.buf) + // If `Msg()` was not called the buffer will be missing the closing curly brace + if !bytes.HasSuffix(buffer, []byte("}")) { + buffer = append(buffer, '}') } if err := json.Unmarshal(buffer, &eventFields); err != nil { return nil, err diff --git a/event_test.go b/event_test.go index f0cc9855..d1b7bc70 100644 --- a/event_test.go +++ b/event_test.go @@ -66,25 +66,61 @@ func TestEvent_EmbedObjectWithNil(t *testing.T) { } func TestEvent_GetFields(t *testing.T) { - e := newEvent(nil, DebugLevel) - e.Str("foo", "bar").Float64("n", 42).Msg("test") - - got, err := e.GetFields() - if err != nil { - t.Error(err) - } - want := map[string]interface{}{ - "foo": "bar", - "n": float64(42), - "message": "test", + type testCase struct { + name string + e *Event + message string + want map[string]interface{} } - if len(got) != len(want) { - t.Errorf("Event.GetFields() = %v, want %v", len(got), len(want)) + testCases := []testCase{ + { + name: "event without message", + e: newEvent(nil, DebugLevel).Str("foo", "bar").Float64("n", 42), + want: map[string]interface{}{ + "foo": "bar", + "n": float64(42), + }, + }, + { + name: "event without message and integer", + e: newEvent(nil, DebugLevel).Str("foo", "bar").Int("n", 42), + want: map[string]interface{}{ + "foo": "bar", + "n": float64(42), + }, + }, + { + name: "event with message", + e: newEvent(nil, DebugLevel).Str("foo", "bar").Float64("n", 42), + message: "test", + want: map[string]interface{}{ + "foo": "bar", + "n": float64(42), + "message": "test", + }, + }, } - for k, v := range want { - if got[k] != v { - t.Errorf("Event.GetFields() = %v, want %v", got[k], v) - } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + if tc.message != "" { + tc.e.Msg(tc.message) + } + got, err := tc.e.GetFields() + if err != nil { + t.Error(err) + } + + if len(got) != len(tc.want) { + t.Errorf("Event.GetFields() = %v, want %v", len(got), len(tc.want)) + } + for k, v := range tc.want { + if got[k] != v { + t.Errorf("Event.GetFields() = %v, want %v", got[k], v) + } + } + }) + } } From 18c2230b6d5d8d1407a6ccc1b2f9dcbd28f13938 Mon Sep 17 00:00:00 2001 From: Erez Rokah Date: Mon, 22 Jul 2024 20:35:10 +0300 Subject: [PATCH 3/7] Update event_test.go --- event_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/event_test.go b/event_test.go index d1b7bc70..a52d50a5 100644 --- a/event_test.go +++ b/event_test.go @@ -1,4 +1,3 @@ -//go:build !binary_log // +build !binary_log package zerolog From 3c94c5b77866d65789537b579768510a96f62b94 Mon Sep 17 00:00:00 2001 From: Erez Rokah Date: Mon, 22 Jul 2024 22:23:59 +0300 Subject: [PATCH 4/7] Update event.go Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com> --- event.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/event.go b/event.go index 14e32b00..9ecb0830 100644 --- a/event.go +++ b/event.go @@ -840,7 +840,7 @@ func (e *Event) GetFields() (map[string]interface{}, error) { eventFields := make(map[string]interface{}) buffer := bytes.TrimSpace(e.buf) // If `Msg()` was not called the buffer will be missing the closing curly brace - if !bytes.HasSuffix(buffer, []byte("}")) { + if !bytes.HasSuffix(buffer, '}')) { buffer = append(buffer, '}') } if err := json.Unmarshal(buffer, &eventFields); err != nil { From 91c7c91fd6597486870bcd1d83002cc4bd50aa73 Mon Sep 17 00:00:00 2001 From: erezrokah Date: Mon, 22 Jul 2024 20:28:29 +0100 Subject: [PATCH 5/7] refactor: Rename to GetMetadata --- event.go | 6 +++--- event_test.go | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/event.go b/event.go index 9ecb0830..da77657b 100644 --- a/event.go +++ b/event.go @@ -831,8 +831,8 @@ func (e *Event) MACAddr(key string, ha net.HardwareAddr) *Event { return e } -// GetFields returns the JSON decoded fields of the event. Please note that the fields might not be mapped to their original type as JSON decoder maps numbers to float64, etc. -func (e *Event) GetFields() (map[string]interface{}, error) { +// GetMetadata returns the JSON decoded fields of the event. Please note that the fields might not be mapped to their original type as JSON decoder maps numbers to float64, etc. +func (e *Event) GetMetadata() (map[string]interface{}, error) { if e == nil { return nil, nil } @@ -840,7 +840,7 @@ func (e *Event) GetFields() (map[string]interface{}, error) { eventFields := make(map[string]interface{}) buffer := bytes.TrimSpace(e.buf) // If `Msg()` was not called the buffer will be missing the closing curly brace - if !bytes.HasSuffix(buffer, '}')) { + if !bytes.HasSuffix(buffer, []byte("}")) { buffer = append(buffer, '}') } if err := json.Unmarshal(buffer, &eventFields); err != nil { diff --git a/event_test.go b/event_test.go index a52d50a5..2b6cbab6 100644 --- a/event_test.go +++ b/event_test.go @@ -1,3 +1,4 @@ +//go:build !binary_log // +build !binary_log package zerolog @@ -106,7 +107,7 @@ func TestEvent_GetFields(t *testing.T) { if tc.message != "" { tc.e.Msg(tc.message) } - got, err := tc.e.GetFields() + got, err := tc.e.GetMetadata() if err != nil { t.Error(err) } From 13b7ac2c6aa749629673f5f24dfb2ba3ba7093fd Mon Sep 17 00:00:00 2001 From: erezrokah Date: Mon, 22 Jul 2024 20:29:29 +0100 Subject: [PATCH 6/7] chore: Update comment --- event.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/event.go b/event.go index da77657b..935a69ca 100644 --- a/event.go +++ b/event.go @@ -831,7 +831,7 @@ func (e *Event) MACAddr(key string, ha net.HardwareAddr) *Event { return e } -// GetMetadata returns the JSON decoded fields of the event. Please note that the fields might not be mapped to their original type as JSON decoder maps numbers to float64, etc. +// GetMetadata returns the JSON decoded metadata of the event. Please note that the metadata might not be mapped to their original field type as JSON decoder maps numbers to float64, etc. func (e *Event) GetMetadata() (map[string]interface{}, error) { if e == nil { return nil, nil From f6d674dbec4b018409abf57e38092f1f2b05c83e Mon Sep 17 00:00:00 2001 From: erezrokah Date: Mon, 22 Jul 2024 21:05:46 +0100 Subject: [PATCH 7/7] test: Fix test method name --- event_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/event_test.go b/event_test.go index 2b6cbab6..53081f04 100644 --- a/event_test.go +++ b/event_test.go @@ -65,7 +65,7 @@ func TestEvent_EmbedObjectWithNil(t *testing.T) { } } -func TestEvent_GetFields(t *testing.T) { +func TestEvent_GetMetadata(t *testing.T) { type testCase struct { name string e *Event @@ -113,11 +113,11 @@ func TestEvent_GetFields(t *testing.T) { } if len(got) != len(tc.want) { - t.Errorf("Event.GetFields() = %v, want %v", len(got), len(tc.want)) + t.Errorf("Event.GetMetadata() = %v, want %v", len(got), len(tc.want)) } for k, v := range tc.want { if got[k] != v { - t.Errorf("Event.GetFields() = %v, want %v", got[k], v) + t.Errorf("Event.GetMetadata() = %v, want %v", got[k], v) } } })