Add support for nested fields in log entries and JSON marshalling

This commit introduces the ability to handle nested fields within log entries.
The  file has been updated with a  function
that transforms flat keys with a : delimiter (e.g., obj:foo) into nested
JSON objects (e.g., ).

The  file includes new test cases to verify that:
- Nested fields are correctly unmarshalled from string representations.
- Nested fields are correctly marshalled into JSON objects.

This enhancement allows for more structured and organized metadata within log entries.
This commit is contained in:
Dan Jones 2026-02-10 16:13:39 -06:00
commit c4e864afa5
2 changed files with 34 additions and 0 deletions

View file

@ -125,6 +125,14 @@ func TestEntryUnmarshal(t *testing.T) {
[]Meta{{"me", json.RawMessage(`{"name":"Dan","coder":true}`)}}, []Meta{{"me", json.RawMessage(`{"name":"Dan","coder":true}`)}},
nil, nil,
}, },
{
"nested-field",
"@begin " + whens + " - A Title\n@me:name Dan\n@me:coder true @end",
"A Title",
when,
[]Meta{{"me:name", "Dan"}, {"me:coder", true}},
nil,
},
{ {
"json-field", "json-field",
"@begin " + whens + " - Some Guy\n" + `@json {"name":"Dan","coder":true} @end`, "@begin " + whens + " - Some Guy\n" + `@json {"name":"Dan","coder":true} @end`,
@ -209,6 +217,8 @@ func TestEntryJsonMarshal(t *testing.T) {
{"obj-field", "A Title", when, []Meta{{"obj", json.RawMessage(`{"foo":"bar","title":"Sub-title"}`)}}, `{"title":"A Title","date":"` + whens + `","obj":{"foo":"bar","title":"Sub-title"}}`, nil}, {"obj-field", "A Title", when, []Meta{{"obj", json.RawMessage(`{"foo":"bar","title":"Sub-title"}`)}}, `{"title":"A Title","date":"` + whens + `","obj":{"foo":"bar","title":"Sub-title"}}`, nil},
{"date-field", "A Title", when, []Meta{{"when", when.Add(time.Hour)}}, `{"title":"A Title","date":"` + whens + `","when":"` + when.Add(time.Hour).Format(time.RFC3339) + `"}`, nil}, {"date-field", "A Title", when, []Meta{{"when", when.Add(time.Hour)}}, `{"title":"A Title","date":"` + whens + `","when":"` + when.Add(time.Hour).Format(time.RFC3339) + `"}`, nil},
{"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}, {"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},
} }
for _, tt := range tests { for _, tt := range tests {

View file

@ -2,6 +2,7 @@ package models
import ( import (
"encoding/json" "encoding/json"
"strings"
"time" "time"
) )
@ -33,9 +34,32 @@ func (ms Metas) Map() map[string]any {
} }
} }
} }
// Next we have to go through them again to find nested fields
parseNestedFields(out)
return out return out
} }
func parseNestedFields(f map[string]any) {
for k, v := range f {
if strings.Contains(k, ":") {
idx := strings.Index(k, ":")
top := k[:idx]
bottom := k[(idx + 1):]
nest, ok := f[top].(map[string]any)
if !ok {
nest = map[string]any{}
}
nest[bottom] = v
parseNestedFields(nest)
f[top] = nest
delete(f, k)
}
}
}
// Implements json.Marshaler // Implements json.Marshaler
func (ms Metas) MarshalJSON() ([]byte, error) { func (ms Metas) MarshalJSON() ([]byte, error) {
return json.Marshal(ms.Map()) return json.Marshal(ms.Map())