Skip to content

Commit

Permalink
NewDialect panics if an unknown dialect is passed
Browse files Browse the repository at this point in the history
The former behavior (returning the default dialect) may return the
default dialect when we actually want an existing dialect but
forgot to import it. This situation should be caught as early as
possible, hence the panic.
  • Loading branch information
cdevienne committed Dec 23, 2016
1 parent eb624b4 commit fbc5a22
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ func NewDialect(driver string) Dialect {
if ok {
return factory()
}
return &DefaultDialect{false}
panic("No such dialect: " + driver)
}

// A DialectFactory is a Dialect Factory
Expand Down
10 changes: 10 additions & 0 deletions dialect_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ type DefaultDialect struct {
escaping bool
}

// NewDefaultDialect instanciate a DefaultDialect
func NewDefaultDialect() Dialect {
return &DefaultDialect{false}
}

// CompileType compiles a type into its DDL
func (d *DefaultDialect) CompileType(t TypeElem) string {
return DefaultCompileType(t, d.SupportsUnsigned())
Expand Down Expand Up @@ -62,3 +67,8 @@ func (d *DefaultDialect) GetCompiler() Compiler {
func (d *DefaultDialect) WrapError(err error) Error {
return Error{Orig: err}
}

func init() {
RegisterDialect("default", NewDefaultDialect)
RegisterDialect("", NewDefaultDialect)
}
6 changes: 6 additions & 0 deletions dialect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ func TestDefaultDialect(t *testing.T) {
qbErr := dialect.WrapError(err)
assert.Equal(t, err, qbErr.Orig)
}

func TestGetDialect(t *testing.T) {
assert.Panics(t, func() {
NewDialect("unknown")
})
}

0 comments on commit fbc5a22

Please sign in to comment.