2016-04-19 01:31:19 +02:00
|
|
|
package hil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-06-03 20:32:07 +02:00
|
|
|
"reflect"
|
2016-04-19 01:31:19 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/hil/ast"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
2016-06-03 20:32:07 +02:00
|
|
|
var hilMapstructureDecodeHookSlice []interface{}
|
|
|
|
var hilMapstructureDecodeHookStringSlice []string
|
|
|
|
var hilMapstructureDecodeHookMap map[string]interface{}
|
|
|
|
|
|
|
|
// hilMapstructureWeakDecode behaves in the same way as mapstructure.WeakDecode
|
|
|
|
// but has a DecodeHook which defeats the backward compatibility mode of mapstructure
|
|
|
|
// which WeakDecodes []interface{}{} into an empty map[string]interface{}. This
|
|
|
|
// allows us to use WeakDecode (desirable), but not fail on empty lists.
|
|
|
|
func hilMapstructureWeakDecode(m interface{}, rawVal interface{}) error {
|
|
|
|
config := &mapstructure.DecoderConfig{
|
|
|
|
DecodeHook: func(source reflect.Type, target reflect.Type, val interface{}) (interface{}, error) {
|
|
|
|
sliceType := reflect.TypeOf(hilMapstructureDecodeHookSlice)
|
|
|
|
stringSliceType := reflect.TypeOf(hilMapstructureDecodeHookStringSlice)
|
|
|
|
mapType := reflect.TypeOf(hilMapstructureDecodeHookMap)
|
|
|
|
|
|
|
|
if (source == sliceType || source == stringSliceType) && target == mapType {
|
|
|
|
return nil, fmt.Errorf("Cannot convert %s into a %s", source, target)
|
|
|
|
}
|
|
|
|
|
|
|
|
return val, nil
|
|
|
|
},
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
Result: rawVal,
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder, err := mapstructure.NewDecoder(config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return decoder.Decode(m)
|
|
|
|
}
|
|
|
|
|
2016-04-19 01:31:19 +02:00
|
|
|
func InterfaceToVariable(input interface{}) (ast.Variable, error) {
|
|
|
|
var stringVal string
|
2016-06-03 20:32:07 +02:00
|
|
|
if err := hilMapstructureWeakDecode(input, &stringVal); err == nil {
|
2016-04-19 01:31:19 +02:00
|
|
|
return ast.Variable{
|
|
|
|
Type: ast.TypeString,
|
|
|
|
Value: stringVal,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-04-19 02:15:50 +02:00
|
|
|
var mapVal map[string]interface{}
|
2016-06-03 20:32:07 +02:00
|
|
|
if err := hilMapstructureWeakDecode(input, &mapVal); err == nil {
|
2016-04-19 02:15:50 +02:00
|
|
|
elements := make(map[string]ast.Variable)
|
|
|
|
for i, element := range mapVal {
|
2016-04-19 01:31:19 +02:00
|
|
|
varElement, err := InterfaceToVariable(element)
|
|
|
|
if err != nil {
|
|
|
|
return ast.Variable{}, err
|
|
|
|
}
|
|
|
|
elements[i] = varElement
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast.Variable{
|
2016-04-19 02:15:50 +02:00
|
|
|
Type: ast.TypeMap,
|
2016-04-19 01:31:19 +02:00
|
|
|
Value: elements,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-04-19 02:15:50 +02:00
|
|
|
var sliceVal []interface{}
|
2016-06-03 20:32:07 +02:00
|
|
|
if err := hilMapstructureWeakDecode(input, &sliceVal); err == nil {
|
2016-04-19 02:15:50 +02:00
|
|
|
elements := make([]ast.Variable, len(sliceVal))
|
|
|
|
for i, element := range sliceVal {
|
2016-04-19 01:31:19 +02:00
|
|
|
varElement, err := InterfaceToVariable(element)
|
|
|
|
if err != nil {
|
|
|
|
return ast.Variable{}, err
|
|
|
|
}
|
|
|
|
elements[i] = varElement
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast.Variable{
|
2016-04-19 02:15:50 +02:00
|
|
|
Type: ast.TypeList,
|
2016-04-19 01:31:19 +02:00
|
|
|
Value: elements,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast.Variable{}, fmt.Errorf("value for conversion must be a string, interface{} or map[string]interface: got %T", input)
|
|
|
|
}
|