Compare commits

...

33 commits

Author SHA1 Message Date
fba5551bb3 🔨 Fix make open-report for Darwin 2024-10-09 15:14:37 -05:00
85671a076c ✏️ Fix Makefile so it doesn't re-run test and fmt when it doesn't need to 2024-10-09 15:13:34 -05:00
da35bf4bcf Fix TestMkdirErr on Darwin 2024-10-09 15:08:37 -05:00
345ef9471d 🔀 Merge branch 'rel/0.0.6' into stable 2024-10-07 15:53:04 -05:00
5f6ce7ca21 📝 Update CHANGELOG 2024-10-07 15:52:37 -05:00
514ffee613 🔀 Merge branch 'feature/date-parser-refactor' into develop 2024-10-07 15:51:06 -05:00
ece9256745 👽️ Support changes in go-dateparser 2024-10-07 15:50:33 -05:00
e9c1bf7070 ♻️ Return bool directly
Silly me
2024-10-07 15:50:02 -05:00
306b2c597d ⬆️ Upgrade go-dateparser to new version
Don't need my replaced version anymore
2024-10-07 15:49:13 -05:00
ab0dea2805 🔀 Merge tag 'v0.0.5' into develop
 Add --output_json option for drop command
2024-10-07 14:20:50 -05:00
7312714ccb 🔀 Merge branch 'rel/0.0.5' into stable 2024-10-07 14:19:29 -05:00
9980ae8cc7 📝 Update CHANGELOG 2024-10-07 14:18:32 -05:00
02a7babb36 Syntactic sugar in drop command for outputting json 2024-10-07 14:15:28 -05:00
80e07dbd14 🔀 Merge tag 'v0.0.4' into develop
🔖 v0.0.4 with config -p flag
2024-05-08 15:07:33 -05:00
9ca2b9156d 🔀 Merge branch 'rel/0.0.4' into stable 2024-05-08 15:06:50 -05:00
b456bcbfcc 📝 Update CHANGELOG 2024-05-08 15:06:22 -05:00
bddf8e1458 Add -p flag to config 2024-05-08 15:04:18 -05:00
396f19f691 🔀 Merge tag 'v0.0.3' into develop
🔖 json and zero formatters
2024-03-11 21:29:44 -05:00
2e70a10d94 🔀 Merge branch 'rel/0.0.3' into stable 2024-03-11 21:28:59 -05:00
821ba6117c 📝 Update CHANGELOG 2024-03-11 21:28:22 -05:00
632c7143f1 Add null formatter 2024-03-11 21:18:52 -05:00
79fa957d02 Some convenience methods for Metas 2024-03-11 16:04:29 -05:00
8086029b03 🚚 Rename stdout.config.formatter to format 2024-03-10 21:08:34 -05:00
d6482952a4 📝 Update README with JSON format 2024-03-10 20:38:41 -05:00
e67f159ce7 🔀 Merge branch 'feature/form-more' into develop 2024-03-10 20:35:30 -05:00
1962e1db50 Pretty print JSON 2024-03-10 20:31:50 -05:00
4c0edcd1a5 Add JSON formatter 2024-03-10 20:31:49 -05:00
d1b3604e1e Add Formatter.Logs 2024-03-10 20:31:49 -05:00
8d238d0d76 🔀 Merge branch 'feature/types' into develop 2024-03-10 20:30:52 -05:00
59634f6c3f 🐛 WriteValue can handle slices and maps 2024-03-10 15:49:36 -05:00
820a2de269 ♻️ Get rid of PartialEntry 2024-03-10 15:26:00 -05:00
7e54d6e46c Fix parse_date_test for DST 2024-03-10 13:50:13 -05:00
b89baa3f82 🔀 Merge tag 'v0.0.2' into develop
🔖 Formatting changes
2024-03-09 17:40:19 -06:00
30 changed files with 522 additions and 96 deletions

View file

