diff options
Diffstat (limited to '')
-rw-r--r-- | internal/config/config.go | 26 | ||||
-rw-r--r-- | internal/config/yaml.go | 7 | ||||
-rw-r--r-- | internal/config/yaml_test.go | 111 |
3 files changed, 141 insertions, 3 deletions
diff --git a/internal/config/config.go b/internal/config/config.go index 361246e..e330a48 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -3,11 +3,37 @@ package config import ( "fmt" "io/ioutil" + "strings" ) type Map map[string]interface{} type Feeds map[string]*Feed +func (f Feeds) String() string { + var b strings.Builder + app := func(a ...interface{}) { + _, _ = fmt.Fprint(&b, a...) + } + app("Feeds [") + + first := true + for k, v := range f { + if !first { + app(", ") + } + app(`"`, k, `"`, ": ") + if v == nil { + app("<nil>") + } else { + _, _ = fmt.Fprintf(&b, "%+v", *v) + } + first = false + } + app("]") + + return b.String() +} + type Config struct { GlobalConfig Map Feeds Feeds diff --git a/internal/config/yaml.go b/internal/config/yaml.go index 156737d..2f507cc 100644 --- a/internal/config/yaml.go +++ b/internal/config/yaml.go @@ -27,7 +27,7 @@ func (grpFeed *configGroupFeed) isGroup() bool { } func (grpFeed *configGroupFeed) isFeed() bool { - return grpFeed.Name != "" + return grpFeed.Feed != Feed{} } func (grpFeed *configGroupFeed) target() string { @@ -60,12 +60,13 @@ func appTarget(target, app string) string { return target } - return target + "/" + app + return target + "." + app } // Parse the group structure and populate the `Target` fields in the feeds func buildFeeds(cfg []configGroupFeed, target string, feeds Feeds) error { - for _, f := range cfg { + for idx := range cfg { + f := &cfg[idx] // cannot use `_, f := range cfg` as it returns copies(!), but we need the originals target := appTarget(target, f.target()) switch { case f.isFeed() && f.isGroup(): diff --git a/internal/config/yaml_test.go b/internal/config/yaml_test.go index f5a7b03..94566c6 100644 --- a/internal/config/yaml_test.go +++ b/internal/config/yaml_test.go @@ -8,6 +8,117 @@ import ( func s(s string) *string { return &s } func b(b bool) *bool { return &b } +func TestBuildFeeds(t *testing.T) { + tests := []struct { + name string + wantErr bool + target string + feeds []configGroupFeed + result Feeds + }{ + {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"}}, + }, result: Feeds{}}, + {name: "Empty Feed", wantErr: true, target: "", + feeds: []configGroupFeed{ + {Target: nil, 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"}}, + }, result: Feeds{}}, + {name: "Simple", wantErr: false, target: "", + feeds: []configGroupFeed{ + {Target: s("foo"), Feed: Feed{Name: "muh"}}, + }, + result: Feeds{"muh": &Feed{Name: "muh", Target: "foo"}}, + }, + {name: "Simple With Target", wantErr: false, target: "moep", + feeds: []configGroupFeed{ + {Target: s("foo"), Feed: Feed{Name: "muh"}}, + }, + result: Feeds{"muh": &Feed{Name: "muh", Target: "moep.foo"}}, + }, + {name: "Simple With Nil Target", wantErr: false, target: "moep", + feeds: []configGroupFeed{ + {Target: nil, Feed: Feed{Name: "muh"}}, + }, + result: Feeds{"muh": &Feed{Name: "muh", Target: "moep.muh"}}, + }, + {name: "Simple With Empty Target", wantErr: false, target: "moep", + feeds: []configGroupFeed{ + {Target: s(""), Feed: Feed{Name: "muh"}}, + }, + result: Feeds{"muh": &Feed{Name: "muh", Target: "moep"}}, + }, + {name: "Multiple Feeds", wantErr: false, target: "moep", + feeds: []configGroupFeed{ + {Target: s("foo"), Feed: Feed{Name: "muh"}}, + {Target: nil, Feed: Feed{Name: "bar"}}, + }, + result: Feeds{ + "muh": &Feed{Name: "muh", Target: "moep.foo"}, + "bar": &Feed{Name: "bar", Target: "moep.bar"}, + }, + }, + {name: "Empty Group", wantErr: false, target: "", + feeds: []configGroupFeed{ + {Target: nil, 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"}}, + }}}, + }, + result: Feeds{ + "F1": &Feed{Name: "F1", Target: "G1.bar"}, + "F2": &Feed{Name: "F2", Target: "G1"}, + "F3": &Feed{Name: "F3", Target: "G1.F3"}, + }, + }, + {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"}}}}}, + }}}, + }, + result: Feeds{ + "F0": &Feed{Name: "F0", Target: "G1.F0"}, + "F1": &Feed{Name: "F1", Target: "G1.bar.F1"}, + "F2": &Feed{Name: "F2", Target: "G1.baz"}, + "F3": &Feed{Name: "F3", Target: "G1.G4.F3"}, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var feeds Feeds = Feeds{} + err := buildFeeds(tt.feeds, tt.target, feeds) + if (err != nil) != tt.wantErr { + t.Errorf("buildFeeds() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !tt.wantErr && !reflect.DeepEqual(feeds, tt.result) { + t.Errorf("buildFeeds() got = %v, want %v", feeds, tt.result) + } + }) + } +} + //noinspection GoNilness,GoNilness func TestParse(t *testing.T) { tests := []struct { |