config.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package clickhouse
  2. import (
  3. "fmt"
  4. "git.beejay.kim/tool/service/config"
  5. "strings"
  6. )
  7. type Config struct {
  8. Username string `json:"username" yaml:"username"`
  9. Password string `json:"password" yaml:"password"`
  10. Host string `json:"host" yaml:"host"`
  11. Port int `json:"port" yaml:"port"`
  12. Database string `json:"database" yaml:"database"`
  13. Params params `json:"params" yaml:"params"`
  14. }
  15. func (c Config) Invalidate() error {
  16. if strings.TrimSpace(c.Username) == "" {
  17. return fmt.Errorf("`database.username` must not be an empty string")
  18. }
  19. if strings.TrimSpace(c.Host) == "" {
  20. c.Host = "localhost"
  21. }
  22. if c.Port < 1 {
  23. c.Port = 9000
  24. }
  25. if strings.TrimSpace(c.Database) == "" {
  26. return fmt.Errorf("`database.database` must not be an empty string")
  27. }
  28. return config.Invalidate(c)
  29. }
  30. type params struct {
  31. Timeout int `json:"timeout" yaml:"timeout"`
  32. ReadTimeout int `json:"read_timeout" yaml:"read-timeout"`
  33. WriteTimeout int `json:"write_timeout" yaml:"write-timeout"`
  34. }
  35. func (p params) Invalidate() error {
  36. if p.Timeout < 1 {
  37. return fmt.Errorf("`database.params.timeout` must not be at least 1")
  38. }
  39. if p.ReadTimeout < 1 {
  40. return fmt.Errorf("`database.params.read-timeout` must not be at least 1")
  41. }
  42. if p.WriteTimeout < 1 {
  43. return fmt.Errorf("`database.params.write-timeout` must not be at least 1")
  44. }
  45. return config.Invalidate(&p)
  46. }