loader.go 717 B

123456789101112131415161718192021222324252627282930313233343536
  1. package loader
  2. import (
  3. "errors"
  4. "v2ray.com/core/common"
  5. )
  6. var (
  7. ErrUnknownConfigID = errors.New("Unknown config ID.")
  8. )
  9. type ConfigCreator func() interface{}
  10. type ConfigCreatorCache map[string]ConfigCreator
  11. func (this ConfigCreatorCache) RegisterCreator(id string, creator ConfigCreator) error {
  12. if _, found := this[id]; found {
  13. return common.ErrDuplicatedName
  14. }
  15. this[id] = creator
  16. return nil
  17. }
  18. func (this ConfigCreatorCache) CreateConfig(id string) (interface{}, error) {
  19. creator, found := this[id]
  20. if !found {
  21. return nil, ErrUnknownConfigID
  22. }
  23. return creator(), nil
  24. }
  25. type ConfigLoader interface {
  26. Load([]byte) (interface{}, string, error)
  27. LoadWithID([]byte, string) (interface{}, error)
  28. }