Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for strfmt.DateTime to datetime mapping to support go-swagger generated models #348

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ _obj
6.out
gorptest.bin
tmp
.idea
4 changes: 4 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ func (m *DbMap) readStructColumns(t reflect.Type) (cols []*ColumnMap, primaryKey
var isAuto bool
var isPK bool
var isNotNull bool
var unique bool
for _, argString := range cArguments[1:] {
argString = strings.TrimSpace(argString)
arg := strings.SplitN(argString, ":", 2)
Expand Down Expand Up @@ -288,6 +289,8 @@ func (m *DbMap) readStructColumns(t reflect.Type) (cols []*ColumnMap, primaryKey
isAuto = true
case "notnull":
isNotNull = true
case "unique":
unique = true
default:
panic(fmt.Sprintf("Unrecognized tag option for field %v: %v", f.Name, arg))
}
Expand Down Expand Up @@ -333,6 +336,7 @@ func (m *DbMap) readStructColumns(t reflect.Type) (cols []*ColumnMap, primaryKey
isAutoIncr: isAuto,
isNotNull: isNotNull,
MaxSize: maxSize,
Unique: unique,
}
if isPK {
primaryKey = append(primaryKey, cm)
Expand Down
7 changes: 4 additions & 3 deletions dialect_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

// Implementation of Dialect for MySQL databases.
type MySQLDialect struct {

// Engine is the storage engine to use "InnoDB" vs "MyISAM" for example
Engine string

Expand Down Expand Up @@ -58,16 +57,18 @@ func (d MySQLDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool)
return "mediumblob"
}
}

switch val.Name() {
case "NullInt64":
return "bigint"
case "NullFloat64":
return "double"
case "NullBool":
return "tinyint"
case "Time":
case "Time", "DateTime":
return "datetime"
case "Date":
return "date"

}

if maxsize < 1 {
Expand Down
2 changes: 2 additions & 0 deletions dialect_mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
. "github.com/onsi/gomega"

"github.com/go-gorp/gorp"
"github.com/go-openapi/strfmt"
)

var _ = Describe("MySQLDialect", func() {
Expand Down Expand Up @@ -61,6 +62,7 @@ var _ = Describe("MySQLDialect", func() {
Entry("NullFloat64", sql.NullFloat64{}, 0, false, "double"),
Entry("NullBool", sql.NullBool{}, 0, false, "tinyint"),
Entry("Time", time.Time{}, 0, false, "datetime"),
Entry("DateTime", strfmt.DateTime{}, 0, false, "datetime"),
Entry("default-size string", "", 0, false, "varchar(255)"),
Entry("sized string", "", 50, false, "varchar(50)"),
Entry("large string", "", 1024, false, "text"),
Expand Down
2 changes: 1 addition & 1 deletion dialect_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (d OracleDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool)
return "double precision"
case "NullBool":
return "boolean"
case "NullTime", "Time":
case "NullTime", "Time", "DateTime":
return "timestamp with time zone"
}

Expand Down
2 changes: 1 addition & 1 deletion dialect_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (d PostgresDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr boo
return "double precision"
case "NullBool":
return "boolean"
case "Time", "NullTime":
case "Time", "NullTime","DateTime":
return "timestamp with time zone"
}

Expand Down
3 changes: 2 additions & 1 deletion dialect_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ func (d SqliteDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool)
return "real"
case "NullBool":
return "integer"
case "Time":
case "Time", "DateTime":
return "datetime"

}

if maxsize < 1 {
Expand Down
2 changes: 1 addition & 1 deletion dialect_sqlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (d SqlServerDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bo
return "float(53)"
case "NullBool":
return "bit"
case "NullTime", "Time":
case "NullTime", "Time","DateTime":
if d.Version == "2005" {
return "datetime"
}
Expand Down