Added sub objects support in JSON resolver (#178)
This commit is contained in:
+13
-2
@@ -39,8 +39,19 @@ func JSON(r io.Reader) (Resolver, error) {
|
||||
var f ResolverFunc = func(context *Context, parent *Path, flag *Flag) (interface{}, error) {
|
||||
name := strings.ReplaceAll(flag.Name, "-", "_")
|
||||
raw, ok := values[name]
|
||||
if !ok {
|
||||
return nil, nil
|
||||
if ok {
|
||||
return raw, nil
|
||||
}
|
||||
raw = values
|
||||
for _, part := range strings.Split(name, ".") {
|
||||
if values, ok := raw.(map[string]interface{}); ok {
|
||||
raw, ok = values[part]
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
} else {
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
return raw, nil
|
||||
}
|
||||
|
||||
+14
-1
@@ -147,18 +147,29 @@ func TestEnv(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestJSONBasic(t *testing.T) {
|
||||
type Embed struct {
|
||||
String string
|
||||
}
|
||||
|
||||
var cli struct {
|
||||
String string
|
||||
Slice []int
|
||||
SliceWithCommas []string
|
||||
Bool bool
|
||||
|
||||
One Embed `prefix:"one." embed:""`
|
||||
Two Embed `prefix:"two." embed:""`
|
||||
}
|
||||
|
||||
json := `{
|
||||
"string": "🍕",
|
||||
"slice": [5, 8],
|
||||
"bool": true,
|
||||
"slice_with_commas": ["a,b", "c"]
|
||||
"slice_with_commas": ["a,b", "c"],
|
||||
"one":{
|
||||
"string": "one value"
|
||||
},
|
||||
"two.string": "two value"
|
||||
}`
|
||||
|
||||
r, err := kong.JSON(strings.NewReader(json))
|
||||
@@ -170,6 +181,8 @@ func TestJSONBasic(t *testing.T) {
|
||||
require.Equal(t, "🍕", cli.String)
|
||||
require.Equal(t, []int{5, 8}, cli.Slice)
|
||||
require.Equal(t, []string{"a,b", "c"}, cli.SliceWithCommas)
|
||||
require.Equal(t, "one value", cli.One.String)
|
||||
require.Equal(t, "two value", cli.Two.String)
|
||||
require.True(t, cli.Bool)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user