From ab2af979fd2903d4faa7b064ca8bb2e16d4648c5 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 14 Jul 2013 16:51:20 +0900 Subject: [PATCH] command/fix: fixer for iso_md5 change --- command/fix/fixer.go | 18 ++++++++++++ command/fix/fixer_iso_md5.go | 43 +++++++++++++++++++++++++++++ command/fix/fixer_iso_md5_test.go | 46 +++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 command/fix/fixer.go create mode 100644 command/fix/fixer_iso_md5.go create mode 100644 command/fix/fixer_iso_md5_test.go diff --git a/command/fix/fixer.go b/command/fix/fixer.go new file mode 100644 index 000000000..7a754225a --- /dev/null +++ b/command/fix/fixer.go @@ -0,0 +1,18 @@ +package fix + +// A Fixer is something that can perform a fix operation on a template. +type Fixer interface { + // Fix takes a raw map structure input, potentially transforms it + // in some way, and returns the new, transformed structure. The + // Fix method is allowed to mutate the input. + Fix(input map[string]interface{}) (map[string]interface{}, error) +} + +// Fixers is the map of all available fixers, by name. +var Fixers map[string]Fixer + +func init() { + Fixers = map[string]Fixer{ + "iso-md5": new(FixerISOMD5), + } +} diff --git a/command/fix/fixer_iso_md5.go b/command/fix/fixer_iso_md5.go new file mode 100644 index 000000000..0b14f45e1 --- /dev/null +++ b/command/fix/fixer_iso_md5.go @@ -0,0 +1,43 @@ +package fix + +import ( + "github.com/mitchellh/mapstructure" +) + +// FixerISOMD5 is a Fixer that replaces the "iso_md5" configuration key +// with the newer "iso_checksum" and "iso_checksum_type" within builders. +type FixerISOMD5 struct{} + +func (FixerISOMD5) Fix(input map[string]interface{}) (map[string]interface{}, error) { + // Our template type we'll use for this fixer only + type template struct { + Builders []map[string]interface{} + } + + // Decode the input into our structure, if we can + var tpl template + if err := mapstructure.Decode(input, &tpl); err != nil { + return nil, err + } + + // Go through each builder and replace the iso_md5 if we can + for _, builder := range tpl.Builders { + md5raw, ok := builder["iso_md5"] + if !ok { + continue + } + + md5, ok := md5raw.(string) + if !ok { + // TODO: error? + continue + } + + delete(builder, "iso_md5") + builder["iso_checksum"] = md5 + builder["iso_checksum_type"] = "md5" + } + + input["builders"] = tpl.Builders + return input, nil +} diff --git a/command/fix/fixer_iso_md5_test.go b/command/fix/fixer_iso_md5_test.go new file mode 100644 index 000000000..09be5e0e1 --- /dev/null +++ b/command/fix/fixer_iso_md5_test.go @@ -0,0 +1,46 @@ +package fix + +import ( + "reflect" + "testing" +) + +func TestFixerISOMD5_Impl(t *testing.T) { + var raw interface{} + raw = new(FixerISOMD5) + if _, ok := raw.(Fixer); !ok { + t.Fatalf("must be a Fixer") + } +} + +func TestFixerISOMD5_Fix(t *testing.T) { + var f FixerISOMD5 + + input := map[string]interface{}{ + "builders": []interface{}{ + map[string]string{ + "type": "foo", + "iso_md5": "bar", + }, + }, + } + + expected := map[string]interface{}{ + "builders": []map[string]interface{}{ + map[string]interface{}{ + "type": "foo", + "iso_checksum": "bar", + "iso_checksum_type": "md5", + }, + }, + } + + output, err := f.Fix(input) + if err != nil { + t.Fatalf("err: %s", err) + } + + if !reflect.DeepEqual(output, expected) { + t.Fatalf("unexpected: %#v\nexpected: %#v\n", output, expected) + } +}