Skip to content

Commit

Permalink
use unexported values for sqlstore.Config struct fields
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvankooten committed Nov 2, 2018
1 parent dc1573c commit 4dec431
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
57 changes: 29 additions & 28 deletions pkg/datastore/sqlstore/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,62 @@ import (
)

type Config struct {
URL string `default:""`
Driver string `default:"sqlite3"`
Host string `default:""`
User string `default:""`
Password string `default:""`
Name string `default:"fathom.db"`
SSLMode string `default:""`
url string `default:""`
host string `default:""`
user string `default:""`
password string `default:""`
name string `default:"fathom.db"`
sslmode string `default:""`
}

func (c *Config) DSN() string {
var dsn string

// if FATHOM_DATABASE_URL was set, use that
// this relies on the user to set the appropriate parameters, eg ?parseTime=true when using MySQL
if c.URL != "" {
return c.URL
if c.url != "" {
return c.url
}

// otherwise, generate from individual fields
switch c.Driver {
case POSTGRES:
params := map[string]string{
"host": c.Host,
"dbname": c.Name,
"user": c.User,
"password": c.Password,
"sslmode": c.SSLMode,
}

for k, v := range params {
if v == "" {
continue
}

dsn = dsn + k + "=" + v + " "
if c.host != "" {
dsn += " host=" + c.host
}
if c.name != "" {
dsn += " dbname=" + c.name
}
if c.user != "" {
dsn += " user=" + c.user
}
if c.password != "" {
dsn += " password=" + c.password
}
if c.sslmode != "" {
dsn += " sslmode=" + c.sslmode
}

dsn = strings.TrimSpace(dsn)
case MYSQL:
mc := mysql.NewConfig()
mc.User = c.User
mc.Passwd = c.Password
mc.Addr = c.Host
mc.User = c.user
mc.Passwd = c.password
mc.Addr = c.host
mc.Net = "tcp"
mc.DBName = c.Name
mc.DBName = c.name
mc.Params = map[string]string{
"parseTime": "true",
"loc": "Local",
}
if c.SSLMode != "" {
mc.Params["tls"] = c.SSLMode
if c.sslmode != "" {
mc.Params["tls"] = c.sslmode
}
dsn = mc.FormatDSN()
case SQLITE:
dsn = c.Name + "?_loc=auto&_busy_timeout=5000"
dsn = c.name + "?_loc=auto&_busy_timeout=5000"
}

return dsn
Expand Down
14 changes: 7 additions & 7 deletions pkg/datastore/sqlstore/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ import (
func TestConfigDSN(t *testing.T) {
c := Config{
Driver: "postgres",
User: "john",
Password: "foo",
user: "john",
password: "foo",
}
e := fmt.Sprintf("user=%s password=%s", c.User, c.Password)
e := fmt.Sprintf("user=%s password=%s", c.user, c.password)
if v := c.DSN(); v != e {
t.Errorf("Invalid DSN. Expected %s, got %s", e, v)
}

c = Config{
Driver: "postgres",
User: "john",
Password: "foo",
SSLMode: "disable",
user: "john",
password: "foo",
sslmode: "disable",
}
e = fmt.Sprintf("user=%s password=%s sslmode=%s", c.User, c.Password, c.SSLMode)
e = fmt.Sprintf("user=%s password=%s sslmode=%s", c.user, c.password, c.sslmode)
if v := c.DSN(); v != e {
t.Errorf("Invalid DSN. Expected %s, got %s", e, v)
}
Expand Down

0 comments on commit 4dec431

Please sign in to comment.