♻️ Get rid of PartialEntry

This commit is contained in:
Dan Jones 2024-03-10 15:26:00 -05:00
commit 820a2de269
5 changed files with 111 additions and 57 deletions

View file

@ -16,14 +16,9 @@ import (
const DateFormat = tools.DateFormat
type Entry struct {
Title string
Date time.Time
Fields []Meta
skipMissing bool
}
func PartialEntry() Entry {
return Entry{skipMissing: true}
Title string
Date time.Time
Fields Metas
}
type metaRes struct {
@ -42,9 +37,9 @@ func (e Entry) getFieldMarshalChan() chan metaRes {
defer wg.Done()
if m.Key == "json" {
if j, ok := m.Value.(json.RawMessage); ok {
sub := Entry{skipMissing: true}
sub := Metas{}
json.Unmarshal(j, &sub)
for _, subM := range sub.Fields {
for _, subM := range sub {
o, er := subM.MarshalText()
ch <- metaRes{o, er}
}
@ -160,9 +155,9 @@ func (e *Entry) getFieldUnarshalChan(in []byte) chan Meta {
if err == nil {
if m.Key == "json" {
if j, ok := m.Value.(json.RawMessage); ok {
sub := Entry{skipMissing: true}
json.Unmarshal(j, &sub)
for _, subM := range sub.Fields {
ms := Metas{}
json.Unmarshal(j, &ms)
for _, subM := range ms {
ch <- subM
}
}
@ -191,26 +186,8 @@ func (e Entry) MarshalJSON() ([]byte, error) {
out := map[string]any{}
out["title"] = e.Title
out["date"] = e.Date.Format(time.RFC3339)
for _, f := range e.Fields {
if _, ok := out[f.Key]; !ok {
if f.Key == "json" {
ob := map[string]any{}
if j, ok := f.Value.(json.RawMessage); ok {
json.Unmarshal(j, &ob)
}
// If we couldn't get valid data from there, this will just be empty
for k, v := range ob {
if k != "title" && k != "date" {
out[k] = v
}
}
} else {
out[f.Key] = f.Value
if vt, ok := f.Value.(time.Time); ok {
out[f.Key] = vt.Format(time.RFC3339)
}
}
}
for k, v := range e.Fields.toMap() {
out[k] = v
}
return json.Marshal(out)
}
@ -259,16 +236,16 @@ func (e *Entry) UnmarshalJSON(in []byte) error {
return newParsingError(err)
}
title, ok := out["title"].(string)
if (!ok || title == "") && !e.skipMissing {
if !ok || title == "" {
return ErrorMissingTitle
}
e.Title = title
dates, ok := out["date"].(string)
if (!ok || dates == "") && !e.skipMissing {
if !ok || dates == "" {
return ErrorMissingDate
}
date, err := tools.ParseDate(dates)
if err != nil && !e.skipMissing {
if err != nil {
return newParsingError(err)
}
e.Date = date