7 "github.com/hashicorp/hil/ast"
8 "github.com/mitchellh/mapstructure"
11 // UnknownValue is a sentinel value that can be used to denote
12 // that a value of a variable (or map element, list element, etc.)
13 // is unknown. This will always have the type ast.TypeUnknown.
14 const UnknownValue = "74D93920-ED26-11E3-AC10-0800200C9A66"
16 var hilMapstructureDecodeHookSlice []interface{}
17 var hilMapstructureDecodeHookStringSlice []string
18 var hilMapstructureDecodeHookMap map[string]interface{}
20 // hilMapstructureWeakDecode behaves in the same way as mapstructure.WeakDecode
21 // but has a DecodeHook which defeats the backward compatibility mode of mapstructure
22 // which WeakDecodes []interface{}{} into an empty map[string]interface{}. This
23 // allows us to use WeakDecode (desirable), but not fail on empty lists.
24 func hilMapstructureWeakDecode(m interface{}, rawVal interface{}) error {
25 config := &mapstructure.DecoderConfig{
26 DecodeHook: func(source reflect.Type, target reflect.Type, val interface{}) (interface{}, error) {
27 sliceType := reflect.TypeOf(hilMapstructureDecodeHookSlice)
28 stringSliceType := reflect.TypeOf(hilMapstructureDecodeHookStringSlice)
29 mapType := reflect.TypeOf(hilMapstructureDecodeHookMap)
31 if (source == sliceType || source == stringSliceType) && target == mapType {
32 return nil, fmt.Errorf("Cannot convert %s into a %s", source, target)
37 WeaklyTypedInput: true,
41 decoder, err := mapstructure.NewDecoder(config)
46 return decoder.Decode(m)
49 func InterfaceToVariable(input interface{}) (ast.Variable, error) {
50 if iv, ok := input.(ast.Variable); ok {
54 // This is just to maintain backward compatibility
55 // after https://github.com/mitchellh/mapstructure/pull/98
56 if v, ok := input.([]ast.Variable); ok {
62 if v, ok := input.(map[string]ast.Variable); ok {
70 if err := hilMapstructureWeakDecode(input, &stringVal); err == nil {
71 // Special case the unknown value to turn into "unknown"
72 if stringVal == UnknownValue {
73 return ast.Variable{Value: UnknownValue, Type: ast.TypeUnknown}, nil
76 // Otherwise return the string value
83 var mapVal map[string]interface{}
84 if err := hilMapstructureWeakDecode(input, &mapVal); err == nil {
85 elements := make(map[string]ast.Variable)
86 for i, element := range mapVal {
87 varElement, err := InterfaceToVariable(element)
89 return ast.Variable{}, err
91 elements[i] = varElement
100 var sliceVal []interface{}
101 if err := hilMapstructureWeakDecode(input, &sliceVal); err == nil {
102 elements := make([]ast.Variable, len(sliceVal))
103 for i, element := range sliceVal {
104 varElement, err := InterfaceToVariable(element)
106 return ast.Variable{}, err
108 elements[i] = varElement
117 return ast.Variable{}, fmt.Errorf("value for conversion must be a string, interface{} or map[string]interface: got %T", input)
120 func VariableToInterface(input ast.Variable) (interface{}, error) {
121 if input.Type == ast.TypeString {
122 if inputStr, ok := input.Value.(string); ok {
125 return nil, fmt.Errorf("ast.Variable with type string has value which is not a string")
129 if input.Type == ast.TypeList {
130 inputList, ok := input.Value.([]ast.Variable)
132 return nil, fmt.Errorf("ast.Variable with type list has value which is not a []ast.Variable")
135 result := make([]interface{}, 0)
136 if len(inputList) == 0 {
140 for _, element := range inputList {
141 if convertedElement, err := VariableToInterface(element); err == nil {
142 result = append(result, convertedElement)
151 if input.Type == ast.TypeMap {
152 inputMap, ok := input.Value.(map[string]ast.Variable)
154 return nil, fmt.Errorf("ast.Variable with type map has value which is not a map[string]ast.Variable")
157 result := make(map[string]interface{}, 0)
158 if len(inputMap) == 0 {
162 for key, value := range inputMap {
163 if convertedValue, err := VariableToInterface(value); err == nil {
164 result[key] = convertedValue
173 return nil, fmt.Errorf("unknown input type: %s", input.Type)