diff options
Diffstat (limited to '')
-rw-r--r-- | go.mod | 1 | ||||
-rw-r--r-- | go.sum | 2 | ||||
-rw-r--r-- | pkg/config/yaml.go | 56 | ||||
-rw-r--r-- | pkg/config/yaml_test.go | 10 |
4 files changed, 28 insertions, 41 deletions
@@ -14,6 +14,7 @@ require ( github.com/gabriel-vasile/mimetype v1.4.0 github.com/google/go-cmp v0.5.6 github.com/google/uuid v1.3.0 + github.com/mitchellh/mapstructure v1.4.3 github.com/nightlyone/lockfile v1.0.0 golang.org/x/net v0.0.0-20210916014120-12bc252f5db8 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b @@ -47,6 +47,8 @@ github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzp github.com/mattn/go-runewidth v0.0.8/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs= +github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf h1:sWGE2v+hO0Nd4yFU/S/mDBM5plIU8v/Qhfz41hkDIAI= github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf/go.mod h1:pasqhqstspkosTneA62Nc+2p9SOBBYAPbnmRRWPQ0V8= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= diff --git a/pkg/config/yaml.go b/pkg/config/yaml.go index a80c80d..3050825 100644 --- a/pkg/config/yaml.go +++ b/pkg/config/yaml.go @@ -4,9 +4,9 @@ import ( "errors" "fmt" "io" - "reflect" "strings" + "github.com/mitchellh/mapstructure" "gopkg.in/yaml.v3" "github.com/Necoro/feed2imap-go/pkg/log" @@ -139,50 +139,34 @@ func appTarget(target []string, app string) []string { } } -func buildOptions(globalFeedOptions *Options, options Map) (feedOptions Options, unknownFields []string) { +func buildOptions(globalFeedOptions *Options, options Map) (Options, []string) { + // copy global as default + feedOptions := *globalFeedOptions + if options == nil { // no options set for the feed: copy global options and be done - return *globalFeedOptions, unknownFields + return feedOptions, []string{} } - fv := reflect.ValueOf(&feedOptions).Elem() - gv := reflect.ValueOf(globalFeedOptions).Elem() - - n := gv.NumField() - for i := 0; i < n; i++ { - field := fv.Field(i) - f := fv.Type().Field(i) - - if f.PkgPath != "" && !f.Anonymous { - continue - } - - tag := f.Tag.Get("yaml") - if tag == "" { - continue - } - - name := strings.Split(tag, ",")[0] + var md mapstructure.Metadata + mapstructureConfig := mapstructure.DecoderConfig{ + TagName: "yaml", + Metadata: &md, + Result: &feedOptions, + } - set, ok := options[name] - if ok { // in the map -> copy and delete - value := reflect.ValueOf(set) - if !value.Type().AssignableTo(field.Type()) { - value = value.Convert(field.Type()) - } - field.Set(value) - delete(options, name) - } else { // not in the map -> copy from global - field.Set(gv.Field(i)) - } + var err error + dec, err := mapstructure.NewDecoder(&mapstructureConfig) + if err != nil { + panic(err) } - // remaining fields are unknown - for k := range options { - unknownFields = append(unknownFields, k) + err = dec.Decode(options) + if err != nil { + panic(err) } - return feedOptions, unknownFields + return feedOptions, md.Unused } // Fetch the group structure and populate the `targetStr` fields in the feeds diff --git a/pkg/config/yaml_test.go b/pkg/config/yaml_test.go index b02c4c4..b422c38 100644 --- a/pkg/config/yaml_test.go +++ b/pkg/config/yaml_test.go @@ -30,12 +30,12 @@ func TestBuildOptions(tst *testing.T) { out Options unknowns []string }{ - {"Empty", nil, Options{}, Options{}, nil}, - {"Simple copy", nil, Options{MinFreq: 75}, Options{MinFreq: 75}, nil}, + {"Empty", nil, Options{}, Options{}, []string{}}, + {"Simple copy", nil, Options{MinFreq: 75}, Options{MinFreq: 75}, []string{}}, {"Unknowns", Map{"foo": 1}, Options{}, Options{}, []string{"foo"}}, - {"Override", Map{"include-images": true}, Options{InclImages: false}, Options{InclImages: true}, nil}, - {"Non-Standard Type", Map{"body": "both"}, Options{}, Options{Body: "both"}, nil}, - {"Mixed", Map{"min-frequency": 24}, Options{MinFreq: 6, InclImages: true}, Options{MinFreq: 24, InclImages: true}, nil}, + {"Override", Map{"include-images": true}, Options{InclImages: false}, Options{InclImages: true}, []string{}}, + {"Non-Standard Type", Map{"body": "both"}, Options{}, Options{Body: "both"}, []string{}}, + {"Mixed", Map{"min-frequency": 24}, Options{MinFreq: 6, InclImages: true}, Options{MinFreq: 24, InclImages: true}, []string{}}, {"All", Map{"max-frequency": 12, "include-images": true, "ignore-hash": true, "obsolete": 54}, Options{MinFreq: 6, InclImages: true, IgnHash: false}, |