diff options
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | go.sum | 4 | ||||
-rw-r--r-- | pkg/config/yaml.go | 25 | ||||
-rw-r--r-- | pkg/config/yaml_test.go | 103 |
4 files changed, 85 insertions, 49 deletions
@@ -3,9 +3,9 @@ module github.com/Necoro/feed2imap-go go 1.14 require ( - github.com/davecgh/go-spew v1.1.1 github.com/emersion/go-imap v1.0.4 github.com/emersion/go-message v0.11.3-0.20200422153910-8c6ac6b57e3d + github.com/google/go-cmp v0.4.0 github.com/mmcdole/gofeed v1.0.0-beta2.0.20200331235650-4298e4366be3 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c ) @@ -15,6 +15,8 @@ github.com/emersion/go-sasl v0.0.0-20191210011802-430746ea8b9b h1:uhWtEWBHgop1rq github.com/emersion/go-sasl v0.0.0-20191210011802-430746ea8b9b/go.mod h1:G/dpzLu16WtQpBfQ/z3LYiYJn3ZhKSGWn83fyoyQe/k= github.com/emersion/go-textwrapper v0.0.0-20160606182133-d0e65e56babe h1:40SWqY0zE3qCi6ZrtTf5OUdNm5lDnGnjRSq9GgmeTrg= github.com/emersion/go-textwrapper v0.0.0-20160606182133-d0e65e56babe/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U= +github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/martinlindhe/base36 v1.0.0 h1:eYsumTah144C0A8P1T/AVSUk5ZoLnhfYFM3OGQxB52A= github.com/martinlindhe/base36 v1.0.0/go.mod h1:+AtEs8xrBpCeYgSLoY/aJ6Wf37jtBuR0s35750M27+8= github.com/mmcdole/gofeed v1.0.0-beta2.0.20200331235650-4298e4366be3 h1:Wy+ed15cpwtLcJYNiO4Z0wmjZHpNj4q0RsGbsoxWSMA= @@ -36,6 +38,8 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 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.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= diff --git a/pkg/config/yaml.go b/pkg/config/yaml.go index 84f4af6..6d48099 100644 --- a/pkg/config/yaml.go +++ b/pkg/config/yaml.go @@ -6,6 +6,12 @@ import ( "gopkg.in/yaml.v3" ) +const ( + strTag = "!!str" + nullTag = "!!null" + emptyTag = "" +) + type config struct { *Config `yaml:",inline"` GlobalConfig Map `yaml:",inline"` // need to be duplicated, because the Map in Config is not filled @@ -18,7 +24,7 @@ type group struct { } type configGroupFeed struct { - Target *string + Target yaml.Node Feed Feed `yaml:",inline"` Group group `yaml:",inline"` } @@ -32,9 +38,18 @@ func (grpFeed *configGroupFeed) isFeed() bool { } func (grpFeed *configGroupFeed) target() string { - if grpFeed.Target != nil { - return *grpFeed.Target + tag := grpFeed.Target.ShortTag() + switch tag { + case strTag: + return grpFeed.Target.Value + case nullTag: + return "" + case emptyTag: + // tag not set: continue on + default: + panic("unexpected tag " + tag + " for target node") } + if grpFeed.Feed.Name != "" { return grpFeed.Feed.Name } @@ -89,13 +104,13 @@ func appTarget(target []string, app string) []string { } } -// Fetch the group structure and populate the `Target` fields in the feeds +// Fetch the group structure and populate the `targetStr` fields in the feeds func buildFeeds(cfg []configGroupFeed, target []string, feeds Feeds) error { for _, f := range cfg { target := appTarget(target, f.target()) switch { case f.isFeed() && f.isGroup(): - return fmt.Errorf("Entry with Target %s is both a Feed and a group", target) + return fmt.Errorf("Entry with targetStr %s is both a Feed and a group", target) case f.isFeed(): feedCopy := f.Feed diff --git a/pkg/config/yaml_test.go b/pkg/config/yaml_test.go index 40ee325..575927f 100644 --- a/pkg/config/yaml_test.go +++ b/pkg/config/yaml_test.go @@ -1,22 +1,25 @@ package config import ( - "reflect" "strings" "testing" - "github.com/davecgh/go-spew/spew" + "github.com/google/go-cmp/cmp" + "gopkg.in/yaml.v3" ) -func i(i int) *int { return &i } -func s(s string) *string { return &s } -func b(b bool) *bool { return &b } +func i(i int) *int { return &i } +func b(b bool) *bool { return &b } func t(s string) []string { if s == "" { return []string{} } return strings.Split(s, ".") } +func n(s string) (n yaml.Node) { + n.SetString(s) + return +} func TestBuildFeeds(tst *testing.T) { tests := []struct { @@ -29,45 +32,51 @@ func TestBuildFeeds(tst *testing.T) { {name: "Empty input", wantErr: false, target: "", feeds: nil, result: Feeds{}}, {name: "Empty Feed", wantErr: true, target: "", feeds: []configGroupFeed{ - {Target: s("foo"), Feed: Feed{Url: "google.de"}}, + {Target: n("foo"), Feed: Feed{Url: "google.de"}}, }, result: Feeds{}}, {name: "Empty Feed", wantErr: true, target: "", feeds: []configGroupFeed{ - {Target: nil, Feed: Feed{Url: "google.de"}}, + {Feed: Feed{Url: "google.de"}}, }, result: Feeds{}}, {name: "Duplicate Feed Name", wantErr: true, target: "", feeds: []configGroupFeed{ - {Target: nil, Feed: Feed{Name: "Dup"}}, - {Target: nil, Feed: Feed{Name: "Dup"}}, + {Feed: Feed{Name: "Dup"}}, + {Feed: Feed{Name: "Dup"}}, }, result: Feeds{}}, {name: "Simple", wantErr: false, target: "", feeds: []configGroupFeed{ - {Target: s("foo"), Feed: Feed{Name: "muh"}}, + {Target: n("foo"), Feed: Feed{Name: "muh"}}, }, result: Feeds{"muh": &Feed{Name: "muh", Target: t("foo")}}, }, {name: "Simple With Target", wantErr: false, target: "moep", feeds: []configGroupFeed{ - {Target: s("foo"), Feed: Feed{Name: "muh"}}, + {Target: n("foo"), Feed: Feed{Name: "muh"}}, }, result: Feeds{"muh": &Feed{Name: "muh", Target: t("moep.foo")}}, }, - {name: "Simple With Nil Target", wantErr: false, target: "moep", + {name: "Simple Without Target", wantErr: false, target: "moep", feeds: []configGroupFeed{ - {Target: nil, Feed: Feed{Name: "muh"}}, + {Feed: Feed{Name: "muh"}}, }, result: Feeds{"muh": &Feed{Name: "muh", Target: t("moep.muh")}}, }, + {name: "Simple With Nil Target", wantErr: false, target: "moep", + feeds: []configGroupFeed{ + {Target: yaml.Node{Tag: "!!null"}, Feed: Feed{Name: "muh"}}, + }, + result: Feeds{"muh": &Feed{Name: "muh", Target: t("moep")}}, + }, {name: "Simple With Empty Target", wantErr: false, target: "moep", feeds: []configGroupFeed{ - {Target: s(""), Feed: Feed{Name: "muh"}}, + {Target: n(""), Feed: Feed{Name: "muh"}}, }, result: Feeds{"muh": &Feed{Name: "muh", Target: t("moep")}}, }, {name: "Multiple Feeds", wantErr: false, target: "moep", feeds: []configGroupFeed{ - {Target: s("foo"), Feed: Feed{Name: "muh"}}, - {Target: nil, Feed: Feed{Name: "bar"}}, + {Target: n("foo"), Feed: Feed{Name: "muh"}}, + {Feed: Feed{Name: "bar"}}, }, result: Feeds{ "muh": &Feed{Name: "muh", Target: t("moep.foo")}, @@ -76,16 +85,16 @@ func TestBuildFeeds(tst *testing.T) { }, {name: "Empty Group", wantErr: false, target: "", feeds: []configGroupFeed{ - {Target: nil, Group: group{Group: "G1"}}, + {Group: group{Group: "G1"}}, }, result: Feeds{}, }, {name: "Simple Group", wantErr: false, target: "", feeds: []configGroupFeed{ - {Target: nil, Group: group{Group: "G1", Feeds: []configGroupFeed{ - {Target: s("bar"), Feed: Feed{Name: "F1"}}, - {Target: s(""), Feed: Feed{Name: "F2"}}, - {Target: nil, Feed: Feed{Name: "F3"}}, + {Group: group{Group: "G1", Feeds: []configGroupFeed{ + {Target: n("bar"), Feed: Feed{Name: "F1"}}, + {Target: n(""), Feed: Feed{Name: "F2"}}, + {Feed: Feed{Name: "F3"}}, }}}, }, result: Feeds{ @@ -96,14 +105,14 @@ func TestBuildFeeds(tst *testing.T) { }, {name: "Nested Groups", wantErr: false, target: "", feeds: []configGroupFeed{ - {Target: nil, Group: group{Group: "G1", Feeds: []configGroupFeed{ - {Target: nil, Feed: Feed{Name: "F0"}}, - {Target: s("bar"), Group: group{Group: "G2", - Feeds: []configGroupFeed{{Target: nil, Feed: Feed{Name: "F1"}}}}}, - {Target: s(""), Group: group{Group: "G3", - Feeds: []configGroupFeed{{Target: s("baz"), Feed: Feed{Name: "F2"}}}}}, - {Target: nil, Group: group{Group: "G4", - Feeds: []configGroupFeed{{Target: nil, Feed: Feed{Name: "F3"}}}}}, + {Group: group{Group: "G1", Feeds: []configGroupFeed{ + {Feed: Feed{Name: "F0"}}, + {Target: n("bar"), Group: group{Group: "G2", + Feeds: []configGroupFeed{{Feed: Feed{Name: "F1"}}}}}, + {Target: n(""), Group: group{Group: "G3", + Feeds: []configGroupFeed{{Target: n("baz"), Feed: Feed{Name: "F2"}}}}}, + {Group: group{Group: "G4", + Feeds: []configGroupFeed{{Feed: Feed{Name: "F3"}}}}}, }}}, }, result: Feeds{ @@ -122,8 +131,8 @@ func TestBuildFeeds(tst *testing.T) { tst.Errorf("buildFeeds() error = %v, wantErr %v", err, tt.wantErr) return } - if !tt.wantErr && !reflect.DeepEqual(feeds, tt.result) { - tst.Errorf("buildFeeds() got: %s\nwant: %s", spew.Sdump(feeds), spew.Sdump(tt.result)) + if diff := cmp.Diff(feeds, tt.result); !tt.wantErr && diff != "" { + tst.Error(diff) } }) } @@ -180,7 +189,7 @@ feeds: `, wantErr: false, config: defaultConfig([]configGroupFeed{ - {Target: s("bar"), Feed: Feed{ + {Target: n("bar"), Feed: Feed{ Name: "Foo", Url: "whatever", Options: Options{ @@ -202,7 +211,7 @@ feeds: `, wantErr: false, config: defaultConfig([]configGroupFeed{ - {Target: nil, Feed: Feed{ + {Feed: Feed{ Name: "Foo", Url: "whatever", Options: Options{ @@ -210,7 +219,7 @@ feeds: InclImages: nil, }, }}, - {Target: s("bla"), Feed: Feed{ + {Target: n("bla"), Feed: Feed{ Name: "Shrubbery", Url: "google.de", Options: Options{ @@ -227,7 +236,7 @@ feeds: target: bla `, wantErr: false, - config: defaultConfig([]configGroupFeed{{Target: s("bla"), Group: group{"Foo", nil}}}, nil), + config: defaultConfig([]configGroupFeed{{Target: n("bla"), Group: group{"Foo", nil}}}, nil), }, {name: "Feeds and Groups", inp: ` @@ -247,27 +256,31 @@ feeds: `, wantErr: false, config: defaultConfig([]configGroupFeed{ - {Target: nil, Feed: Feed{ + {Feed: Feed{ Name: "Foo", Url: "whatever", }}, - {Target: s("target"), Group: group{ + {Target: n("target"), Group: group{ Group: "G1", Feeds: []configGroupFeed{ - {Target: s(""), Group: group{ + {Target: n(""), Group: group{ Group: "G2", Feeds: []configGroupFeed{ - {Target: nil, Feed: Feed{Name: "F1", Url: "google.de"}}, + {Feed: Feed{Name: "F1", Url: "google.de"}}, }}, }, - {Target: nil, Feed: Feed{Name: "F2"}}, - {Target: nil, Group: group{Group: "G3"}}, + {Feed: Feed{Name: "F2"}}, + {Group: group{Group: "G3"}}, }}, }, }, nil), }, } + eqNode := cmp.Comparer(func(l, r yaml.Node) bool { + return l.Tag == r.Tag && l.Value == r.Value + }) + for _, tt := range tests { tst.Run(tt.name, func(tst *testing.T) { var buf = []byte(tt.inp) @@ -276,8 +289,12 @@ feeds: tst.Errorf("parse() error = %v, wantErr %v", err, tt.wantErr) return } - if err == nil && !reflect.DeepEqual(got, tt.config) { - tst.Errorf("parse() got: %s\nwant: %s", spew.Sdump(got), spew.Sdump(tt.config)) + + if err == nil { + diff := cmp.Diff(got, tt.config, eqNode) + if diff != "" { + tst.Error(diff) + } } }) } |