♻️ Get rid of PartialEntry
This commit is contained in:
parent
7e54d6e46c
commit
820a2de269
5 changed files with 111 additions and 57 deletions
64
models/metas.go
Normal file
64
models/metas.go
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
//"bufio"
|
||||
//"bytes"
|
||||
"encoding/json"
|
||||
//"fmt"
|
||||
//"errors"
|
||||
//"regexp"
|
||||
//"strings"
|
||||
//"sync"
|
||||
"time"
|
||||
//"codeberg.org/danjones000/my-log/tools"
|
||||
)
|
||||
|
||||
type Metas []Meta
|
||||
|
||||
func (ms Metas) toMap() map[string]any {
|
||||
out := map[string]any{}
|
||||
for _, f := range ms {
|
||||
if _, found := out[f.Key]; found || f.Key == "title" || f.Key == "date" {
|
||||
continue
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (ms Metas) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(ms.toMap())
|
||||
}
|
||||
|
||||
func (ms *Metas) UnmarshalJSON(in []byte) error {
|
||||
old := (*ms).toMap()
|
||||
out := map[string]any{}
|
||||
err := json.Unmarshal(in, &out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ret := *ms
|
||||
for k, v := range out {
|
||||
if _, found := old[k]; k != "title" && k != "date" && !found {
|
||||
ret = append(ret, Meta{k, v})
|
||||
}
|
||||
}
|
||||
*ms = ret
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue