diff options
Diffstat (limited to 'internal')
-rw-r--r-- | internal/config/config.go | 4 | ||||
-rw-r--r-- | internal/config/yaml.go | 21 | ||||
-rw-r--r-- | internal/config/yaml_test.go | 55 |
3 files changed, 44 insertions, 36 deletions
diff --git a/internal/config/config.go b/internal/config/config.go index e330a48..fa14315 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -41,7 +41,7 @@ type Config struct { type Feed struct { Name string - Target string `yaml:"-"` + Target []string `yaml:"-"` Url string MinFreq int `yaml:"min-frequency"` InclImages *bool `yaml:"include-images"` @@ -65,7 +65,7 @@ func Load(path string) (Config, error) { Feeds: make(Feeds), } - if err := buildFeeds(parsedCfg.Feeds, "", finishedCfg.Feeds); err != nil { + if err := buildFeeds(parsedCfg.Feeds, []string{}, finishedCfg.Feeds); err != nil { return finishedCfg, fmt.Errorf("while parsing: %w", err) } diff --git a/internal/config/yaml.go b/internal/config/yaml.go index 8c40107..80fb383 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.Feed != Feed{} + return grpFeed.Feed.Name != "" || grpFeed.Feed.Url != "" } func (grpFeed *configGroupFeed) target() string { @@ -51,20 +51,21 @@ func parse(buf []byte) (config, error) { return parsedCfg, nil } -func appTarget(target, app string) string { - if target == "" { - return app - } - - if app == "" { +func appTarget(target []string, app string) []string { + switch { + case len(target) == 0 && app == "": + return []string{} + case len(target) == 0: + return []string{app} + case app == "": return target + default: + return append(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 { +func buildFeeds(cfg []configGroupFeed, target []string, feeds Feeds) error { 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()) diff --git a/internal/config/yaml_test.go b/internal/config/yaml_test.go index 94566c6..1e77700 100644 --- a/internal/config/yaml_test.go +++ b/internal/config/yaml_test.go @@ -2,13 +2,20 @@ package config import ( "reflect" + "strings" "testing" ) func s(s string) *string { return &s } func b(b bool) *bool { return &b } +func t(s string) []string { + if s == "" { + return []string{} + } + return strings.Split(s, ".") +} -func TestBuildFeeds(t *testing.T) { +func TestBuildFeeds(tst *testing.T) { tests := []struct { name string wantErr bool @@ -34,25 +41,25 @@ func TestBuildFeeds(t *testing.T) { feeds: []configGroupFeed{ {Target: s("foo"), Feed: Feed{Name: "muh"}}, }, - result: Feeds{"muh": &Feed{Name: "muh", Target: "foo"}}, + 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"}}, }, - result: Feeds{"muh": &Feed{Name: "muh", Target: "moep.foo"}}, + result: Feeds{"muh": &Feed{Name: "muh", Target: t("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"}}, + result: Feeds{"muh": &Feed{Name: "muh", Target: t("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"}}, + result: Feeds{"muh": &Feed{Name: "muh", Target: t("moep")}}, }, {name: "Multiple Feeds", wantErr: false, target: "moep", feeds: []configGroupFeed{ @@ -60,8 +67,8 @@ func TestBuildFeeds(t *testing.T) { {Target: nil, Feed: Feed{Name: "bar"}}, }, result: Feeds{ - "muh": &Feed{Name: "muh", Target: "moep.foo"}, - "bar": &Feed{Name: "bar", Target: "moep.bar"}, + "muh": &Feed{Name: "muh", Target: t("moep.foo")}, + "bar": &Feed{Name: "bar", Target: t("moep.bar")}, }, }, {name: "Empty Group", wantErr: false, target: "", @@ -79,9 +86,9 @@ func TestBuildFeeds(t *testing.T) { }}}, }, result: Feeds{ - "F1": &Feed{Name: "F1", Target: "G1.bar"}, - "F2": &Feed{Name: "F2", Target: "G1"}, - "F3": &Feed{Name: "F3", Target: "G1.F3"}, + "F1": &Feed{Name: "F1", Target: t("G1.bar")}, + "F2": &Feed{Name: "F2", Target: t("G1")}, + "F3": &Feed{Name: "F3", Target: t("G1.F3")}, }, }, {name: "Nested Groups", wantErr: false, target: "", @@ -97,30 +104,30 @@ func TestBuildFeeds(t *testing.T) { }}}, }, 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"}, + "F0": &Feed{Name: "F0", Target: t("G1.F0")}, + "F1": &Feed{Name: "F1", Target: t("G1.bar.F1")}, + "F2": &Feed{Name: "F2", Target: t("G1.baz")}, + "F3": &Feed{Name: "F3", Target: t("G1.G4.F3")}, }, }, } for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + tst.Run(tt.name, func(tst *testing.T) { var feeds Feeds = Feeds{} - err := buildFeeds(tt.feeds, tt.target, feeds) + err := buildFeeds(tt.feeds, t(tt.target), feeds) if (err != nil) != tt.wantErr { - t.Errorf("buildFeeds() error = %v, wantErr %v", err, tt.wantErr) + tst.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) + tst.Errorf("buildFeeds() got = %v, want %v", feeds, tt.result) } }) } } //noinspection GoNilness,GoNilness -func TestParse(t *testing.T) { +func TestParse(tst *testing.T) { tests := []struct { name string inp string @@ -148,7 +155,7 @@ feeds: feeds: []configGroupFeed{ {Target: s("bar"), Feed: Feed{ Name: "Foo", - Target: "", + Target: nil, Url: "whatever", MinFreq: 0, InclImages: b(true), @@ -230,18 +237,18 @@ feeds: } for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + tst.Run(tt.name, func(tst *testing.T) { var buf = []byte(tt.inp) got, err := parse(buf) if (err != nil) != tt.wantErr { - t.Errorf("parse() error = %v, wantErr %v", err, tt.wantErr) + tst.Errorf("parse() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got.Feeds, tt.feeds) { - t.Errorf("parse() got = %v, want %v", got.Feeds, tt.feeds) + tst.Errorf("parse() got = %v, want %v", got.Feeds, tt.feeds) } if !reflect.DeepEqual(got.GlobalConfig, tt.globalConfig) { - t.Errorf("parse() got = %v, want %v", got.GlobalConfig, tt.globalConfig) + tst.Errorf("parse() got = %v, want %v", got.GlobalConfig, tt.globalConfig) } }) } |