@@ -31,16 +31,17 @@ func fprintCRUDCommon(osFile osFile, buf buffer, arcSrcSetSlice ARCSourceSetSlic
3131}
3232
3333const (
34- sqlQueryerContextVarName = "sqlContext"
35- sqlQueryerContextTypeName = "sqlQueryerContext"
34+ importName = "orm"
35+ queryerContextVarName = "queryerContext"
36+ queryerContextTypeName = "QueryerContext"
3637)
3738
3839//nolint:cyclop,funlen,gocognit,maintidx
3940func generateCRUDCommonFileContent (buf buffer , arcSrcSetSlice ARCSourceSetSlice , crudFiles []string ) (string , error ) {
4041 astFile := & ast.File {
4142 // package
4243 Name : & ast.Ident {
43- Name : config .GoCRUDPackageName (),
44+ Name : config .GoORMPackageName (),
4445 },
4546 // methods
4647 Decls : []ast.Decl {},
@@ -60,7 +61,7 @@ func generateCRUDCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice,
6061 // "database/sql"
6162 // "log/slog"
6263 //
63- // dao "path/to/your/dao "
64+ // orm "path/to/your/orm "
6465 // )
6566 & ast.GenDecl {
6667 Tok : token .IMPORT ,
@@ -75,14 +76,14 @@ func generateCRUDCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice,
7576 Path : & ast.BasicLit {Kind : token .STRING , Value : strconv .Quote ("log/slog" )},
7677 },
7778 & ast.ImportSpec {
78- Name : & ast.Ident {Name : "dao" },
79+ Name : & ast.Ident {Name : importName },
7980 Path : & ast.BasicLit {Kind : token .STRING , Value : strconv .Quote (structPackagePath )},
8081 },
8182 },
8283 },
8384 )
8485
85- // type sqlQueryerContext interface {
86+ // type QueryerContext interface {
8687 // QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
8788 // QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
8889 // ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
@@ -93,7 +94,7 @@ func generateCRUDCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice,
9394 Specs : []ast.Spec {
9495 & ast.TypeSpec {
9596 // Assign: token.Pos(1),
96- Name : & ast.Ident {Name : sqlQueryerContextTypeName },
97+ Name : & ast.Ident {Name : queryerContextTypeName },
9798 Type : & ast.InterfaceType {
9899 Methods : & ast.FieldList {
99100 List : []* ast.Field {
@@ -153,7 +154,7 @@ func generateCRUDCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice,
153154 Tok : token .TYPE ,
154155 Specs : []ast.Spec {
155156 & ast.TypeSpec {
156- Name : & ast.Ident {Name : config .GoCRUDTypeNameUnexported ()},
157+ Name : & ast.Ident {Name : config .GoORMStructName ()},
157158 Type : & ast.StructType {Fields : & ast.FieldList {}},
158159 },
159160 },
@@ -221,7 +222,7 @@ func generateCRUDCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice,
221222 )
222223
223224 // type CRUD interface {
224- // Create{StructName}(ctx context.Context, sqlQueryer sqlQueryerContext , s *{Struct}) error
225+ // Create{StructName}(ctx context.Context, queryerContext QueryerContext , s *{Struct}) error
225226 // ...
226227 // }
227228 methods := make ([]* ast.Field , 0 )
@@ -241,7 +242,7 @@ func generateCRUDCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice,
241242 if n .Recv != nil && len (n .Recv .List ) > 0 {
242243 if t , ok := n .Recv .List [0 ].Type .(* ast.StarExpr ); ok {
243244 if ident , ok := t .X .(* ast.Ident ); ok {
244- if ident .Name == config .GoCRUDTypeNameUnexported () {
245+ if ident .Name == config .GoORMStructName () {
245246 methods = append (methods , & ast.Field {
246247 Names : []* ast.Ident {{Name : n .Name .Name }},
247248 Type : n .Type ,
@@ -261,7 +262,7 @@ func generateCRUDCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice,
261262 Tok : token .TYPE ,
262263 Specs : []ast.Spec {
263264 & ast.TypeSpec {
264- Name : & ast.Ident {Name : config .GoCRUDTypeName ()},
265+ Name : & ast.Ident {Name : config .GoORMTypeName ()},
265266 Type : & ast.InterfaceType {
266267 Methods : & ast.FieldList {List : methods },
267268 },
@@ -275,9 +276,9 @@ func generateCRUDCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice,
275276 // }
276277 astFile .Decls = append (astFile .Decls ,
277278 & ast.FuncDecl {
278- Name : & ast.Ident {Name : "New" + config .GoCRUDTypeName ()},
279- Type : & ast.FuncType {Results : & ast.FieldList {List : []* ast.Field {{Type : & ast.Ident {Name : config .GoCRUDTypeName ()}}}}},
280- Body : & ast.BlockStmt {List : []ast.Stmt {& ast.ReturnStmt {Results : []ast.Expr {& ast.UnaryExpr {Op : token .AND , X : & ast.Ident {Name : config .GoCRUDTypeNameUnexported () + "{}" }}}}}},
279+ Name : & ast.Ident {Name : "New" + config .GoORMTypeName ()},
280+ Type : & ast.FuncType {Results : & ast.FieldList {List : []* ast.Field {{Type : & ast.Ident {Name : config .GoORMTypeName ()}}}}},
281+ Body : & ast.BlockStmt {List : []ast.Stmt {& ast.ReturnStmt {Results : []ast.Expr {& ast.UnaryExpr {Op : token .AND , X : & ast.Ident {Name : config .GoORMStructName () + "{}" }}}}}},
281282 },
282283 )
283284
0 commit comments