From ba05119a751283d9c5fead1258886f540314aced Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 28 Apr 2014 16:18:11 -0700 Subject: [PATCH] common: user variable conversion to non-string types works [GH-1079] --- common/config.go | 9 +++++++++ common/config_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/common/config.go b/common/config.go index 4c6fbbbef..f55cbbb2f 100644 --- a/common/config.go +++ b/common/config.go @@ -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) diff --git a/common/config_test.go b/common/config_test.go index 3217a2ebd..1108ccb18 100644 --- a/common/config_test.go +++ b/common/config_test.go @@ -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")