Entry implements json.Marshaler

This commit is contained in:
Dan Jones 2024-01-28 19:41:30 -06:00
commit f3bf89b09e
2 changed files with 22 additions and 2 deletions

View file

@ -3,6 +3,7 @@ package models
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"regexp"
"strings"
@ -157,5 +158,23 @@ func (e *Entry) getFieldUnarshalChan(in []byte) chan Meta {
}
func (e Entry) MarshalJSON() ([]byte, error) {
return []byte(`{}`), nil
if e.Title == "" {
return []byte{}, ErrorMissingTitle
}
if e.Date == (time.Time{}) {
return []byte{}, ErrorMissingDate
}
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 {
out[f.Key] = f.Value
if vt, ok := f.Value.(time.Time); ok {
out[f.Key] = vt.Format(time.RFC3339)
}
}
}
return json.Marshal(out)
}