/usr/share/gocode/src/github.com/hashicorp/hil/convert.go is in golang-github-hashicorp-hil-dev 0.0~git20160711.1e86c6b-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | package hil
import (
"fmt"
"reflect"
"github.com/hashicorp/hil/ast"
"github.com/mitchellh/mapstructure"
)
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)
}
func InterfaceToVariable(input interface{}) (ast.Variable, error) {
if inputVariable, ok := input.(ast.Variable); ok {
return inputVariable, nil
}
var stringVal string
if err := hilMapstructureWeakDecode(input, &stringVal); err == nil {
return ast.Variable{
Type: ast.TypeString,
Value: stringVal,
}, nil
}
var mapVal map[string]interface{}
if err := hilMapstructureWeakDecode(input, &mapVal); err == nil {
elements := make(map[string]ast.Variable)
for i, element := range mapVal {
varElement, err := InterfaceToVariable(element)
if err != nil {
return ast.Variable{}, err
}
elements[i] = varElement
}
return ast.Variable{
Type: ast.TypeMap,
Value: elements,
}, nil
}
var sliceVal []interface{}
if err := hilMapstructureWeakDecode(input, &sliceVal); err == nil {
elements := make([]ast.Variable, len(sliceVal))
for i, element := range sliceVal {
varElement, err := InterfaceToVariable(element)
if err != nil {
return ast.Variable{}, err
}
elements[i] = varElement
}
return ast.Variable{
Type: ast.TypeList,
Value: elements,
}, nil
}
return ast.Variable{}, fmt.Errorf("value for conversion must be a string, interface{} or map[string]interface: got %T", input)
}
func VariableToInterface(input ast.Variable) (interface{}, error) {
if input.Type == ast.TypeString {
if inputStr, ok := input.Value.(string); ok {
return inputStr, nil
} else {
return nil, fmt.Errorf("ast.Variable with type string has value which is not a string")
}
}
if input.Type == ast.TypeList {
inputList, ok := input.Value.([]ast.Variable)
if !ok {
return nil, fmt.Errorf("ast.Variable with type list has value which is not a []ast.Variable")
}
result := make([]interface{}, 0)
if len(inputList) == 0 {
return result, nil
}
for _, element := range inputList {
if convertedElement, err := VariableToInterface(element); err == nil {
result = append(result, convertedElement)
} else {
return nil, err
}
}
return result, nil
}
if input.Type == ast.TypeMap {
inputMap, ok := input.Value.(map[string]ast.Variable)
if !ok {
return nil, fmt.Errorf("ast.Variable with type map has value which is not a map[string]ast.Variable")
}
result := make(map[string]interface{}, 0)
if len(inputMap) == 0 {
return result, nil
}
for key, value := range inputMap {
if convertedValue, err := VariableToInterface(value); err == nil {
result[key] = convertedValue
} else {
return nil, err
}
}
return result, nil
}
return nil, fmt.Errorf("unknown input type: %s", input.Type)
}
|