options.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Package options provides a way to pass unstructured sets of options to a
  2. // component expecting a strongly-typed configuration structure.
  3. package options
  4. import (
  5. "fmt"
  6. "reflect"
  7. )
  8. // NoSuchFieldError is the error returned when the generic parameters hold a
  9. // value for a field absent from the destination structure.
  10. type NoSuchFieldError struct {
  11. Field string
  12. Type string
  13. }
  14. func (e NoSuchFieldError) Error() string {
  15. return fmt.Sprintf("no field %q in type %q", e.Field, e.Type)
  16. }
  17. // CannotSetFieldError is the error returned when the generic parameters hold a
  18. // value for a field that cannot be set in the destination structure.
  19. type CannotSetFieldError struct {
  20. Field string
  21. Type string
  22. }
  23. func (e CannotSetFieldError) Error() string {
  24. return fmt.Sprintf("cannot set field %q of type %q", e.Field, e.Type)
  25. }
  26. // TypeMismatchError is the error returned when the type of the generic value
  27. // for a field mismatches the type of the destination structure.
  28. type TypeMismatchError struct {
  29. Field string
  30. ExpectType string
  31. ActualType string
  32. }
  33. func (e TypeMismatchError) Error() string {
  34. return fmt.Sprintf("type mismatch, field %s require type %v, actual type %v", e.Field, e.ExpectType, e.ActualType)
  35. }
  36. // Generic is a basic type to store arbitrary settings.
  37. type Generic map[string]interface{}
  38. // NewGeneric returns a new Generic instance.
  39. func NewGeneric() Generic {
  40. return make(Generic)
  41. }
  42. // GenerateFromModel takes the generic options, and tries to build a new
  43. // instance of the model's type by matching keys from the generic options to
  44. // fields in the model.
  45. //
  46. // The return value is of the same type than the model (including a potential
  47. // pointer qualifier).
  48. func GenerateFromModel(options Generic, model interface{}) (interface{}, error) {
  49. modType := reflect.TypeOf(model)
  50. // If the model is of pointer type, we need to dereference for New.
  51. resType := reflect.TypeOf(model)
  52. if modType.Kind() == reflect.Ptr {
  53. resType = resType.Elem()
  54. }
  55. // Populate the result structure with the generic layout content.
  56. res := reflect.New(resType)
  57. for name, value := range options {
  58. field := res.Elem().FieldByName(name)
  59. if !field.IsValid() {
  60. return nil, NoSuchFieldError{name, resType.String()}
  61. }
  62. if !field.CanSet() {
  63. return nil, CannotSetFieldError{name, resType.String()}
  64. }
  65. if reflect.TypeOf(value) != field.Type() {
  66. return nil, TypeMismatchError{name, field.Type().String(), reflect.TypeOf(value).String()}
  67. }
  68. field.Set(reflect.ValueOf(value))
  69. }
  70. // If the model is not of pointer type, return content of the result.
  71. if modType.Kind() == reflect.Ptr {
  72. return res.Interface(), nil
  73. }
  74. return res.Elem().Interface(), nil
  75. }