✨ Some convenience methods for Metas
This commit is contained in:
parent
8086029b03
commit
79fa957d02
4 changed files with 39 additions and 16 deletions
|
|
@ -42,17 +42,17 @@ var dropCmd = &cobra.Command{
|
|||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
log := args[0]
|
||||
title := args[1]
|
||||
ms := models.Metas{}
|
||||
ms := &models.Metas{}
|
||||
if len(j.RawMessage) > 8 {
|
||||
err := json.Unmarshal([]byte(j.RawMessage), &ms)
|
||||
err := json.Unmarshal([]byte(j.RawMessage), ms)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for k, v := range fields {
|
||||
ms = append(ms, models.Meta{k, tools.ParseString(v)})
|
||||
ms.AppendTo(k, tools.ParseString(v))
|
||||
}
|
||||
e := models.Entry{title, d.t, ms}
|
||||
e := models.Entry{title, d.t, *ms}
|
||||
l := models.Log{log, []models.Entry{e}}
|
||||
err := files.Append(l)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -186,7 +186,7 @@ func (e Entry) MarshalJSON() ([]byte, error) {
|
|||
out := map[string]any{}
|
||||
out["title"] = e.Title
|
||||
out["date"] = e.Date.Format(time.RFC3339)
|
||||
for k, v := range e.Fields.toMap() {
|
||||
for k, v := range e.Fields.Map() {
|
||||
out[k] = v
|
||||
}
|
||||
return json.Marshal(out)
|
||||
|
|
|
|||
|
|
@ -13,6 +13,8 @@ import (
|
|||
// Type assertions
|
||||
var _ encoding.TextMarshaler = Meta{}
|
||||
var _ encoding.TextUnmarshaler = new(Meta)
|
||||
var _ json.Marshaler = Metas{}
|
||||
var _ json.Unmarshaler = new(Metas)
|
||||
|
||||
var skipMarshalTest = errors.New("skip marshal")
|
||||
|
||||
|
|
@ -125,3 +127,17 @@ func TestMetasJsonError(t *testing.T) {
|
|||
assert.Error(t, err)
|
||||
assert.Len(t, ms, 0)
|
||||
}
|
||||
|
||||
func TestMetasAppend(t *testing.T) {
|
||||
ms := Metas{}
|
||||
ms = ms.Append("foo", 42)
|
||||
assert.Len(t, ms, 1)
|
||||
assert.Equal(t, Meta{"foo", 42}, ms[0])
|
||||
}
|
||||
|
||||
func TestMetasAppendTo(t *testing.T) {
|
||||
ms := &Metas{}
|
||||
ms.AppendTo("foo", 42)
|
||||
assert.Len(t, *ms, 1)
|
||||
assert.Equal(t, Meta{"foo", 42}, (*ms)[0])
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,21 +1,15 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
//"bufio"
|
||||
//"bytes"
|
||||
"encoding/json"
|
||||
//"fmt"
|
||||
//"errors"
|
||||
//"regexp"
|
||||
//"strings"
|
||||
//"sync"
|
||||
"time"
|
||||
//"codeberg.org/danjones000/my-log/tools"
|
||||
)
|
||||
|
||||
// A slice of Meta
|
||||
type Metas []Meta
|
||||
|
||||
func (ms Metas) toMap() map[string]any {
|
||||
// Returns a single map containing all the Meta. Is useful when encoding to JSON
|
||||
func (ms Metas) Map() map[string]any {
|
||||
out := map[string]any{}
|
||||
for _, f := range ms {
|
||||
if _, found := out[f.Key]; found || f.Key == "title" || f.Key == "date" {
|
||||
|
|
@ -42,12 +36,14 @@ func (ms Metas) toMap() map[string]any {
|
|||
return out
|
||||
}
|
||||
|
||||
// Implements json.Marshaler
|
||||
func (ms Metas) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(ms.toMap())
|
||||
return json.Marshal(ms.Map())
|
||||
}
|
||||
|
||||
// Implements json.Unmarshaler
|
||||
func (ms *Metas) UnmarshalJSON(in []byte) error {
|
||||
old := (*ms).toMap()
|
||||
old := (*ms).Map()
|
||||
out := map[string]any{}
|
||||
err := json.Unmarshal(in, &out)
|
||||
if err != nil {
|
||||
|
|
@ -62,3 +58,14 @@ func (ms *Metas) UnmarshalJSON(in []byte) error {
|
|||
*ms = ret
|
||||
return nil
|
||||
}
|
||||
|
||||
// Returns a new Metas with a new Meta appended
|
||||
func (ms Metas) Append(k string, v any) Metas {
|
||||
return append(ms, Meta{k, v})
|
||||
}
|
||||
|
||||
// Appends a new Meta to this Metas
|
||||
func (ms *Metas) AppendTo(k string, v any) {
|
||||
n := (*ms).Append(k, v)
|
||||
*ms = n
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue