2015-03-04 16:25:43 -05:00
|
|
|
// Package options provides a way to pass unstructured sets of options to a
|
2015-02-20 01:21:05 -05:00
|
|
|
// component expecting a strongly-typed configuration structure.
|
|
|
|
package options
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
2015-03-04 16:25:43 -05:00
|
|
|
// NoSuchFieldError is the error returned when the generic parameters hold a
|
|
|
|
// value for a field absent from the destination structure.
|
2015-02-20 01:21:05 -05:00
|
|
|
type NoSuchFieldError struct {
|
|
|
|
Field string
|
|
|
|
Type string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e NoSuchFieldError) Error() string {
|
|
|
|
return fmt.Sprintf("no field %q in type %q", e.Field, e.Type)
|
|
|
|
}
|
|
|
|
|
2015-03-04 16:25:43 -05:00
|
|
|
// CannotSetFieldError is the error returned when the generic parameters hold a
|
|
|
|
// value for a field that cannot be set in the destination structure.
|
2015-02-20 01:21:05 -05:00
|
|
|
type CannotSetFieldError struct {
|
|
|
|
Field string
|
|
|
|
Type string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e CannotSetFieldError) Error() string {
|
|
|
|
return fmt.Sprintf("cannot set field %q of type %q", e.Field, e.Type)
|
|
|
|
}
|
|
|
|
|
2015-08-25 23:30:17 -04:00
|
|
|
// TypeMismatchError is the error returned when the type of the generic value
|
|
|
|
// for a field mismatches the type of the destination structure.
|
|
|
|
type TypeMismatchError struct {
|
|
|
|
Field string
|
|
|
|
ExpectType string
|
|
|
|
ActualType string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e TypeMismatchError) Error() string {
|
|
|
|
return fmt.Sprintf("type mismatch, field %s require type %v, actual type %v", e.Field, e.ExpectType, e.ActualType)
|
|
|
|
}
|
|
|
|
|
2016-06-02 01:37:39 -04:00
|
|
|
// Generic is a basic type to store arbitrary settings.
|
2015-02-20 01:21:05 -05:00
|
|
|
type Generic map[string]interface{}
|
|
|
|
|
2015-03-04 16:25:43 -05:00
|
|
|
// NewGeneric returns a new Generic instance.
|
2015-02-20 01:21:05 -05:00
|
|
|
func NewGeneric() Generic {
|
|
|
|
return make(Generic)
|
|
|
|
}
|
|
|
|
|
2015-03-04 16:25:43 -05:00
|
|
|
// GenerateFromModel takes the generic options, and tries to build a new
|
|
|
|
// instance of the model's type by matching keys from the generic options to
|
|
|
|
// fields in the model.
|
|
|
|
//
|
|
|
|
// The return value is of the same type than the model (including a potential
|
|
|
|
// pointer qualifier).
|
2015-02-20 01:21:05 -05:00
|
|
|
func GenerateFromModel(options Generic, model interface{}) (interface{}, error) {
|
2015-02-20 12:12:29 -05:00
|
|
|
modType := reflect.TypeOf(model)
|
|
|
|
|
|
|
|
// If the model is of pointer type, we need to dereference for New.
|
|
|
|
resType := reflect.TypeOf(model)
|
|
|
|
if modType.Kind() == reflect.Ptr {
|
|
|
|
resType = resType.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate the result structure with the generic layout content.
|
|
|
|
res := reflect.New(resType)
|
2015-02-20 01:21:05 -05:00
|
|
|
for name, value := range options {
|
|
|
|
field := res.Elem().FieldByName(name)
|
|
|
|
if !field.IsValid() {
|
2015-02-22 20:58:52 -05:00
|
|
|
return nil, NoSuchFieldError{name, resType.String()}
|
2015-02-20 01:21:05 -05:00
|
|
|
}
|
|
|
|
if !field.CanSet() {
|
2015-02-22 20:58:52 -05:00
|
|
|
return nil, CannotSetFieldError{name, resType.String()}
|
2015-02-20 01:21:05 -05:00
|
|
|
}
|
2015-08-25 23:30:17 -04:00
|
|
|
if reflect.TypeOf(value) != field.Type() {
|
|
|
|
return nil, TypeMismatchError{name, field.Type().String(), reflect.TypeOf(value).String()}
|
|
|
|
}
|
2015-02-20 01:21:05 -05:00
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
}
|
2015-02-20 12:12:29 -05:00
|
|
|
|
|
|
|
// If the model is not of pointer type, return content of the result.
|
|
|
|
if modType.Kind() == reflect.Ptr {
|
|
|
|
return res.Interface(), nil
|
|
|
|
}
|
|
|
|
return res.Elem().Interface(), nil
|
2015-02-20 01:21:05 -05:00
|
|
|
}
|