config.go 717 B

1234567891011121314151617181920212223242526272829303132
  1. package config
  2. import "fmt"
  3. type Configer interface {
  4. Set(key, val string) error
  5. Get(key string) (interface{}, error)
  6. Int(key string) (int, error)
  7. Int64(key string) (int64, error)
  8. Bool(key string) (bool, error)
  9. String(key string) (string, error)
  10. Float64(key string) (float64, error)
  11. RawData() []byte
  12. }
  13. type Config interface {
  14. ParseFile(file string) (Configer, error)
  15. }
  16. var adapters = make(map[string]Config)
  17. func Register(adapterName string, adapter Config) {
  18. adapters[adapterName] = adapter
  19. }
  20. func NewConfig(adapterName, file string) (Configer, error) {
  21. adapter, ok := adapters[adapterName]
  22. if !ok {
  23. return nil, fmt.Errorf("Config:%s not exist", adapterName)
  24. }
  25. return adapter.ParseFile(file)
  26. }