config.go 638 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package config
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. type IConfig interface {
  7. Invalidate() error
  8. }
  9. func Invalidate(config IConfig) error {
  10. if config == nil {
  11. return fmt.Errorf("config must not be a nil")
  12. }
  13. val := reflect.ValueOf(config)
  14. if val.Kind() != reflect.Struct {
  15. if val.IsZero() {
  16. return fmt.Errorf("config is empty")
  17. }
  18. val = reflect.ValueOf(config).Elem()
  19. }
  20. for i := 0; i < val.NumField(); i++ {
  21. if !val.Field(i).CanInterface() || val.Field(i).IsZero() {
  22. continue
  23. }
  24. if elm, ok := val.Field(i).Interface().(IConfig); ok {
  25. if er := elm.Invalidate(); er != nil {
  26. return er
  27. }
  28. }
  29. }
  30. return nil
  31. }