diff --git a/sqlconnect/config/config.go b/sqlconnect/config/config.go new file mode 100644 index 0000000..b282be7 --- /dev/null +++ b/sqlconnect/config/config.go @@ -0,0 +1,19 @@ +package config + +import ( + "github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/bigquery" + "github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/databricks" + "github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/mysql" + "github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/postgres" + "github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/snowflake" + "github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/trino" +) + +type ( + BigQuery = bigquery.Config + Databricks = databricks.Config + Mysql = mysql.Config + Postgres = postgres.Config + Snowflake = snowflake.Config + Trino = trino.Config +) diff --git a/sqlconnect/internal/bigquery/config.go b/sqlconnect/internal/bigquery/config.go index 061aa96..27463c0 100644 --- a/sqlconnect/internal/bigquery/config.go +++ b/sqlconnect/internal/bigquery/config.go @@ -13,7 +13,7 @@ type Config struct { UseLegacyMappings bool `json:"useLegacyMappings"` } -func ParseConfig(configJSON json.RawMessage) (config Config, err error) { - err = json.Unmarshal(configJSON, &config) - return +// Parse parses the given JSON into the config +func (c *Config) Parse(configJSON json.RawMessage) error { + return json.Unmarshal(configJSON, c) } diff --git a/sqlconnect/internal/bigquery/db.go b/sqlconnect/internal/bigquery/db.go index 590224d..2d2708b 100644 --- a/sqlconnect/internal/bigquery/db.go +++ b/sqlconnect/internal/bigquery/db.go @@ -22,7 +22,8 @@ const ( // NewDB creates a new client func NewDB(configJSON json.RawMessage) (*DB, error) { - config, err := ParseConfig(configJSON) + var config Config + err := config.Parse(configJSON) if err != nil { return nil, err } diff --git a/sqlconnect/internal/databricks/config.go b/sqlconnect/internal/databricks/config.go index bdefba9..158725f 100644 --- a/sqlconnect/internal/databricks/config.go +++ b/sqlconnect/internal/databricks/config.go @@ -22,14 +22,14 @@ type Config struct { UseLegacyMappings bool `json:"useLegacyMappings"` } -func ParseConfig(configJson json.RawMessage) (config Config, err error) { - err = json.Unmarshal(configJson, &config) +func (c *Config) Parse(configJson json.RawMessage) error { + err := json.Unmarshal(configJson, c) if err != nil { - return + return err } // if catalog is empty from the UI, use default "hive_metastore" - if config.Catalog == "" { - config.Catalog = "hive_metastore" + if c.Catalog == "" { + c.Catalog = "hive_metastore" } - return + return nil } diff --git a/sqlconnect/internal/databricks/db.go b/sqlconnect/internal/databricks/db.go index a69d605..3599622 100644 --- a/sqlconnect/internal/databricks/db.go +++ b/sqlconnect/internal/databricks/db.go @@ -20,7 +20,8 @@ const ( // NewDB creates a new postgres-specific client func NewDB(configJson json.RawMessage) (*DB, error) { - config, err := ParseConfig(configJson) + var config Config + err := config.Parse(configJson) if err != nil { return nil, err } @@ -43,9 +44,6 @@ func NewDB(configJson json.RawMessage) (*DB, error) { } db := sql.OpenDB(connector) - if err != nil { - return nil, err - } db.SetConnMaxIdleTime(config.MaxConnIdleTime) return &DB{ diff --git a/sqlconnect/internal/mysql/config.go b/sqlconnect/internal/mysql/config.go index 65e22f7..187baa3 100644 --- a/sqlconnect/internal/mysql/config.go +++ b/sqlconnect/internal/mysql/config.go @@ -41,13 +41,13 @@ func (c Config) TLS() (string, error) { } } -func ParseConfig(input json.RawMessage) (config Config, err error) { - err = json.Unmarshal(input, &config) +func (c *Config) Parse(input json.RawMessage) error { + err := json.Unmarshal(input, c) if err != nil { - return + return err } - if !config.SkipHostValidation { - err = util.ValidateHost(config.Host) + if !c.SkipHostValidation { + return util.ValidateHost(c.Host) } - return + return nil } diff --git a/sqlconnect/internal/mysql/config_test.go b/sqlconnect/internal/mysql/config_test.go index 34163a4..82eafe2 100644 --- a/sqlconnect/internal/mysql/config_test.go +++ b/sqlconnect/internal/mysql/config_test.go @@ -10,13 +10,14 @@ import ( func TestConfig(t *testing.T) { t.Run("host validation", func(t *testing.T) { - _, err := mysql.ParseConfig([]byte(`{"host": "localhost"}`)) + var config mysql.Config + err := config.Parse([]byte(`{"host": "localhost"}`)) require.Error(t, err, "should not allow localhost") - _, err = mysql.ParseConfig([]byte(`{"host": "127.0.0.1"}`)) + err = config.Parse([]byte(`{"host": "127.0.0.1"}`)) require.Error(t, err, "should not allow 127.0.0.1") - _, err = mysql.ParseConfig([]byte(`{"host": "0.0.0.0"}`)) + err = config.Parse([]byte(`{"host": "0.0.0.0"}`)) require.Error(t, err, "should not allow 0.0.0.0") }) diff --git a/sqlconnect/internal/mysql/db.go b/sqlconnect/internal/mysql/db.go index d3b318b..0737d57 100644 --- a/sqlconnect/internal/mysql/db.go +++ b/sqlconnect/internal/mysql/db.go @@ -19,7 +19,8 @@ const ( // NewDB creates a new postgres-specific client func NewDB(configJSON json.RawMessage) (*DB, error) { - config, err := ParseConfig(configJSON) + var config Config + err := config.Parse(configJSON) if err != nil { return nil, err } diff --git a/sqlconnect/internal/postgres/config.go b/sqlconnect/internal/postgres/config.go index f06da39..a0e6e07 100644 --- a/sqlconnect/internal/postgres/config.go +++ b/sqlconnect/internal/postgres/config.go @@ -34,13 +34,13 @@ func (c Config) ConnectionString() string { return fmt.Sprintf("host=%s port=%d dbname=%s user=%s password=%s sslmode=%s", c.Host, c.Port, c.DBName, c.User, c.Password, sslMode) } -func ParseConfig(input json.RawMessage) (config Config, err error) { - err = json.Unmarshal(input, &config) +func (c *Config) Parse(input json.RawMessage) error { + err := json.Unmarshal(input, c) if err != nil { - return + return err } - if !config.SkipHostValidation { - err = util.ValidateHost(config.Host) + if !c.SkipHostValidation { + return util.ValidateHost(c.Host) } - return + return nil } diff --git a/sqlconnect/internal/postgres/db.go b/sqlconnect/internal/postgres/db.go index 01e0aa7..53f7749 100644 --- a/sqlconnect/internal/postgres/db.go +++ b/sqlconnect/internal/postgres/db.go @@ -18,7 +18,8 @@ const ( // NewDB creates a new postgres-specific client func NewDB(credentialsJSON json.RawMessage) (*DB, error) { - config, err := ParseConfig(credentialsJSON) + var config Config + err := config.Parse(credentialsJSON) if err != nil { return nil, err } diff --git a/sqlconnect/internal/redshift/db.go b/sqlconnect/internal/redshift/db.go index eb95b09..cc50f9a 100644 --- a/sqlconnect/internal/redshift/db.go +++ b/sqlconnect/internal/redshift/db.go @@ -19,7 +19,8 @@ const ( ) func NewDB(credentialsJSON json.RawMessage) (*DB, error) { - config, err := postgres.ParseConfig(credentialsJSON) + var config postgres.Config + err := config.Parse(credentialsJSON) if err != nil { return nil, err } diff --git a/sqlconnect/internal/snowflake/config.go b/sqlconnect/internal/snowflake/config.go index 46cecb7..0495985 100644 --- a/sqlconnect/internal/snowflake/config.go +++ b/sqlconnect/internal/snowflake/config.go @@ -49,7 +49,6 @@ func (c Config) ConnectionString() (dsn string, err error) { return } -func ParseConfig(configJSON json.RawMessage) (config Config, err error) { - err = json.Unmarshal(configJSON, &config) - return +func (c *Config) Parse(configJSON json.RawMessage) error { + return json.Unmarshal(configJSON, c) } diff --git a/sqlconnect/internal/snowflake/db.go b/sqlconnect/internal/snowflake/db.go index 1ade364..953e298 100644 --- a/sqlconnect/internal/snowflake/db.go +++ b/sqlconnect/internal/snowflake/db.go @@ -19,7 +19,8 @@ const ( // NewDB creates a new postgres-specific client func NewDB(configJSON json.RawMessage) (*DB, error) { - config, err := ParseConfig(configJSON) + var config Config + err := config.Parse(configJSON) if err != nil { return nil, err } diff --git a/sqlconnect/internal/trino/config.go b/sqlconnect/internal/trino/config.go index d58f645..728ede7 100644 --- a/sqlconnect/internal/trino/config.go +++ b/sqlconnect/internal/trino/config.go @@ -45,11 +45,10 @@ func (c Config) ConnectionString() (string, error) { return dsn, nil } -func ParseConfig(input json.RawMessage) (config Config, err error) { - err = json.Unmarshal(input, &config) +func (c *Config) Parse(input json.RawMessage) error { + err := json.Unmarshal(input, c) if err != nil { - return + return err } - err = util.ValidateHost(config.Host) - return + return util.ValidateHost(c.Host) } diff --git a/sqlconnect/internal/trino/db.go b/sqlconnect/internal/trino/db.go index 11de956..0fbdd9d 100644 --- a/sqlconnect/internal/trino/db.go +++ b/sqlconnect/internal/trino/db.go @@ -19,7 +19,8 @@ const ( // NewDB creates a new postgres-specific client func NewDB(configJSON json.RawMessage) (*DB, error) { - config, err := ParseConfig(configJSON) + var config Config + err := config.Parse(configJSON) if err != nil { return nil, err }