Skip to content
This repository has been archived by the owner on Sep 29, 2021. It is now read-only.

Commit

Permalink
Merge pull request #11 from suzuki-shunsuke/refactor/use-go-convmap
Browse files Browse the repository at this point in the history
refactor: use the library suzuki-shunsuke/go-convmap
  • Loading branch information
suzuki-shunsuke authored Oct 15, 2020
2 parents fcba815 + 2c38903 commit ca24af0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 32 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
36 changes: 4 additions & 32 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package config

import (
"fmt"

"github.com/suzuki-shunsuke/go-convmap/convmap"
)

type Config struct {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
}

0 comments on commit ca24af0

Please sign in to comment.