diff --git a/confection.go b/confection.go index 88eb651..86cc79a 100644 --- a/confection.go +++ b/confection.go @@ -27,3 +27,16 @@ func unmarshal(body []byte, target interface{}) error { return nil } + +func duplicate(original interface{}) interface{} { + // Get the interface value + val := reflect.ValueOf(original) + // We expect a pointer to a struct, so now we need the underlying staruct + val = reflect.Indirect(val) + // Now we need the type (name) of this struct + typ := val.Type() + // Creating a duplicate instance of that struct + dupe := reflect.New(typ).Interface() + + return dupe +} diff --git a/confection_test.go b/confection_test.go index 01d5055..b169327 100644 --- a/confection_test.go +++ b/confection_test.go @@ -42,3 +42,12 @@ func TestUnmarshal(t *testing.T) { t.Errorf("Expected Bar to equal %q, got %q", 1, conf.Bar) } } + +func TestDuplicate(t *testing.T) { + var i interface{} = &testConf{} + + dupe := duplicate(i) + if _, ok := dupe.(*testConf); !ok { + t.Error("Duplication failed") + } +}