package config import ( "fmt" "github.com/ClickHouse/clickhouse-go/v2" "strings" ) var compressionMap = map[string]clickhouse.CompressionMethod{ "none": clickhouse.CompressionNone, "zstd": clickhouse.CompressionZSTD, "lz4": clickhouse.CompressionLZ4, "gzip": clickhouse.CompressionGZIP, "deflate": clickhouse.CompressionDeflate, "br": clickhouse.CompressionBrotli, } type ClickhouseConfig 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 ClickhouseConfig) 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 Invalidate(c) } func (p params) Invalidate() error { if p.Compress == "" { p.Compress = "lz4" } if _, ok := compressionMap[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 Invalidate(p) } func (p params) CompressionMethod() clickhouse.CompressionMethod { return compressionMap[p.Compress] }