diff options
Diffstat (limited to 'pkg/config')
-rw-r--r-- | pkg/config/yaml.go | 25 | ||||
-rw-r--r-- | pkg/config/yaml_test.go | 103 |
2 files changed, 80 insertions, 48 deletions
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) + } } }) } |