Skip to content

Commit

Permalink
Check index types (google#116)
Browse files Browse the repository at this point in the history
Before it would just crash
  • Loading branch information
sbarzowski authored and sparkprime committed Oct 10, 2017
1 parent ba0f236 commit 4a4a7fe
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 7 deletions.
24 changes: 17 additions & 7 deletions interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,16 +362,26 @@ func (i *interpreter) evaluate(a ast.Node) (value, error) {
return nil, err
}
switch target := targetValue.(type) {
// TODO(sbarzowski) better error handling if bad index type
case valueObject:
indexString := index.(*valueString).getString()
return target.index(e, indexString)
indexString, err := e.getString(index)
if err != nil {
return nil, err
}
return target.index(e, indexString.getString())
case *valueArray:
indexInt := int(index.(*valueNumber).value)
return e.evaluate(target.elements[indexInt])
indexInt, err := e.getNumber(index)
if err != nil {
return nil, err
}
// TODO(https://github.com/google/jsonnet/issues/377): non-integer indexes should be an error
return e.evaluate(target.elements[int(indexInt.value)])
case *valueString:
indexInt := int(index.(*valueNumber).value)
return target.index(e, indexInt)
indexInt, err := e.getNumber(index)
if err != nil {
return nil, err
}
// TODO(https://github.com/google/jsonnet/issues/377): non-integer indexes should be an error
return target.index(e, int(indexInt.value))
}

return nil, e.Error(fmt.Sprintf("Value non indexable: %v", reflect.TypeOf(targetValue)))
Expand Down
10 changes: 10 additions & 0 deletions testdata/bad_index_array.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
RUNTIME ERROR: Unexpected type string, expected number
-------------------------------------------------
testdata/bad_index_array:1:1-10 $

[]["xxx"]

-------------------------------------------------
During evaluation


1 change: 1 addition & 0 deletions testdata/bad_index_array.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]["xxx"]
10 changes: 10 additions & 0 deletions testdata/bad_index_object.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
RUNTIME ERROR: Unexpected type number, expected string
-------------------------------------------------
testdata/bad_index_object:1:1-7 $

{}[42]

-------------------------------------------------
During evaluation


1 change: 1 addition & 0 deletions testdata/bad_index_object.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}[42]
10 changes: 10 additions & 0 deletions testdata/bad_index_string.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
RUNTIME ERROR: Unexpected type string, expected number
-------------------------------------------------
testdata/bad_index_string:1:2-13 $

"xxx"["xxx"]

-------------------------------------------------
During evaluation


1 change: 1 addition & 0 deletions testdata/bad_index_string.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"xxx"["xxx"]

0 comments on commit 4a4a7fe

Please sign in to comment.