codegen: add helpers
All checks were successful
CI / build-docker (push) Successful in 4s
CI / build-docker-bootstrap (push) Has been skipped
CI / release-test (push) Successful in 15s

This commit is contained in:
2026-01-31 20:35:03 -08:00
parent b8a024a4b9
commit 5cbb657666
3 changed files with 44 additions and 34 deletions

View File

@@ -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() {