2015-08-29 09:45:00 +00:00
|
|
|
package confection2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// config stores application config.
|
|
|
|
config interface{}
|
|
|
|
)
|
|
|
|
|
2015-08-29 09:55:34 +00:00
|
|
|
// Manage accepts a pointer to a configuration struct.
|
|
|
|
func Manage(target interface{}) {
|
|
|
|
if ok := isStructPtr(target); !ok {
|
|
|
|
panic("Argument must be a pointer to a struct")
|
|
|
|
}
|
|
|
|
|
|
|
|
config = target
|
|
|
|
}
|
|
|
|
|
2015-08-29 09:45:00 +00:00
|
|
|
func isStructPtr(target interface{}) bool {
|
|
|
|
if val := reflect.ValueOf(target); val.Kind() == reflect.Ptr {
|
|
|
|
if val = reflect.Indirect(val); val.Kind() == reflect.Struct {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshal(body []byte, target interface{}) error {
|
|
|
|
if err := json.Unmarshal(body, target); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-08-29 09:55:18 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|