options.go 2.7 KB

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