Fix jsonpath to handle maps with key of nonstring types

This commit is contained in:
Avesh Agarwal
2016-03-22 17:32:40 -04:00
parent b50e89e323
commit d5e135e42c
3 changed files with 21 additions and 6 deletions

View File

@@ -325,7 +325,13 @@ func (j *JSONPath) evalField(input []reflect.Value, node *FieldNode) ([]reflect.
return nil, err
}
} else if value.Kind() == reflect.Map {
result = value.MapIndex(reflect.ValueOf(node.Value))
mapKeyType := value.Type().Key()
nodeValue := reflect.ValueOf(node.Value)
// node value type must be convertible to map key type
if !nodeValue.Type().ConvertibleTo(mapKeyType) {
return results, fmt.Errorf("%s is not convertible to %s", nodeValue, mapKeyType)
}
result = value.MapIndex(nodeValue.Convert(mapKeyType))
}
if result.IsValid() {
results = append(results, result)