From 4fc1c623a0957be936a9ec01f6ee2e2e7ce8bf98 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Tue, 10 Feb 2026 18:15:07 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Implement=20full=20support=20for=20?= =?UTF-8?q?nested=20fields=20in=20Meta=20and=20Entry=20marshalling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit completes the implementation of nested field support. - : - now correctly handles and by recursively flattening them into format. - Introduced for recursive map marshalling. - Refactored for cleaner buffer writing. - : Added comprehensive test cases for nested JSON, nested maps, double-nested maps, and nested keys within JSON to ensure correct marshalling and unmarshalling. - : Updated tests to reflect the new nil handling and removed redundant JSON object test. This allows for more flexible and structured data representation within log entries. --- models/entry_test.go | 37 +++++++++++++++++++++++++++++ models/meta.go | 56 ++++++++++++++++++++++++++++++++++++++++---- models/meta_test.go | 3 +-- 3 files changed, 90 insertions(+), 6 deletions(-) diff --git a/models/entry_test.go b/models/entry_test.go index 852277b..a4fd2b7 100644 --- a/models/entry_test.go +++ b/models/entry_test.go @@ -61,6 +61,42 @@ func TestEntryMarshal(t *testing.T) { []string{"@age 41", "@cool true", "@name Jim"}, nil, }, + { + "nested-json", + "Title N", + when, + []Meta{{"me", json.RawMessage(`{"age": 43, "cool": true}`)}}, + "@begin " + whens + " - Title N", + []string{"@me:age 43", "@me:cool true"}, + nil, + }, + { + "nested-map", + "Title M", + when, + []Meta{{"me", map[string]any{"age": 43, "cool": true}}}, + "@begin " + whens + " - Title M", + []string{"@me:age 43", "@me:cool true"}, + nil, + }, + { + "double-nested-map", + "Title DM", + when, + []Meta{{"me", map[string]any{"age": 43, "name": map[string]any{"first": "Dan", "last": "Jones"}}}}, + "@begin " + whens + " - Title DM", + []string{"@me:age 43", "@me:name:first Dan", "@me:name:last Jones"}, + nil, + }, + { + "nested-keys-in-json", + "Title NKJ", + when, + []Meta{{"me", json.RawMessage(`{"name:first": "Dan", "name:last": "Jones"}`)}}, + "@begin " + whens + " - Title NKJ", + []string{"@me:name:first Dan", "@me:name:last Jones"}, + nil, + }, } for _, tt := range tests { @@ -219,6 +255,7 @@ func TestEntryJsonMarshal(t *testing.T) { {"json-field", "A Title", when, []Meta{{"json", json.RawMessage(`{"age": 41, "cool": true, "name": "Jim"}`)}}, `{"title":"A Title","date":"` + whens + `","age":41,"cool": true, "name": "Jim"}`, nil}, {"nested-field", "A Title", when, []Meta{{"obj:foo", "bar"}, {"obj:title", "Sub-title"}}, `{"title":"A Title","date":"` + whens + `","obj":{"foo":"bar","title":"Sub-title"}}`, nil}, {"double-nested-field", "A Title", when, []Meta{{"obj:foo", "bar"}, {"obj:me:name", "Dan"}, {"obj:me:age", 27}}, `{"title":"A Title","date":"` + whens + `","obj":{"foo":"bar","me":{"name":"Dan","age":27}}}`, nil}, + {"nested-plus-json", "A Title", when, []Meta{{"obj:foo", "bar"}, {"obj:me", json.RawMessage(`{"name":"Dan","age":27}`)}}, `{"title":"A Title","date":"` + whens + `","obj":{"foo":"bar","me":{"name":"Dan","age":27}}}`, nil}, } for _, tt := range tests { diff --git a/models/meta.go b/models/meta.go index dba99b2..c35f302 100644 --- a/models/meta.go +++ b/models/meta.go @@ -2,6 +2,7 @@ package models import ( "bytes" + "encoding/json" "errors" "fmt" "regexp" @@ -18,16 +19,63 @@ func (m Meta) MarshalText() ([]byte, error) { if regexp.MustCompile(`\s`).MatchString(m.Key) { return []byte{}, fmt.Errorf("whitespace is not allowed in key: %s", m.Key) } + buff := &bytes.Buffer{} + + if jv, ok := m.Value.(map[string]any); ok { + err := marshalMap(m.Key, jv, buff) + return buff.Bytes(), err + } + + if jj, ok := m.Value.(json.RawMessage); ok { + mp := map[string]any{} + err := json.Unmarshal(jj, &mp) + if err == nil { + err := marshalMap(m.Key, mp, buff) + return buff.Bytes(), err + } + } + + if err := m.marshalToBuff(buff); err != nil { + return nil, err + } + + return buff.Bytes(), nil +} + +func marshalMap(pre string, mp map[string]any, buff *bytes.Buffer) error { + var idx uint + for k, v := range mp { + if idx > 0 { + buff.WriteRune('\n') + } + idx++ + if subM, ok := v.(map[string]any); ok { + if err := marshalMap(pre+":"+k, subM, buff); err != nil { + return err + } + } else { + mSub := Meta{pre + ":" + k, v} + if err := mSub.marshalToBuff(buff); err != nil { + return err + } + } + } + return nil +} + +func (m Meta) marshalToBuff(buff *bytes.Buffer) error { buff.WriteRune('@') buff.WriteString(m.Key) buff.WriteRune(' ') n, err := tools.WriteValue(buff, m.Value) - if n == 0 || err != nil { - return []byte{}, err + if err != nil { + return err } - - return buff.Bytes(), nil + if n == 0 { + return ErrorParsing + } + return nil } func (m *Meta) UnmarshalText(in []byte) error { diff --git a/models/meta_test.go b/models/meta_test.go index 29e32a1..c6dd0e4 100644 --- a/models/meta_test.go +++ b/models/meta_test.go @@ -35,12 +35,11 @@ func TestMeta(t *testing.T) { {"json number", "num", json.Number("42.13"), "@num 42.13", nil, 42.13}, {"true", "b", true, "@b true", nil, true}, {"false", "b", false, "@b false", nil, false}, - {"nil", "n", nil, "", nil, ErrorParsing}, + {"nil", "n", nil, "", ErrorParsing, ErrorParsing}, {"time", "when", when, "@when " + when.Format(time.RFC3339), nil, when}, {"rune", "char", '@', "@char @", nil, "@"}, {"bytes", "byteme", []byte("yo"), "@byteme yo", nil, "yo"}, {"byte", "byteme", byte(67), "@byteme C", nil, "C"}, - {"json-obj", "obj", json.RawMessage(`{"foo":"bar","baz":"quux"}`), `@obj {"foo":"bar","baz":"quux"}`, nil, json.RawMessage(`{"foo":"bar","baz":"quux"}`)}, {"json-arr", "arr", json.RawMessage(`["foo",42,"bar", null,"quux", true]`), `@arr ["foo",42,"bar", null,"quux", true]`, nil, json.RawMessage(`["foo",42,"bar", null,"quux", true]`)}, {"chan", "nope", make(chan bool), "", errors.New("Unsupported type chan bool"), ""}, {"whitespace-key", "no space", "hi", "", errors.New("whitespace is not allowed in key: no space"), ""},