From 2c389036f956c17364ecd1ce0071880cb7d3ea98 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Fri, 16 Oct 2020 00:33:48 +0900 Subject: [PATCH] refactor: use the library suzuki-shunsuke/go-convmap --- go.mod | 1 + go.sum | 2 ++ pkg/config/config.go | 36 ++++-------------------------------- 3 files changed, 7 insertions(+), 32 deletions(-) diff --git a/go.mod b/go.mod index 7cc2f2f..ee04e22 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/d5/tengo/v2 v2.6.2 github.com/google/go-cmp v0.5.2 github.com/sirupsen/logrus v1.7.0 + github.com/suzuki-shunsuke/go-convmap v0.1.0 github.com/suzuki-shunsuke/go-findconfig v0.1.0 github.com/urfave/cli/v2 v2.2.0 gopkg.in/yaml.v2 v2.3.0 diff --git a/go.sum b/go.sum index f125b01..3399238 100644 --- a/go.sum +++ b/go.sum @@ -15,6 +15,8 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/suzuki-shunsuke/go-convmap v0.1.0 h1:0qzSC9IAzYdylNJvgzFpxbD/KeWpuVM608d/S0VjYsU= +github.com/suzuki-shunsuke/go-convmap v0.1.0/go.mod h1:S0yDqBPFU0lmlHhoMKNSSHDRt87tUyI+KY/fBuXNn14= github.com/suzuki-shunsuke/go-findconfig v0.1.0 h1:ORAMPULNwus1kIvhcHaubo0Pw1ehlgRbSAxmAJL7KI4= github.com/suzuki-shunsuke/go-findconfig v0.1.0/go.mod h1:u/0Zz6/GDE6G0gofzVhR9UPOIKLSUoDMjUoFWqOoVlg= github.com/urfave/cli v1.22.4 h1:u7tSpNPPswAFymm8IehJhy4uJMlUuU/GmqSkvJ1InXA= diff --git a/pkg/config/config.go b/pkg/config/config.go index 910ccb7..c210dbb 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -2,6 +2,8 @@ package config import ( "fmt" + + "github.com/suzuki-shunsuke/go-convmap/convmap" ) type Config struct { @@ -29,14 +31,14 @@ type Test struct { func ConvertConfig(cfg Config) error { for i, entry := range cfg.Entries { for k, v := range entry.Params { - p, err := ConvertMapKey(v) + p, err := convmap.Convert(v) if err != nil { return fmt.Errorf("convert entry.params: entry_name: %s: key: %s: %w", entry.Name, k, err) } entry.Params[k] = p } for j, test := range entry.Tests { - p, err := ConvertMapKey(test.Equal) + p, err := convmap.Convert(test.Equal) if err != nil { return fmt.Errorf("convert test.Equal: entry_name: %s: test_name: %s: %w", entry.Name, test.Name, err) } @@ -47,33 +49,3 @@ func ConvertConfig(cfg Config) error { } return nil } - -func ConvertMapKey(data interface{}) (interface{}, error) { - switch t := data.(type) { - case map[interface{}]interface{}: - m := make(map[string]interface{}, len(t)) - for k, v := range t { - s, ok := k.(string) - if !ok { - return nil, fmt.Errorf("the map key should be string: %+v", k) - } - val, err := ConvertMapKey(v) - if err != nil { - return nil, fmt.Errorf("key: %s: %w", s, err) - } - m[s] = val - } - return m, nil - case []interface{}: - for i, v := range t { - val, err := ConvertMapKey(v) - if err != nil { - return nil, fmt.Errorf("index: %d: %w", i, err) - } - t[i] = val - } - return t, nil - default: - return data, nil - } -}