conf.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package redis
  2. import "errors"
  3. var (
  4. // ErrEmptyHost is an error that indicates no redis host is set.
  5. ErrEmptyHost = errors.New("empty redis host")
  6. // ErrEmptyType is an error that indicates no redis type is set.
  7. ErrEmptyType = errors.New("empty redis type")
  8. // ErrEmptyKey is an error that indicates no redis key is set.
  9. ErrEmptyKey = errors.New("empty redis key")
  10. )
  11. type (
  12. // A RedisConf is a redis config.
  13. RedisConf struct {
  14. Host string
  15. Type string `json:",default=node,options=node|cluster"`
  16. Pass string `json:",optional"`
  17. }
  18. // A RedisKeyConf is a redis config with key.
  19. RedisKeyConf struct {
  20. RedisConf
  21. Key string `json:",optional"`
  22. }
  23. )
  24. // NewRedis returns a Redis.
  25. func (rc RedisConf) NewRedis() *Redis {
  26. return NewRedis(rc.Host, rc.Type, rc.Pass)
  27. }
  28. // Validate validates the RedisConf.
  29. func (rc RedisConf) Validate() error {
  30. if len(rc.Host) == 0 {
  31. return ErrEmptyHost
  32. }
  33. if len(rc.Type) == 0 {
  34. return ErrEmptyType
  35. }
  36. return nil
  37. }
  38. // Validate validates the RedisKeyConf.
  39. func (rkc RedisKeyConf) Validate() error {
  40. if err := rkc.RedisConf.Validate(); err != nil {
  41. return err
  42. }
  43. if len(rkc.Key) == 0 {
  44. return ErrEmptyKey
  45. }
  46. return nil
  47. }