codegen: add helpers
This commit is contained in:
@@ -27,14 +27,6 @@ func SQLFieldsConstIdent(tbl schema.Table) *ast.Ident {
|
||||
return ast.NewIdent(strings.ToLower(tbl.GoTypeName) + "SQLFields")
|
||||
}
|
||||
|
||||
func fkFieldName(col schema.Column) string {
|
||||
if col.IsNonCodeTableForeignKey() {
|
||||
return textutils.SnakeToCamel(strings.TrimSuffix(col.Name, "_id")) + "ID"
|
||||
} else {
|
||||
return textutils.SnakeToCamel(col.Name)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------
|
||||
// Generators
|
||||
// ---------------
|
||||
@@ -66,31 +58,12 @@ func GenerateModelAST(table schema.Table) *ast.GenDecl {
|
||||
default:
|
||||
if col.IsNonCodeTableForeignKey() {
|
||||
fields = append(fields, &ast.Field{
|
||||
Names: []*ast.Ident{ast.NewIdent(fkFieldName(col))},
|
||||
Names: []*ast.Ident{ast.NewIdent(col.GoFieldName())},
|
||||
Type: ast.NewIdent(schema.TypenameFromTablename(col.ForeignKeyTargetTable) + "ID"),
|
||||
Tag: &ast.BasicLit{Kind: token.STRING, Value: fmt.Sprintf("`db:\"%s\" json:\"%s\"`", col.Name, col.Name)},
|
||||
})
|
||||
} else {
|
||||
typeName := "string"
|
||||
switch col.Type {
|
||||
case "integer", "int":
|
||||
if strings.HasPrefix(col.Name, "is_") || strings.HasPrefix(col.Name, "has_") {
|
||||
typeName = "bool"
|
||||
} else if strings.HasSuffix(col.Name, "_at") {
|
||||
typeName = "Timestamp"
|
||||
} else {
|
||||
typeName = "int"
|
||||
}
|
||||
case "text":
|
||||
typeName = "string"
|
||||
case "real":
|
||||
typeName = "float32"
|
||||
case "blob":
|
||||
typeName = "[]byte"
|
||||
default:
|
||||
panic("Unrecognized sqlite column type: " + col.Type)
|
||||
}
|
||||
|
||||
typeName := col.GoType()
|
||||
fields = append(fields, &ast.Field{
|
||||
Names: []*ast.Ident{ast.NewIdent(textutils.SnakeToCamel(col.Name))},
|
||||
Type: ast.NewIdent(typeName),
|
||||
@@ -170,7 +143,7 @@ func GenerateSaveItemFunc(tbl schema.Table) *ast.FuncDecl {
|
||||
}
|
||||
hasFks = true
|
||||
|
||||
structFieldName := fkFieldName(col)
|
||||
structFieldName := col.GoFieldName()
|
||||
structField := &ast.SelectorExpr{X: ast.NewIdent(tbl.VarName), Sel: ast.NewIdent(structFieldName)}
|
||||
|
||||
if col.IsNonCodeTableForeignKey() {
|
||||
|
||||
Reference in New Issue
Block a user