refactor: create helper function for computing a table's generated type-name
This commit is contained in:
parent
d9df69eccf
commit
a32bf873c9
@ -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 {
|
||||
|
||||
@ -40,7 +40,7 @@ func SchemaFromDB(db *sqlx.DB) Schema {
|
||||
var tables []Table
|
||||
PanicIf(db.Select(&tables, `select name, is_strict, is_without_rowid from tables`))
|
||||
for _, tbl := range tables {
|
||||
tbl.TypeName = textutils.SnakeToCamel(inflection.Singular(tbl.TableName))
|
||||
tbl.TypeName = TypenameFromTablename(tbl.TableName)
|
||||
tbl.TypeIDName = tbl.TypeName + "ID"
|
||||
tbl.VarName = strings.ToLower(string(tbl.TableName[0]))
|
||||
|
||||
@ -56,3 +56,7 @@ func SchemaFromDB(db *sqlx.DB) Schema {
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func TypenameFromTablename(tablename string) string {
|
||||
return textutils.SnakeToCamel(inflection.Singular(tablename))
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user