-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: set slice / nested maps with test cases
- Loading branch information
Showing
7 changed files
with
164 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,4 @@ | ||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= | ||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pelletier/go-toml v1.7.0 h1:7utD74fnzVc/cpcyy8sjrlFr5vYpypUixARcHIMIGuI= | ||
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= | ||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= | ||
gopkg.in/yaml.v3 v3.0.0-20200506231410-2ff61e1afc86 h1:OfFoIUYv/me30yv7XlMy4F9RJw8DEm8WQ6QG1Ph4bH0= | ||
gopkg.in/yaml.v3 v3.0.0-20200506231410-2ff61e1afc86/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= | ||
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package gofig | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestParse(t *testing.T) { | ||
type Config struct { | ||
String string `gofig:"string"` | ||
Slice []int `gofig:"slice"` | ||
Map map[string]string `gofig:"map"` | ||
NestedMap map[string]map[string][]int `gofig:"nestedmap"` | ||
DeeplyNestedMap map[string]map[string]map[string][]int `gofig:"deeplynestedmap"` | ||
} | ||
|
||
cases := map[string]struct { | ||
parser *InMemoryParser | ||
want Config | ||
}{ | ||
"String": { | ||
parser: func() *InMemoryParser { | ||
p := NewInMemoryParser() | ||
p.Add("string", "bar") | ||
|
||
return p | ||
}(), | ||
want: Config{ | ||
String: "bar", | ||
}, | ||
}, | ||
"Slice": { | ||
parser: func() *InMemoryParser { | ||
p := NewInMemoryParser() | ||
p.Add("slice", []int{1, 2, 3}) | ||
|
||
return p | ||
}(), | ||
want: Config{ | ||
Slice: []int{1, 2, 3}, | ||
}, | ||
}, | ||
"Map": { | ||
parser: func() *InMemoryParser { | ||
p := NewInMemoryParser() | ||
p.Add("map.key", "value") | ||
|
||
return p | ||
}(), | ||
want: Config{ | ||
Map: map[string]string{ | ||
"key": "value", | ||
}, | ||
}, | ||
}, | ||
"NestedMap": { | ||
parser: func() *InMemoryParser { | ||
p := NewInMemoryParser() | ||
p.Add("nestedmap.foo.bar", []int{1, 2, 3}) | ||
p.Add("nestedmap.foo.baz", []int{4, 5, 6}) | ||
|
||
return p | ||
}(), | ||
want: Config{ | ||
NestedMap: map[string]map[string][]int{ | ||
"foo": { | ||
"bar": {1, 2, 3}, | ||
"baz": {4, 5, 6}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"DeeplyNestedMap": { | ||
parser: func() *InMemoryParser { | ||
p := NewInMemoryParser() | ||
p.Add("deeplynestedmap.foo.bar.fizz", []int{1, 2, 3}) | ||
p.Add("deeplynestedmap.foo.bar.buzz", []int{4, 5, 6}) | ||
p.Add("deeplynestedmap.foo.baz.fizz", []int{1, 2, 3}) | ||
p.Add("deeplynestedmap.foo.baz.buzz", []int{4, 5, 6}) | ||
|
||
return p | ||
}(), | ||
want: Config{ | ||
DeeplyNestedMap: map[string]map[string]map[string][]int{ | ||
"foo": { | ||
"bar": { | ||
"fizz": {1, 2, 3}, | ||
"buzz": {4, 5, 6}, | ||
}, | ||
"baz": { | ||
"fizz": {1, 2, 3}, | ||
"buzz": {4, 5, 6}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for name, testCase := range cases { | ||
tc := testCase | ||
|
||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
var cfg Config | ||
|
||
g, err := New(&cfg, WithDebug(), SetLogger(LoggerFunc(func(v ...interface{}) { | ||
t.Log(v...) | ||
}))) | ||
|
||
if err != nil { | ||
t.Fatal("want nil error, got:", err) | ||
} | ||
|
||
if err := g.Parse(tc.parser); err != nil { | ||
t.Fatal("want nil error, got:", err) | ||
} | ||
|
||
if !cmp.Equal(tc.want, cfg) { | ||
t.Errorf("want %+v, got %+v", tc.want, cfg) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters