From b16a9b4388d05bece21a95df9d932a920a6b8eb6 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Wed, 31 May 2023 10:06:04 -0500 Subject: [PATCH] feat: add thea ability to set DatabaseConfig fields with setter methods --- database/config/config.go | 95 +++++++++++++++++++------- database/postgresql/postgresql_test.go | 15 ++-- 2 files changed, 74 insertions(+), 36 deletions(-) diff --git a/database/config/config.go b/database/config/config.go index e4b29856..bbead4c4 100644 --- a/database/config/config.go +++ b/database/config/config.go @@ -14,7 +14,70 @@ type Config struct { SSLKey string `yaml:"ssl_key"` } -func (c *Config) getURL() *url.URL { +func NewDatabaseConfig( + url, sslModeEnable, sslRootCert, sslCert, sslKey string, + maxOpenConnections int, maxIdleConnections int, + partitionSize int64, batchSize int64, +) Config { + return Config{ + URL: url, + MaxOpenConnections: maxOpenConnections, + MaxIdleConnections: maxIdleConnections, + PartitionSize: partitionSize, + PartitionBatchSize: batchSize, + SSLModeEnable: sslModeEnable, + SSLRootCert: sslRootCert, + SSLCert: sslCert, + SSLKey: sslKey, + } +} + +func (c Config) WithUrl(url string) Config { + c.URL = url + return c +} + +func (c Config) WithMaxOpenConnections(maxOpenConnections int) Config { + c.MaxOpenConnections = maxOpenConnections + return c +} + +func (c Config) WithMaxIdleConnections(maxIdleConnections int) Config { + c.MaxIdleConnections = maxIdleConnections + return c +} + +func (c Config) WithPartitionSize(partitionSize int64) Config { + c.PartitionSize = partitionSize + return c +} + +func (c Config) WithPartitionBatchSize(partitionBatchSize int64) Config { + c.PartitionBatchSize = partitionBatchSize + return c +} + +func (c Config) WithSSLModeEnable(sslModeEnable string) Config { + c.SSLModeEnable = sslModeEnable + return c +} + +func (c Config) WithSSLRootCert(sslRootCert string) Config { + c.SSLRootCert = sslRootCert + return c +} + +func (c Config) WithSSLCert(sslCert string) Config { + c.SSLCert = sslCert + return c +} + +func (c Config) WithSSLKey(sslKey string) Config { + c.SSLKey = sslKey + return c +} + +func (c Config) getURL() *url.URL { parsedURL, err := url.Parse(c.URL) if err != nil { panic(err) @@ -22,49 +85,31 @@ func (c *Config) getURL() *url.URL { return parsedURL } -func (c *Config) GetUser() string { +func (c Config) GetUser() string { return c.getURL().User.Username() } -func (c *Config) GetPassword() string { +func (c Config) GetPassword() string { password, _ := c.getURL().User.Password() return password } -func (c *Config) GetHost() string { +func (c Config) GetHost() string { return c.getURL().Host } -func (c *Config) GetPort() string { +func (c Config) GetPort() string { return c.getURL().Port() } -func (c *Config) GetSchema() string { +func (c Config) GetSchema() string { return c.getURL().Query().Get("search_path") } -func (c *Config) GetSSLMode() string { +func (c Config) GetSSLMode() string { return c.getURL().Query().Get("sslmode") } -func NewDatabaseConfig( - url, sslModeEnable, sslRootCert, sslCert, sslKey string, - maxOpenConnections int, maxIdleConnections int, - partitionSize int64, batchSize int64, -) Config { - return Config{ - URL: url, - MaxOpenConnections: maxOpenConnections, - MaxIdleConnections: maxIdleConnections, - PartitionSize: partitionSize, - PartitionBatchSize: batchSize, - SSLModeEnable: sslModeEnable, - SSLRootCert: sslRootCert, - SSLCert: sslCert, - SSLKey: sslKey, - } -} - // DefaultDatabaseConfig returns the default instance of Config func DefaultDatabaseConfig() Config { return NewDatabaseConfig( diff --git a/database/postgresql/postgresql_test.go b/database/postgresql/postgresql_test.go index baae6ea7..b2e322d3 100644 --- a/database/postgresql/postgresql_test.go +++ b/database/postgresql/postgresql_test.go @@ -31,18 +31,11 @@ func (suite *DbTestSuite) SetupTest() { // Create the codec codec := simapp.MakeTestEncodingConfig() + // Build the database config + dbCfg := databaseconfig.DefaultDatabaseConfig(). + WithUrl("postgres://bdjuno:password@localhost:6433/bdjuno?sslmode=disable&search_path=public") + // Build the database - dbCfg := databaseconfig.NewDatabaseConfig( - "postgres://bdjuno:password@localhost:6433/bdjuno?sslmode=disable&search_path=public", - "false", - "", - "", - "", - -1, - -1, - 100000, - 100, - ) db, err := postgres.Builder(database.NewContext(dbCfg, &codec, logging.DefaultLogger())) suite.Require().NoError(err)