refactor: create helper function for computing a table's generated type-name

This commit is contained in:
2025-12-12 22:37:29 -08:00
parent d9df69eccf
commit a32bf873c9
2 changed files with 6 additions and 4 deletions

View File

@@ -7,8 +7,6 @@ import (
"go/token"
"strings"
"github.com/jinzhu/inflection"
"git.offline-twitter.com/offline-labs/gas-stack/pkg/schema"
"git.offline-twitter.com/offline-labs/gas-stack/pkg/textutils"
)
@@ -40,7 +38,7 @@ func GenerateModelAST(table schema.Table) *ast.GenDecl {
if col.IsForeignKey && strings.HasSuffix(col.Name, "_id") {
fields = append(fields, &ast.Field{
Names: []*ast.Ident{ast.NewIdent(textutils.SnakeToCamel(strings.TrimSuffix(col.Name, "_id")) + "ID")},
Type: ast.NewIdent(textutils.SnakeToCamel(inflection.Singular(col.ForeignKeyTargetTable) + "ID")),
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 {