@ -1,5 +1,22 @@
# Changelog
## [0.0.6] - 2024-10-07
- Update external dependency: go-dateparser
## [0.0.5] - 2024-10-07
- Small change: adds --output_json to drop command.
## [0.0.4] - 2024-05-08
- ✨ Add -p flag to config to print config path
## [0.0.3] - 2024-03-11
- ✨ Add JSON formatter
- 💥 Breaking change: renamed output.stdout.config value formatter to format
## [0.0.2] - 2024-03-09
- ✨ Use plain formatter to output entry from drop

View file

@ -3,6 +3,12 @@ OUT=my-log
GOBIN=$(shell go env GOBIN)
COVEROUT=cover.out
COVERHTML=cover.html
OPEN=xdg-open
OS=$(shell uname -s)
ifeq ($(OS),Darwin)
OPEN=open
endif
.PHONY: help
help: ## Show help for documented recipes
@ -23,7 +29,8 @@ test: ## Test application and generate coverage report
$(MAKE) clean
$(MAKE) $(COVEROUT)
$(COVEROUT): $(SOURCES) fmt
$(COVEROUT): $(SOURCES)
$(MAKE) fmt
go test ./... -race -cover -coverprofile $@
$(COVERHTML): $(COVEROUT)
@ -33,8 +40,8 @@ $(COVERHTML): $(COVEROUT)
report: $(COVERHTML) ## Generate a coverage report
.PHONY: open-report
open-report: report ## Open the coverage report in the default browser
xdg-open $(COVERHTML)
open-report: $(COVERHTML) ## Open the coverage report in the default browser
$(OPEN) $<
.PHONY: build
build: $(OUT) ## Builds the application

View file

@ -123,7 +123,7 @@ Each separate output has its own set of configuration. So, replace `which-one` w
*This section may change in the near future. We're considering supporting multiple formats.*
- `formatter`: Which formatter to use when outputting data. This value is used by `my-log drop` to output the new entry.
- `format`: Which formatter to use when outputting data. This value is also used by `my-log drop` to output the new entry.
### `[formatters]`
@ -144,7 +144,7 @@ Some formatters may have custom configuration.
+ [ ] filter to specific logs
+ [ ] stdout
- [x] plain text
- [ ] JSON
- [x] JSON
- [ ] YAML
- [ ] Other formats? Submit an issue!
+ [ ] file output

View file

