package database import ( "fmt" "github.com/ClickHouse/clickhouse-go/v2" "strings" ) type Config struct { Clickhouse configCH `yaml:"clickhouse"` } func (cfg Config) Invalidate() error { var err error if err = cfg.Clickhouse.Invalidate(); err != nil { return err } return nil } type configCH struct { Username string `json:"username" yaml:"username"` Password string `json:"password" yaml:"password"` Host string `json:"host" yaml:"host"` Port int `json:"port" yaml:"port"` Database string `json:"database" yaml:"database"` Params params `json:"params" yaml:"params"` } type params struct { Secure bool `json:"secure" yaml:"secure"` Compress string `json:"compress" yaml:"compress"` Timeout int `json:"timeout" yaml:"timeout"` ReadTimeout int `json:"read_timeout" yaml:"read-timeout"` WriteTimeout int `json:"write_timeout" yaml:"write-timeout"` } func (c configCH) Invalidate() error { if strings.TrimSpace(c.Username) == "" { c.Username = "default" } if strings.TrimSpace(c.Host) == "" { c.Host = "localhost" } if c.Port < 1 { c.Port = 9000 } if strings.TrimSpace(c.Database) == "" { return fmt.Errorf("`clickhouse.database` must not be an empty string") } return c.Params.Invalidate() } func (p params) Invalidate() error { if p.Compress == "" { p.Compress = "lz4" } if _, ok := compressionCH[p.Compress]; !ok { return fmt.Errorf("illegal `clickhouse.params.compress`") } if p.Timeout < 1 { return fmt.Errorf("`clickhouse.params.timeout` must not be at least 1") } if p.ReadTimeout < 1 { return fmt.Errorf("`clickhouse.params.read-timeout` must not be at least 1") } if p.WriteTimeout < 1 { return fmt.Errorf("`clickhouse.params.write-timeout` must not be at least 1") } return nil } func (p params) CompressionMethod() clickhouse.CompressionMethod { return compressionCH[p.Compress] }