From e84669aa370fac5077c8f3cc0f0fde3b9019c419 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 19 Jul 2013 14:59:04 -0400 Subject: [PATCH] builder/common: extract out decode config --- builder/amazon/ebs/builder.go | 16 +------------ builder/common/config.go | 31 ++++++++++++++++++++++++ builder/common/config_test.go | 45 +++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 15 deletions(-) create mode 100644 builder/common/config.go create mode 100644 builder/common/config_test.go diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index 3ff13884f..9bd4ac519 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -10,7 +10,6 @@ import ( "fmt" "github.com/mitchellh/goamz/aws" "github.com/mitchellh/goamz/ec2" - "github.com/mitchellh/mapstructure" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/builder/common" "github.com/mitchellh/packer/packer" @@ -53,24 +52,11 @@ type Builder struct { } func (b *Builder) Prepare(raws ...interface{}) error { - var md mapstructure.Metadata - decoderConfig := &mapstructure.DecoderConfig{ - Metadata: &md, - Result: &b.config, - } - - decoder, err := mapstructure.NewDecoder(decoderConfig) + md, err := common.DecodeConfig(&b.config, raws...) if err != nil { return err } - for _, raw := range raws { - err := decoder.Decode(raw) - if err != nil { - return err - } - } - // Accumulate any errors errs := make([]error, 0) diff --git a/builder/common/config.go b/builder/common/config.go new file mode 100644 index 000000000..bf52772ba --- /dev/null +++ b/builder/common/config.go @@ -0,0 +1,31 @@ +package common + +import ( + "github.com/mitchellh/mapstructure" +) + +// DecodeConfig is a helper that handles decoding raw configuration using +// mapstructure. It returns the metadata and any errors that may happen. +// If you need extra configuration for mapstructure, you should configure +// it manually and not use this helper function. +func DecodeConfig(target interface{}, raws ...interface{}) (*mapstructure.Metadata, error) { + var md mapstructure.Metadata + decoderConfig := &mapstructure.DecoderConfig{ + Metadata: &md, + Result: target, + } + + decoder, err := mapstructure.NewDecoder(decoderConfig) + if err != nil { + return nil, err + } + + for _, raw := range raws { + err := decoder.Decode(raw) + if err != nil { + return nil, err + } + } + + return &md, nil +} diff --git a/builder/common/config_test.go b/builder/common/config_test.go new file mode 100644 index 000000000..4d0a4d981 --- /dev/null +++ b/builder/common/config_test.go @@ -0,0 +1,45 @@ +package common + +import ( + "reflect" + "testing" +) + +func TestDecodeConfig(t *testing.T) { + type Local struct { + Foo string + Bar string + } + + raws := []interface{}{ + map[string]interface{}{ + "foo": "bar", + }, + map[string]interface{}{ + "bar": "baz", + "baz": "what", + }, + } + + var result Local + md, err := DecodeConfig(&result, raws...) + if err != nil { + t.Fatalf("err: %s", err) + } + + if result.Foo != "bar" { + t.Fatalf("invalid: %#v", result.Foo) + } + + if result.Bar != "baz" { + t.Fatalf("invalid: %#v", result.Bar) + } + + if md == nil { + t.Fatal("metadata should not be nil") + } + + if !reflect.DeepEqual(md.Unused, []string{"baz"}) { + t.Fatalf("unused: %#v", md.Unused) + } +}