Commit ba05119a authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

common: user variable conversion to non-string types works [GH-1079]

parent d236e139
......@@ -217,6 +217,15 @@ func decodeConfigHook(raws []interface{}) (mapstructure.DecodeHookFunc, error) {
return func(f reflect.Kind, t reflect.Kind, v interface{}) (interface{}, error) {
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 {
var err error
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) {
// Invalid URL: has hex code in host
_, err := DownloadableURL("http://what%20.com")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment