common: user variable conversion to non-string types works [GH-1079]
This commit is contained in:
parent
d236e139ac
commit
ba05119a75
|
@ -217,6 +217,15 @@ func decodeConfigHook(raws []interface{}) (mapstructure.DecodeHookFunc, error) {
|
||||||
|
|
||||||
return func(f reflect.Kind, t reflect.Kind, v interface{}) (interface{}, error) {
|
return func(f reflect.Kind, t reflect.Kind, v interface{}) (interface{}, error) {
|
||||||
if t != reflect.String {
|
if t != reflect.String {
|
||||||
|
if f == reflect.Slice {
|
||||||
|
dataVal := reflect.ValueOf(v)
|
||||||
|
dataType := dataVal.Type()
|
||||||
|
elemKind := dataType.Elem().Kind()
|
||||||
|
if elemKind == reflect.Uint8 {
|
||||||
|
v = string(dataVal.Interface().([]uint8))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if sv, ok := v.(string); ok {
|
if sv, ok := v.(string); ok {
|
||||||
var err error
|
var err error
|
||||||
v, err = tpl.Process(sv, nil)
|
v, err = tpl.Process(sv, nil)
|
||||||
|
|
|
@ -148,6 +148,32 @@ func TestDecodeConfig_userVarConversion(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This tests the way MessagePack decodes strings (into []uint8) and
|
||||||
|
// that we can still decode into the proper types.
|
||||||
|
func TestDecodeConfig_userVarConversionUInt8(t *testing.T) {
|
||||||
|
type Local struct {
|
||||||
|
Val int
|
||||||
|
}
|
||||||
|
|
||||||
|
raw := map[string]interface{}{
|
||||||
|
"packer_user_variables": map[string]string{
|
||||||
|
"foo": "42",
|
||||||
|
},
|
||||||
|
|
||||||
|
"val": []uint8("{{user `foo`}}"),
|
||||||
|
}
|
||||||
|
|
||||||
|
var result Local
|
||||||
|
_, err := DecodeConfig(&result, raw)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if result.Val != 42 {
|
||||||
|
t.Fatalf("invalid: %#v", result.Val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDownloadableURL(t *testing.T) {
|
func TestDownloadableURL(t *testing.T) {
|
||||||
// Invalid URL: has hex code in host
|
// Invalid URL: has hex code in host
|
||||||
_, err := DownloadableURL("http://what%20.com")
|
_, err := DownloadableURL("http://what%20.com")
|
||||||
|
|
Loading…
Reference in New Issue