Skip to content

Commit

Permalink
Merge pull request #21 from VarusHsu/time_optimize
Browse files Browse the repository at this point in the history
Time optimize
  • Loading branch information
lqs authored Mar 19, 2024
2 parents c2867d8 + 1d969e2 commit aa857a1
Showing 1 changed file with 16 additions and 3 deletions.
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

0 comments on commit aa857a1

Please sign in to comment.