Skip to content

Commit

Permalink
feat: add thea ability to set DatabaseConfig fields with setter methods
Browse files Browse the repository at this point in the history
  • Loading branch information
RiccardoM authored and MonikaCat committed Jan 19, 2024
1 parent 4e7cf42 commit f7c289b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 36 deletions.
95 changes: 70 additions & 25 deletions database/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,102 @@ 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)
}
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(
Expand Down
15 changes: 4 additions & 11 deletions database/postgresql/postgresql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit f7c289b

Please sign in to comment.