Skip to content
Permalink
Browse files

use unexported values for sqlstore.Config struct fields

  • Loading branch information...
dannyvankooten committed Nov 2, 2018
1 parent dc1573c commit 4dec431c4bbeb80b2b13517d41937b5465d4759c
Showing with 36 additions and 35 deletions.
  1. +29 −28 pkg/datastore/sqlstore/config.go
  2. +7 −7 pkg/datastore/sqlstore/config_test.go
@@ -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
@@ -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)
}

0 comments on commit 4dec431

Please sign in to comment.
You can’t perform that action at this time.