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

Distinguish custom types #6083

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions schema/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Field struct {
BindNames []string
DataType DataType
GORMDataType DataType
CustomDataType DataType
a631807682 marked this conversation as resolved.
Show resolved Hide resolved
PrimaryKey bool
AutoIncrement bool
AutoIncrementIncrement int64
Expand Down Expand Up @@ -310,6 +311,7 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
default:
field.DataType = DataType(val)
}
field.CustomDataType = DataType(val)
}

if field.Size == 0 {
Expand Down
26 changes: 26 additions & 0 deletions schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,38 @@ import (
"strings"
"sync"
"testing"
"time"

"gorm.io/gorm"
"gorm.io/gorm/schema"
"gorm.io/gorm/utils/tests"
)

func TestParseCustomTypeSchema(t *testing.T) {
type Product struct {
Date time.Time `gorm:"type:date"`
Time time.Time `gorm:"type:time"`
DateTime time.Time
}
product, err := schema.Parse(&Product{}, &sync.Map{}, schema.NamingStrategy{})
if err != nil {
t.Fatalf("failed to parse product, got err %v", err)
}

fields := []schema.Field{
{Name: "Date", DBName: "date", BindNames: []string{"Date"}, DataType: "date", CustomDataType: "date", Tag: `gorm:"type:date"`, TagSettings: map[string]string{"TYPE": "date"}},
{Name: "Time", DBName: "time", BindNames: []string{"Time"}, DataType: schema.Time, CustomDataType: "time", Tag: `gorm:"type:time"`, TagSettings: map[string]string{"TYPE": "time"}},
{Name: "DateTime", DBName: "date_time", BindNames: []string{"DateTime"}, DataType: schema.Time},
}
for i := range fields {
checkSchemaField(t, product, &fields[i], func(field *schema.Field) {
field.Creatable = true
field.Updatable = true
field.Readable = true
})
}
}

func TestParseSchema(t *testing.T) {
user, err := schema.Parse(&tests.User{}, &sync.Map{}, schema.NamingStrategy{})
if err != nil {
Expand Down