config.go 921 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package config
  2. import (
  3. "fmt"
  4. )
  5. const Redacted = "REDACTED"
  6. // Config allows for nested configurations which encapsulate their functionality to be validated and compared easily
  7. type Config interface {
  8. Validate() error
  9. Sanitize() Config
  10. Equals(Config) bool
  11. }
  12. // KeyedConfig is a top level Config which uses its public values as a unique identifier allowing duplicates to be identified
  13. type KeyedConfig interface {
  14. Config
  15. Key() string
  16. }
  17. type KeyedConfigWatcher interface {
  18. GetConfigs() []KeyedConfig
  19. }
  20. func GetInterfaceValue[T any](fmap map[string]interface{}, key string) (T, error) {
  21. var value T
  22. interfaceValue, ok := fmap[key]
  23. if !ok {
  24. return value, fmt.Errorf("FromInterface: missing '%s' property", key)
  25. }
  26. typedValue, ok := interfaceValue.(T)
  27. if !ok {
  28. return value, fmt.Errorf("GetInterfaceValue: property '%s' had expected type '%T' but did not match", key, value)
  29. }
  30. return typedValue, nil
  31. }