commit ec372e4809a951df35705af98da721c8689ee4da (tree)
parent 2f0d0b6fbe596f2f5a93e95b0c7f265491224bbc
Author: Motiejus Jakštys <desired.mta@gmail.com>
Date: Fri, 5 Jun 2020 09:34:26 +0300
parse + tests
Diffstat:
5 files changed, 112 insertions(+), 3 deletions(-)
diff --git a/src/joplin2site/go.mod b/src/joplin2site/go.mod
@@ -5,4 +5,5 @@ go 1.13
require (
github.com/jessevdk/go-flags v1.4.0
github.com/stretchr/testify v1.6.0
+ gopkg.in/yaml.v2 v2.3.0
)
diff --git a/src/joplin2site/go.sum b/src/joplin2site/go.sum
@@ -10,5 +10,7 @@ github.com/stretchr/testify v1.6.0 h1:jlIyCplCJFULU/01vCkhKuTyc3OorI3bJFuw6obfgh
github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
+gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/src/joplin2site/internal/conv/parse.go b/src/joplin2site/internal/conv/parse.go
@@ -0,0 +1,32 @@
+package conv
+
+import (
+ "strings"
+
+ "github.com/motiejus/dotfiles/joplin2site/internal/types"
+ "gopkg.in/yaml.v2"
+)
+
+func Parse(in string) (types.JoplinNote, error) {
+ var title, body, params string
+ titleIdx := strings.Index(in, "\n\n")
+ paramsIdx := strings.LastIndex(in, "\n\n")
+ title, body, params = in[0:titleIdx], in[min(titleIdx+2, paramsIdx):paramsIdx], in[paramsIdx:]
+
+ var note types.JoplinNote
+ if err := yaml.Unmarshal([]byte(params), ¬e); err != nil {
+ return types.JoplinNote{}, err
+ }
+
+ note.Title = title
+ note.Body = body
+
+ return note, nil
+}
+
+func min(a, b int) int {
+ if a < b {
+ return a
+ }
+ return b
+}
diff --git a/src/joplin2site/internal/conv/parse_test.go b/src/joplin2site/internal/conv/parse_test.go
@@ -0,0 +1,74 @@
+package conv
+
+import (
+ "testing"
+ "time"
+
+ "github.com/motiejus/dotfiles/joplin2site/internal/types"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestParse(t *testing.T) {
+ tests := []struct {
+ name string
+ note string
+ wantErr string
+ wantNote types.JoplinNote
+ }{
+ {
+ name: "ok, has body",
+ note: `Meta
+
+Fonts: https://news.ycombinator.com/item?id=23381513
+
+id: 4c7dd536ce1641afa4df349d87d9d29f
+parent_id: 9e651b478a5a43c196c31719300fee6e
+type_: 1
+`,
+ wantNote: types.JoplinNote{
+ ID: "4c7dd536ce1641afa4df349d87d9d29f",
+ Title: "Meta",
+ Body: "Fonts: https://news.ycombinator.com/item?id=23381513",
+ ParentID: "9e651b478a5a43c196c31719300fee6e",
+ Type: types.ItemTypeNote,
+ },
+ },
+ {
+ name: "ok folder",
+ note: `blog
+
+id: 9e651b478a5a43c196c31719300fee6e
+updated_time: 2020-06-04T16:07:19.930Z
+encryption_applied: 0
+type_: 2
+`,
+ wantNote: types.JoplinNote{
+ ID: "9e651b478a5a43c196c31719300fee6e",
+ Title: "blog",
+ Body: "",
+ UpdatedTime: time.Date(2020, 6, 4, 16, 07, 19, 930000000, time.UTC),
+ Type: types.ItemTypeFolder,
+ },
+ },
+ {
+ name: "bad yaml",
+ note: `blog
+
+bad yaml`,
+ wantErr: "yaml: unmarshal errors:\n line 3: cannot unmarshal !!str `bad yaml` into types.JoplinNote",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ note, err := Parse(tt.note)
+ if tt.wantErr != "" {
+ assert.EqualError(t, err, tt.wantErr)
+ return
+ }
+ require.NoError(t, err)
+ assert.Equal(t, tt.wantNote, note)
+ })
+ }
+}
diff --git a/src/joplin2site/internal/types/types.go b/src/joplin2site/internal/types/types.go
@@ -5,9 +5,9 @@ import "time"
// JoplinNote is how Joplin understands the note.
type JoplinNote struct {
ID string `yaml:"id"`
- ParentId string `yaml:"parent_id"`
- Title string `yaml:"title"`
- Body string `yaml:"body"`
+ ParentID string `yaml:"parent_id"`
+ Title string `yaml:"-"`
+ Body string `yaml:"-"`
CreatedTime time.Time `yaml:"created_time"`
UpdatedTime time.Time `yaml:"updated_time"`
IsConflict int `yaml:"is_conflict"`