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

Time optimize #21

Merged
merged 2 commits into from
Mar 19, 2024
Merged
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
19 changes: 16 additions & 3 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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

Expand Down
Loading