config_cache.go 621 B

12345678910111213141516171819202122232425262728293031
  1. package router
  2. import (
  3. "errors"
  4. )
  5. type ConfigObjectCreator func([]byte) (interface{}, error)
  6. var (
  7. configCache map[string]ConfigObjectCreator
  8. ErrorRouterNotFound = errors.New("Router not found.")
  9. )
  10. func RegisterRouterConfig(strategy string, creator ConfigObjectCreator) error {
  11. // TODO: check strategy
  12. configCache[strategy] = creator
  13. return nil
  14. }
  15. func CreateRouterConfig(strategy string, data []byte) (interface{}, error) {
  16. creator, found := configCache[strategy]
  17. if !found {
  18. return nil, ErrorRouterNotFound
  19. }
  20. return creator(data)
  21. }
  22. func init() {
  23. configCache = make(map[string]ConfigObjectCreator)
  24. }