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

@ -2,6 +2,7 @@ package models
import (
"encoding/json"
"strings"
"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
}
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
func (ms Metas) MarshalJSON() ([]byte, error) {
return json.Marshal(ms.Map())