From 60d864af7d5cc905fcb015cf2c4cc61fdf2e4e42 Mon Sep 17 00:00:00 2001 From: XuShuo Date: Fri, 2 Feb 2024 16:46:11 +0800 Subject: [PATCH] feat: add time range notice. (there is a sql execute err when insert using time.Time{} due out of range) --- generator/generator.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/generator/generator.go b/generator/generator.go index b6db35c..c89ed36 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -71,7 +71,7 @@ func convertToExportedIdentifier(s string, forceCases []string) string { return result } -func getType(fieldDescriptor fieldDescriptor) (goType string, fieldClass string, err error) { +func getType(fieldDescriptor fieldDescriptor) (goType string, fieldClass string, fieldComment string, err error) { switch strings.ToLower(fieldDescriptor.Type) { case "tinyint": goType = "int8" @@ -103,10 +103,20 @@ func getType(fieldDescriptor fieldDescriptor) (goType string, fieldClass string, // TODO: Switch to specific type instead of interface. goType = "[]interface{}" fieldClass = "ArrayField" - case "datetime", "timestamp": + case "timestamp": if !timeAsString { goType = "time.Time" fieldClass = "DateField" + fieldComment = "NOTICE: the range of timestamp is [1970-01-01 08:00:01, 2038-01-19 11:14:07]" + } else { + goType = "string" + fieldClass = "StringField" + } + case "datetime": + if !timeAsString { + goType = "time.Time" + fieldClass = "DateField" + fieldComment = "NOTICE: the range of datetime is [0000-01-01 00:00:00, 2038-01-19 11:14:07]" } else { goType = "string" fieldClass = "StringField" @@ -332,7 +342,7 @@ func generateTable(schemaFetcher schemaFetcher, tableName string, forceCases []s for _, fieldDescriptor := range fieldDescriptors { goName := convertToExportedIdentifier(fieldDescriptor.Name, forceCases) - goType, fieldClass, err := getType(fieldDescriptor) + goType, fieldClass, typeComment, err := getType(fieldDescriptor) if err != nil { return "", err } @@ -343,6 +353,9 @@ func generateTable(schemaFetcher schemaFetcher, tableName string, forceCases []s if fieldDescriptor.Comment != "" { commentLine = "\t// " + strings.ReplaceAll(fieldDescriptor.Comment, "\n", " ") + "\n" } + if typeComment != "" { + commentLine = "\t// " + typeComment + "\n" + } fieldStructName := strings.ToLower(replaceTypeSpace(fieldDescriptor.Type)) + "_" + className + "_" + goName