command/fix: fixer for iso_md5 change

This commit is contained in:
Mitchell Hashimoto 2013-07-14 16:51:20 +09:00
parent 8ee8612997
commit f91d4f2244
3 changed files with 107 additions and 0 deletions

18
command/fix/fixer.go Normal file
View File

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

View File

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

View File

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