options.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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]any
  38. // GenerateFromModel takes the generic options, and tries to build a new
  39. // instance of the model's type by matching keys from the generic options to
  40. // fields in the model.
  41. //
  42. // The return value is of the same type than the model (including a potential
  43. // pointer qualifier).
  44. func GenerateFromModel(options Generic, model interface{}) (interface{}, error) {
  45. modType := reflect.TypeOf(model)
  46. // If the model is of pointer type, we need to dereference for New.
  47. resType := reflect.TypeOf(model)
  48. if modType.Kind() == reflect.Ptr {
  49. resType = resType.Elem()
  50. }
  51. // Populate the result structure with the generic layout content.
  52. res := reflect.New(resType)
  53. for name, value := range options {
  54. field := res.Elem().FieldByName(name)
  55. if !field.IsValid() {
  56. return nil, NoSuchFieldError{name, resType.String()}
  57. }
  58. if !field.CanSet() {
  59. return nil, CannotSetFieldError{name, resType.String()}
  60. }
  61. if reflect.TypeOf(value) != field.Type() {
  62. return nil, TypeMismatchError{name, field.Type().String(), reflect.TypeOf(value).String()}
  63. }
  64. field.Set(reflect.ValueOf(value))
  65. }
  66. // If the model is not of pointer type, return content of the result.
  67. if modType.Kind() == reflect.Ptr {
  68. return res.Interface(), nil
  69. }
  70. return res.Elem().Interface(), nil
  71. }