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

This commit is contained in:
Mitchell Hashimoto 2014-04-28 16:18:11 -07:00
parent d236e139ac
commit ba05119a75
2 changed files with 35 additions and 0 deletions

View File

@ -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)

View File

@ -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")