Skip to content

Commit

Permalink
feat(schema.go): add support for params of type array (#253)
Browse files Browse the repository at this point in the history
Signed-off-by: Vaughn Dice <vadice@microsoft.com>
  • Loading branch information
vdice committed Jun 1, 2021
1 parent 215e8fc commit 20e825c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
8 changes: 7 additions & 1 deletion bundle/definition/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,14 @@ func (s *Schema) ConvertValue(val string) (interface{}, error) {
default:
return false, errors.Errorf("%q is not a valid boolean", val)
}
case "array":
var obj []interface{}
if err := json.Unmarshal([]byte(val), &obj); err != nil {
return nil, errors.Wrapf(err, "could not unmarshal value %v into a json array", val)
}
return obj, nil
case "object":
var obj interface{}
var obj map[string]interface{}
if err := json.Unmarshal([]byte(val), &obj); err != nil {
return nil, errors.Wrapf(err, "could not unmarshal value %v into a json object", val)
}
Expand Down
35 changes: 26 additions & 9 deletions bundle/definition/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,25 +322,42 @@ func TestConvertValue(t *testing.T) {
is.Error(err)

pd.Type = "array"
_, err = pd.ConvertValue("nope")
out, err = pd.ConvertValue(`"cookies"`)
is.Error(err)
is.Contains(err.Error(), "could not unmarshal")
is.Contains(err.Error(), "into a json array")
is.Equal(nil, out)

_, err = pd.ConvertValue("123")
out, err = pd.ConvertValue(`["chocolate" "chip" "cookies"]`)
is.Error(err)
is.Contains(err.Error(), "could not unmarshal")
is.Contains(err.Error(), "into a json array")
is.Equal(nil, out)

_, err = pd.ConvertValue("true")
is.Error(err)
out, err = pd.ConvertValue(`["chocolate", "chip", "cookies"]`)
is.NoError(err)
is.Equal([]interface{}{"chocolate", "chip", "cookies"}, out)

_, err = pd.ConvertValue("123.5")
pd.Type = "object"
out, err = pd.ConvertValue(`"object"`)
is.Error(err)
is.Contains(err.Error(), "could not unmarshal")
is.Contains(err.Error(), "into a json object")
is.Equal(nil, out)

pd.Type = "object"
out, err = pd.ConvertValue(`{"object": true}`)
is.NoError(err)
is.Equal(map[string]interface{}{"object": true}, out)
out, err = pd.ConvertValue(`"object": true`)
is.Error(err)
is.Contains(err.Error(), "could not unmarshal")
is.Contains(err.Error(), "into a json object")
is.Equal(nil, out)

out, err = pd.ConvertValue(`{"object" true}`)
is.Error(err)
is.Contains(err.Error(), "could not unmarshal")
is.Contains(err.Error(), "into a json object")
is.Equal(nil, out)

out, err = pd.ConvertValue(`{"object": true}`)
is.NoError(err)
is.Equal(map[string]interface{}{"object": true}, out)
}

0 comments on commit 20e825c

Please sign in to comment.