@ -32,6 +32,11 @@ var configCmd = &cobra.Command{
//Long: ``,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) (err error) {
print, _ := cmd.Flags().GetBool("print")
if print {
fmt.Fprintln(cmd.OutOrStdout(), config.ConfigPath)
return nil
}
force, _ := cmd.Flags().GetBool("force")
if !force {
_, err = os.Stat(config.ConfigPath)
@ -60,4 +65,5 @@ func init() {
rootCmd.AddCommand(configCmd)
configCmd.Flags().BoolP("force", "f", false, "Force overwrite")
configCmd.Flags().BoolP("print", "p", false, "Print path only")
}

View file

@ -21,6 +21,7 @@ import (
"fmt"
"time"
"codeberg.org/danjones000/my-log/config"
"codeberg.org/danjones000/my-log/files"
"codeberg.org/danjones000/my-log/formatters"
"codeberg.org/danjones000/my-log/models"
@ -31,6 +32,7 @@ import (
var d Date
var fields map[string]string
var j Json
var outJson bool
// dropCmd represents the drop command
var dropCmd = &cobra.Command{
@ -40,20 +42,23 @@ var dropCmd = &cobra.Command{
Args: cobra.ExactArgs(2),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if outJson {
config.Overrides["output.stdout.config.format"] = "json"
}
log := args[0]
title := args[1]
e := models.PartialEntry()
ms := &models.Metas{}
if len(j.RawMessage) > 8 {
err := json.Unmarshal([]byte(j.RawMessage), &e)
err := json.Unmarshal([]byte(j.RawMessage), ms)
if err != nil {
return err
}
}
for k, v := range fields {
e.Fields = append(e.Fields, models.Meta{k, tools.ParseString(v)})
ms.AppendTo(k, tools.ParseString(v))
}
e.Title = title
e.Date = d.t
e := models.Entry{title, d.t, *ms}
l := models.Log{log, []models.Entry{e}}
err := files.Append(l)
if err != nil {
@ -68,7 +73,10 @@ var dropCmd = &cobra.Command{
if err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "%s\n", out)
if len(out) > 0 && out[len(out)-1] != 10 {
out = append(out, 10)
}
fmt.Fprintf(cmd.OutOrStdout(), "%s", out)
return nil
},
@ -81,6 +89,7 @@ func init() {
dropCmd.Flags().VarP(&d, "date", "d", "Date for log entry")
dropCmd.Flags().StringToStringVarP(&fields, "fields", "f", nil, "Fields you add to entry")
dropCmd.Flags().VarP(&j, "json", "j", "Entire entry as json")
dropCmd.Flags().BoolVarP(&outJson, "output_json", "o", false, "Output result as JSON")
}
type Json struct {

View file

@ -50,7 +50,7 @@ func init() {
// will be global for your application.
rootCmd.PersistentFlags().StringVarP(&config.ConfigPath, "config", "c", config.ConfigPath, "config file")
rootCmd.PersistentFlags().StringToStringVarP(&config.Overrides, "config-value", "v", config.Overrides, "Override config values. Use dot syntax to specify key. E.g. -v output.stdout.config.formatter=json")
rootCmd.PersistentFlags().StringToStringVarP(&config.Overrides, "config-value", "v", config.Overrides, "Override config values. Use dot syntax to specify key. E.g. -v output.stdout.config.format=json")
// Cobra also supports local flags, which will only run
// when this action is called directly.

View file

@ -28,7 +28,7 @@ dotFolder = true
enabled = true
[output.stdout.config]
# Formatter to use when outputting to stdout
formatter = "plain"
format = "plain"
[formatters]

View file

@ -70,14 +70,14 @@ func TestStdoutMissing(t *testing.T) {
}
func TestStdoutLoad(t *testing.T) {
os.Setenv("LOG_STDOUT_FORMATTER", "json")
defer os.Unsetenv("LOG_STDOUT_FORMATTER")
os.Setenv("LOG_STDOUT_FORMAT", "json")
defer os.Unsetenv("LOG_STDOUT_FORMAT")
os.Setenv("LOG_STDOUT_ENABLED", "true")
defer os.Unsetenv("LOG_STDOUT_ENABLED")
c, _ := Load()
std, en := c.Outputs.Stdout()
assert.True(t, en)
assert.Equal(t, "json", std.Formatter)
assert.Equal(t, "json", std.Format)
}
func TestFormatJson(t *testing.T) {

View file

@ -21,7 +21,7 @@ type Output struct {
}
type Stdout struct {
Formatter string `env:"LOG_STDOUT_FORMATTER" mapstructure:"formatter"`
Format string `env:"LOG_STDOUT_FORMAT" mapstructure:"format"`
}
type stdoutEnabled struct {

View file

@ -211,7 +211,7 @@ func (s *AppendTestSuite) TestConfLoadErr() {
func (s *AppendTestSuite) TestMkdirErr() {
// Don't run this test as root
config.Overrides["input.path"] = "/root/my-logs-test"
config.Overrides["input.path"] = "/var/my-logs-test"
defer func(path string) {
config.Overrides["input.path"] = path
}(s.dir)

View file

@ -4,6 +4,7 @@ import "codeberg.org/danjones000/my-log/models"
type Formatter interface {
Name() string
Logs([]models.Log) (out []byte, err error)
Log(models.Log) (out []byte, err error)
Entry(models.Entry) (out []byte, err error)
Meta(models.Meta) (out []byte, err error)

77
formatters/json.go Normal file
View file

@ -0,0 +1,77 @@
package formatters
import (
"bytes"
"encoding/json"
"time"
"codeberg.org/danjones000/my-log/config"
"codeberg.org/danjones000/my-log/models"
)
func newJson(ff config.Formatters) (Formatter, error) {
return &Json{ff.Json().PrettyPrint}, nil
}
type Json struct {
prettPrint bool
}
func (js *Json) Name() string {
return "json"
}
func (js *Json) marshal(v any) (o []byte, err error) {
o, err = json.Marshal(v)
if err != nil {
return
}
if js.prettPrint {
buff := &bytes.Buffer{}
err = json.Indent(buff, o, "", "\t")
if err == nil {
o = buff.Bytes()
}
}
return
}
func (js *Json) Meta(m models.Meta) ([]byte, error) {
o := map[string]any{m.Key: m.Value}
return js.marshal(o)
}
func (js *Json) entryMap(e models.Entry) map[string]any {
o := map[string]any{
"title": e.Title,
"date": e.Date.Format(time.RFC3339),
}
for _, m := range e.Fields {
o[m.Key] = m.Value
}
return o
}
func (js *Json) Entry(e models.Entry) ([]byte, error) {
return js.marshal(js.entryMap(e))
}
func (js *Json) Log(l models.Log) ([]byte, error) {
return js.Logs([]models.Log{l})
}
func (js *Json) Logs(logs []models.Log) (out []byte, err error) {
if len(logs) == 0 {
return
}
o := map[string][]map[string]any{}
for _, l := range logs {
es := []map[string]any{}
for _, e := range l.Entries {
es = append(es, js.entryMap(e))
}
o[l.Name] = es
}
return js.marshal(o)
}

81
formatters/json_test.go Normal file
View file

@ -0,0 +1,81 @@
package formatters
import (
"fmt"
"testing"
"time"
"codeberg.org/danjones000/my-log/models"
"github.com/stretchr/testify/assert"
)
func TestJsonName(t *testing.T) {
f, _ := New("json")
assert.Equal(t, "json", f.Name())
}
func TestJsonMeta(t *testing.T) {
f, _ := New("json")
m := models.Meta{"foo", 42}
exp := `{"foo":42}`
o, err := f.Meta(m)
assert.NoError(t, err)
assert.JSONEq(t, exp, string(o))
}
func TestJsonEntry(t *testing.T) {
when := time.Now()
f, _ := New("json")
m := models.Meta{"foo", 42}
e := models.Entry{
Title: "Homer",
Date: when,
Fields: []models.Meta{m},
}
exp := fmt.Sprintf(`{"title":"%s","date":"%s","foo":42}`, e.Title, when.Format(time.RFC3339))
o, err := f.Entry(e)
assert.NoError(t, err)
assert.JSONEq(t, exp, string(o))
}
func TestJsonLog(t *testing.T) {
when := time.Now()
f, _ := New("json")
m := models.Meta{"foo", 42}
e := models.Entry{
Title: "Homer",
Date: when,
Fields: []models.Meta{m},
}
l := models.Log{"stuff", []models.Entry{e}}
exp := fmt.Sprintf(`{"%s":[{"title":"%s","date":"%s","foo":42}]}`, l.Name, e.Title, when.Format(time.RFC3339))
o, err := f.Log(l)
assert.NoError(t, err)
assert.JSONEq(t, exp, string(o))
}
func TestJsonNoLogs(t *testing.T) {
f, _ := New("json")
o, err := f.Logs([]models.Log{})
var exp []byte
assert.NoError(t, err)
assert.Equal(t, exp, o)
}
func TestJsonErr(t *testing.T) {
f, _ := New("json")
o, err := f.Meta(models.Meta{"foo", make(chan bool)})
var exp []byte
assert.Error(t, err)
assert.Equal(t, exp, o)
}
func TestJsonPretty(t *testing.T) {
f := Json{true}
o, err := f.Meta(models.Meta{"foo", 42})
exp := `{
"foo": 42
}`
assert.NoError(t, err)
assert.Equal(t, exp, string(o))
}

View file

@ -10,6 +10,8 @@ type formatMaker func(config.Formatters) (Formatter, error)
var formatterMap = map[string]formatMaker{
"plain": newPlain,
"json": newJson,
"zero": newNull,
}
func Preferred() (f Formatter, err error) {
@ -18,8 +20,7 @@ func Preferred() (f Formatter, err error) {
return
}
std, _ := conf.Outputs.Stdout()
kind := std.Formatter
return New(kind)
return New(std.Format)
}
func New(kind string) (f Formatter, err error) {

View file

@ -10,7 +10,7 @@ import (
)
func TestKinds(t *testing.T) {
assert.Equal(t, []string{"plain"}, Kinds())
assert.ElementsMatch(t, []string{"plain", "json", "zero"}, Kinds())
}
func TestNewUnsupported(t *testing.T) {

32
formatters/null.go Normal file
View file

@ -0,0 +1,32 @@
package formatters
import (
"codeberg.org/danjones000/my-log/config"
"codeberg.org/danjones000/my-log/models"
)
func newNull(ff config.Formatters) (Formatter, error) {
return &Null{}, nil
}
type Null struct{}
func (n *Null) Name() string {
return "zero"
}
func (n *Null) Meta(m models.Meta) (o []byte, err error) {
return
}
func (n *Null) Entry(e models.Entry) (o []byte, err error) {
return
}
func (n *Null) Log(l models.Log) (o []byte, err error) {
return
}
func (n *Null) Logs(logs []models.Log) (out []byte, err error) {
return
}

45
formatters/null_test.go Normal file
View file

@ -0,0 +1,45 @@
package formatters
import (
"testing"
"time"
"codeberg.org/danjones000/my-log/models"
"github.com/stretchr/testify/assert"
)
var empty []byte
func TestNullName(t *testing.T) {
f, err := New("zero")
assert.NoError(t, err)
assert.Equal(t, "zero", f.Name())
}
func TestNullMeta(t *testing.T) {
f, _ := New("zero")
o, err := f.Meta(models.Meta{"foo", 42})
assert.NoError(t, err)
assert.Equal(t, empty, o)
}
func TestNullEntry(t *testing.T) {
f, _ := New("zero")
o, err := f.Entry(models.Entry{"title", time.Now(), models.Metas{}})
assert.NoError(t, err)
assert.Equal(t, empty, o)
}
func TestNullLog(t *testing.T) {
f, _ := New("zero")
o, err := f.Log(models.Log{"jim", []models.Entry{{"title", time.Now(), models.Metas{}}}})
assert.NoError(t, err)
assert.Equal(t, empty, o)
}
func TestNullLogs(t *testing.T) {
f, _ := New("zero")
o, err := f.Logs([]models.Log{{"jim", []models.Entry{{"title", time.Now(), models.Metas{}}}}})
assert.NoError(t, err)
assert.Equal(t, empty, o)
}

View file

@ -20,6 +20,26 @@ func (pt *PlainText) Name() string {
return "plain"
}
func (pt *PlainText) Logs(logs []models.Log) (out []byte, err error) {
if len(logs) == 0 {
return
}
buff := &bytes.Buffer{}
first := true
for _, log := range logs {
o, _ := pt.Log(log)
if !first {
buff.WriteByte(10)
buff.WriteByte(10)
}
first = false
buff.Write(o)
}
out = buff.Bytes()
return
}
func (pt *PlainText) Log(log models.Log) (out []byte, err error) {
if len(log.Entries) == 0 {
return

View file

@ -13,7 +13,7 @@ import (
"github.com/stretchr/testify/require"
)
func TestPlainLog(t *testing.T) {
func TestPlainLogs(t *testing.T) {
m := []models.Meta{
{"foo", "bar"},
{"baz", 42},
@ -23,11 +23,17 @@ func TestPlainLog(t *testing.T) {
{Title: "small", Date: time.Now()},
}
l := models.Log{"stuff", e}
e2 := models.Entry{
Title: "three",
Date: time.Now(),
}
l2 := models.Log{"more-stuff", []models.Entry{e2}}
logs := []models.Log{l, l2}
f, err := New("plain")
require.NoError(t, err)
out, err := f.Log(l)
out, err := f.Logs(logs)
require.NoError(t, err)
read := bytes.NewReader(out)
@ -67,6 +73,24 @@ func TestPlainLog(t *testing.T) {
line = scan.Text()
assert.Equal(t, "Date: "+e[1].Date.Format(tools.DateFormat), line)
scan.Scan()
scan.Scan()
line = scan.Text()
assert.Equal(t, l2.Name, line)
scan.Scan()
line = scan.Text()
assert.Equal(t, "#######", line)
scan.Scan()
scan.Scan()
line = scan.Text()
assert.Equal(t, "Title: "+e2.Title, line)
scan.Scan()
line = scan.Text()
assert.Equal(t, "Date: "+e2.Date.Format(tools.DateFormat), line)
more := scan.Scan()
assert.False(t, more)
}
@ -76,6 +100,13 @@ func TestPlainName(t *testing.T) {
assert.Equal(t, "plain", f.Name())
}
func TestPlainLogNone(t *testing.T) {
f, _ := New("plain")
out, err := f.Logs([]models.Log{})
assert.NoError(t, err)
assert.Len(t, out, 0)
}
func TestPlainLogNoEntries(t *testing.T) {
f, _ := New("plain")
out, err := f.Log(models.Log{Name: "foo"})

8
go.mod
View file

@ -5,10 +5,10 @@ go 1.21.5
require (
github.com/BurntSushi/toml v1.3.2
github.com/caarlos0/env/v10 v10.0.0
github.com/markusmobius/go-dateparser v1.2.1
github.com/markusmobius/go-dateparser v1.2.3
github.com/mitchellh/mapstructure v1.5.0
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
)
require (
@ -25,9 +25,7 @@ require (
github.com/tetratelabs/wazero v1.2.1 // indirect
github.com/wasilibs/go-re2 v1.3.0 // indirect
golang.org/x/exp v0.0.0-20220321173239-a90fa8a75705 // indirect
golang.org/x/text v0.10.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
replace github.com/markusmobius/go-dateparser => github.com/goodevilgenius/go-dateparser v1.2.2

12
go.sum
View file

@ -8,8 +8,6 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/elliotchance/pie/v2 v2.7.0 h1:FqoIKg4uj0G/CrLGuMS9ejnFKa92lxE1dEgBD3pShXg=
github.com/elliotchance/pie/v2 v2.7.0/go.mod h1:18t0dgGFH006g4eVdDtWfgFZPQEgl10IoEO8YWEq3Og=
github.com/goodevilgenius/go-dateparser v1.2.2 h1:Up9KokPx/h07mesQGAZQg3Xi/8yrDVn1638h3k/lRyk=
github.com/goodevilgenius/go-dateparser v1.2.2/go.mod h1:5xYsZ1h7iB3sE1BSu8bkjYpbFST7EU1/AFxcyO3mgYg=
github.com/hablullah/go-hijri v1.0.2 h1:drT/MZpSZJQXo7jftf5fthArShcaMtsal0Zf/dnmp6k=
github.com/hablullah/go-hijri v1.0.2/go.mod h1:OS5qyYLDjORXzK4O1adFw9Q5WfhOcMdAKglDkcTxgWQ=
github.com/hablullah/go-juliandays v1.0.0 h1:A8YM7wIj16SzlKT0SRJc9CD29iiaUzpBLzh5hr0/5p0=
@ -24,6 +22,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo=
github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/markusmobius/go-dateparser v1.2.3 h1:TvrsIvr5uk+3v6poDjaicnAFJ5IgtFHgLiuMY2Eb7Nw=
github.com/markusmobius/go-dateparser v1.2.3/go.mod h1:cMwQRrBUQlK1UI5TIFHEcvpsMbkWrQLXuaPNMFzuYLk=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
@ -36,8 +36,8 @@ github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tetratelabs/wazero v1.2.1 h1:J4X2hrGzJvt+wqltuvcSjHQ7ujQxA9gb6PeMs4qlUWs=
github.com/tetratelabs/wazero v1.2.1/go.mod h1:wYx2gNRg8/WihJfSDxA1TIL8H+GkfLYm+bIfbblu9VQ=
github.com/wasilibs/go-re2 v1.3.0 h1:LFhBNzoStM3wMie6rN2slD1cuYH2CGiHpvNL3UtcsMw=
@ -46,8 +46,8 @@ github.com/wasilibs/nottinygc v0.4.0 h1:h1TJMihMC4neN6Zq+WKpLxgd9xCFMw7O9ETLwY2e
github.com/wasilibs/nottinygc v0.4.0/go.mod h1:oDcIotskuYNMpqMF23l7Z8uzD4TC0WXHK8jetlB3HIo=
golang.org/x/exp v0.0.0-20220321173239-a90fa8a75705 h1:ba9YlqfDGTTQ5aZ2fwOoQ1hf32QySyQkR6ODGDzHlnE=
golang.org/x/exp v0.0.0-20220321173239-a90fa8a75705/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58=
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

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.Map() {
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

View file

@ -9,7 +9,6 @@ import (
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Type assertions
@ -18,19 +17,6 @@ var _ encoding.TextUnmarshaler = new(Entry)
var _ json.Marshaler = Entry{}
var _ json.Unmarshaler = new(Entry)
func TestPartialEntry(t *testing.T) {
e := PartialEntry()
assert.True(t, e.skipMissing)
err := json.Unmarshal([]byte(`{"a":42}`), &e)
assert.NoError(t, err)
assert.Equal(t, "", e.Title)
assert.Equal(t, time.Time{}, e.Date)
require.Len(t, e.Fields, 1)
f := e.Fields[0]
assert.Equal(t, "a", f.Key)
assert.Equal(t, int64(42), f.Value)
}
func TestEntryMarshal(t *testing.T) {
when := time.Now()
whens := when.Format(DateFormat)
@ -84,7 +70,7 @@ func TestEntryMarshal(t *testing.T) {
func getEntryMarshalTestRunner(title string, date time.Time, fields []Meta, first string, lines []string, err error) func(*testing.T) {
return func(t *testing.T) {
en := Entry{title, date, fields, false}
en := Entry{title, date, fields}
o, er := en.MarshalText()
assert.Equal(t, err, er)
if first == "" {
@ -232,7 +218,7 @@ func TestEntryJsonMarshal(t *testing.T) {
func getEntryJsonMarshalTestRunner(title string, date time.Time, fields []Meta, out string, err error) func(t *testing.T) {
return func(t *testing.T) {
e := Entry{title, date, fields, false}
e := Entry{title, date, fields}
o, er := json.Marshal(e)
if err == nil {
assert.JSONEq(t, out, string(o))

View file

@ -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")
@ -97,3 +99,45 @@ func getMetaTestRunner(key string, value any, out string, err error, newVal any)
}
}
}
func TestMetasJson(t *testing.T) {
ms := Metas{{"me", 41}, {"you", false}}
exp := `{"me":41,"you":false}`
o, err := json.Marshal(ms)
assert.NoError(t, err)
assert.JSONEq(t, exp, string(o))
}
func TestMetasJsonUnmarshal(t *testing.T) {
ms := Metas{}
in := `{"me":"cool","you":false}`
err := json.Unmarshal([]byte(in), &ms)
assert.NoError(t, err)
assert.Len(t, ms, 2)
assert.ElementsMatch(t, Metas{
{"me", "cool"},
{"you", false},
}, ms)
}
func TestMetasJsonError(t *testing.T) {
ms := Metas{}
in := "not json"
err := (&ms).UnmarshalJSON([]byte(in))
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])
}

71
models/metas.go Normal file
View file

@ -0,0 +1,71 @@
package models
import (
"encoding/json"
"time"
)
// A slice of Meta
type Metas []Meta
// 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" {
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
}
// Implements json.Marshaler
func (ms Metas) MarshalJSON() ([]byte, error) {
return json.Marshal(ms.Map())
}
// Implements json.Unmarshaler
func (ms *Metas) UnmarshalJSON(in []byte) error {
old := (*ms).Map()
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
}
// 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
}

View file

@ -24,11 +24,7 @@ func ParseString(in string) any {
if null.MatchString(s) {
return nil
} else if yesno.MatchString(s) {
if yes.MatchString(s) {
return true
} else {
return false
}
return yes.MatchString(s)
} else if i, err := strconv.Atoi(s); err == nil {
return i
} else if f, err := strconv.ParseFloat(s, 64); err == nil {

View file

@ -1,6 +1,7 @@
package tools
import (
"strconv"
"time"
dp "github.com/markusmobius/go-dateparser"
@ -23,6 +24,19 @@ func ParseDate(in string) (t time.Time, err error) {
return MaxTime, nil
}
var er error
for _, format := range []string{time.RFC3339, DateFormat} {
if t, er = time.ParseInLocation(format, in, nil); er == nil {
return
}
}
var ts int64
if ts, er = strconv.ParseInt(in, 10, 0); er == nil {
t = time.Unix(ts, 0)
return
}
conf := dp.Configuration{
CurrentTime: time.Now().Local(),
ReturnTimeAsPeriod: true,
@ -32,12 +46,11 @@ func ParseDate(in string) (t time.Time, err error) {
d, err := dp.Parse(&conf, in)
t = d.Time
if err != nil {
d, err = dp.Parse(&conf, in, DateFormat)
t = d.Time
return
}
y, mon, day, h, loc := t.Year(), t.Month(), t.Day(), t.Hour(), t.Location()
switch d.Period {
case date.Second:
t = t.Truncate(time.Second)

View file

@ -16,14 +16,14 @@ func TestParseDate(t *testing.T) {
y, mon, d, h, loc := now.Year(), now.Month(), now.Day(), now.Hour(), now.Location()
sec := now.Truncate(time.Second)
today := time.Date(y, mon, d, 0, 0, 0, 0, loc)
tomorrow := today.Add(day)
yesterday := today.Add(-day)
tomorrow := time.Date(y, mon, d+1, 0, 0, 0, 0, loc)
yesterday := time.Date(y, mon, d-1, 0, 0, 0, 0, loc)
twoMin := now.Add(2 * time.Minute).Truncate(time.Minute)
twoHour := time.Date(y, mon, d, h+2, 0, 0, 0, loc)
firstMonth := time.Date(y, mon, 1, 0, 0, 0, 0, loc)
firstYear := time.Date(y, 1, 1, 0, 0, 0, 0, loc)
exact := "2075-02-12T12:13:54.536-02:00"
exactd, _ := time.ParseInLocation(time.RFC3339, exact, time.FixedZone("UTC-02:00", -7200))
exactd, _ := time.ParseInLocation(time.RFC3339, exact, nil)
var ts int64 = 1708876012
tsd := time.Unix(ts, 0)
ent := "February 25, 2024 at 04:00:13AM +0230"

View file

@ -14,6 +14,18 @@ func WriteValue(buff *bytes.Buffer, val any) (n int, err error) {
err = fmt.Errorf("Unsupported type %T", v)
case nil:
return
case []any:
var o []byte
o, err = json.Marshal(v)
if err == nil {
return buff.Write(o)
}
case map[string]any:
var o []byte
o, err = json.Marshal(v)
if err == nil {
return buff.Write(o)
}
case string:
return buff.WriteString(v)
case int:

View file

@ -30,6 +30,8 @@ func TestWriteBuffer(t *testing.T) {
{"json.Number", json.Number("42.13"), "42.13", nil},
{"json.RawMessage", json.RawMessage("{}"), "{}", nil},
{"time", when, when.Format(time.RFC3339), nil},
{"slice", []any{1, 2, "foo"}, `[1,2,"foo"]`, nil},
{"map", map[string]any{"baz": 42, "foo": "bar"}, `{"baz":42,"foo":"bar"}`, nil},
{"struct", struct{}{}, "", errors.New("Unsupported type struct {}")},
}