From 5946215dfaf523028684a8a384a1e1fc79355cf9 Mon Sep 17 00:00:00 2001 From: ginokent <29125616+ginokent@users.noreply.github.com> Date: Tue, 13 Aug 2024 03:13:06 +0900 Subject: [PATCH] fix: Update QueryerContext interface --- internal/arcgen/lang/go/generate.go | 4 +- .../go/{generate_crud.go => generate_orm.go} | 8 +- ..._crud_common.go => generate_orm_common.go} | 112 ++++++++++-------- ..._crud_create.go => generate_orm_create.go} | 0 ..._crud_delete.go => generate_orm_delete.go} | 0 ...rate_crud_read.go => generate_orm_read.go} | 0 ..._crud_update.go => generate_orm_update.go} | 0 7 files changed, 68 insertions(+), 56 deletions(-) rename internal/arcgen/lang/go/{generate_crud.go => generate_orm.go} (87%) rename internal/arcgen/lang/go/{generate_crud_common.go => generate_orm_common.go} (71%) rename internal/arcgen/lang/go/{generate_crud_create.go => generate_orm_create.go} (100%) rename internal/arcgen/lang/go/{generate_crud_delete.go => generate_orm_delete.go} (100%) rename internal/arcgen/lang/go/{generate_crud_read.go => generate_orm_read.go} (100%) rename internal/arcgen/lang/go/{generate_crud_update.go => generate_orm_update.go} (100%) diff --git a/internal/arcgen/lang/go/generate.go b/internal/arcgen/lang/go/generate.go index 940e421..61e1b57 100644 --- a/internal/arcgen/lang/go/generate.go +++ b/internal/arcgen/lang/go/generate.go @@ -70,7 +70,7 @@ func generate(arcSrcSetSlice ARCSourceSetSlice) error { defer f.Close() crudFiles = append(crudFiles, filename) - if err := fprintCRUD( + if err := fprintORM( f, bytes.NewBuffer(nil), arcSrcSet, @@ -91,7 +91,7 @@ func generate(arcSrcSetSlice ARCSourceSetSlice) error { } defer f.Close() - if err := fprintCRUDCommon(f, bytes.NewBuffer(nil), arcSrcSetSlice, crudFiles); err != nil { + if err := fprintORMCommon(f, bytes.NewBuffer(nil), arcSrcSetSlice, crudFiles); err != nil { return errorz.Errorf("sprint: %w", err) } diff --git a/internal/arcgen/lang/go/generate_crud.go b/internal/arcgen/lang/go/generate_orm.go similarity index 87% rename from internal/arcgen/lang/go/generate_crud.go rename to internal/arcgen/lang/go/generate_orm.go index 077b537..5e7784a 100644 --- a/internal/arcgen/lang/go/generate_crud.go +++ b/internal/arcgen/lang/go/generate_orm.go @@ -16,10 +16,10 @@ import ( "github.com/kunitsucom/arcgen/pkg/errors" ) -func fprintCRUD(osFile osFile, buf buffer, arcSrcSet *ARCSourceSet) error { - content, err := generateCRUDFileContent(buf, arcSrcSet) +func fprintORM(osFile osFile, buf buffer, arcSrcSet *ARCSourceSet) error { + content, err := generateORMFileContent(buf, arcSrcSet) if err != nil { - return errorz.Errorf("generateCRUDFileContent: %w", err) + return errorz.Errorf("generateORMFileContent: %w", err) } // write to file @@ -31,7 +31,7 @@ func fprintCRUD(osFile osFile, buf buffer, arcSrcSet *ARCSourceSet) error { } //nolint:funlen -func generateCRUDFileContent(buf buffer, arcSrcSet *ARCSourceSet) (string, error) { +func generateORMFileContent(buf buffer, arcSrcSet *ARCSourceSet) (string, error) { if arcSrcSet == nil || arcSrcSet.PackageName == "" { return "", errors.ErrInvalidSourceSet } diff --git a/internal/arcgen/lang/go/generate_crud_common.go b/internal/arcgen/lang/go/generate_orm_common.go similarity index 71% rename from internal/arcgen/lang/go/generate_crud_common.go rename to internal/arcgen/lang/go/generate_orm_common.go index 6ab094f..ccf6536 100644 --- a/internal/arcgen/lang/go/generate_crud_common.go +++ b/internal/arcgen/lang/go/generate_orm_common.go @@ -16,10 +16,10 @@ import ( "github.com/kunitsucom/arcgen/internal/config" ) -func fprintCRUDCommon(osFile osFile, buf buffer, arcSrcSetSlice ARCSourceSetSlice, crudFiles []string) error { - content, err := generateCRUDCommonFileContent(buf, arcSrcSetSlice, crudFiles) +func fprintORMCommon(osFile osFile, buf buffer, arcSrcSetSlice ARCSourceSetSlice, crudFiles []string) error { + content, err := generateORMCommonFileContent(buf, arcSrcSetSlice, crudFiles) if err != nil { - return errorz.Errorf("generateCRUDCommonFileContent: %w", err) + return errorz.Errorf("generateORMCommonFileContent: %w", err) } // write to file @@ -37,7 +37,7 @@ const ( ) //nolint:cyclop,funlen,gocognit,maintidx -func generateCRUDCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice, crudFiles []string) (string, error) { +func generateORMCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice, crudFiles []string) (string, error) { astFile := &ast.File{ // package Name: &ast.Ident{ @@ -84,9 +84,10 @@ func generateCRUDCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice, ) // type QueryerContext interface { + // ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) + // PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) // QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) // QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row - // ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) // } astFile.Decls = append(astFile.Decls, &ast.GenDecl{ @@ -96,58 +97,69 @@ func generateCRUDCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice, // Assign: token.Pos(1), Name: &ast.Ident{Name: queryerContextTypeName}, Type: &ast.InterfaceType{ - Methods: &ast.FieldList{ - List: []*ast.Field{ - { - Names: []*ast.Ident{{Name: "QueryContext"}}, - Type: &ast.FuncType{ - Params: &ast.FieldList{List: []*ast.Field{ - {Names: []*ast.Ident{{Name: "ctx"}}, Type: &ast.Ident{Name: "context.Context"}}, - {Names: []*ast.Ident{{Name: "query"}}, Type: &ast.Ident{Name: "string"}}, - {Names: []*ast.Ident{{Name: "args"}}, Type: &ast.Ellipsis{Elt: &ast.Ident{Name: "interface{}"}}}, - }}, - Results: &ast.FieldList{List: []*ast.Field{ - {Type: &ast.StarExpr{X: &ast.SelectorExpr{X: &ast.Ident{Name: "sql"}, Sel: &ast.Ident{Name: "Rows"}}}}, - {Type: &ast.Ident{Name: "error"}}, - }}, - }, + Methods: &ast.FieldList{List: []*ast.Field{ + { + Names: []*ast.Ident{{Name: "ExecContext"}}, + Type: &ast.FuncType{ + Params: &ast.FieldList{List: []*ast.Field{ + {Names: []*ast.Ident{{Name: "ctx"}}, Type: &ast.Ident{Name: "context.Context"}}, + {Names: []*ast.Ident{{Name: "query"}}, Type: &ast.Ident{Name: "string"}}, + {Names: []*ast.Ident{{Name: "args"}}, Type: &ast.Ellipsis{Elt: &ast.Ident{Name: "interface{}"}}}, + }}, + Results: &ast.FieldList{List: []*ast.Field{ + {Type: &ast.SelectorExpr{X: &ast.Ident{Name: "sql"}, Sel: &ast.Ident{Name: "Result"}}}, + {Type: &ast.Ident{Name: "error"}}, + }}, }, - { - Names: []*ast.Ident{{Name: "QueryRowContext"}}, - Type: &ast.FuncType{ - Params: &ast.FieldList{List: []*ast.Field{ - {Names: []*ast.Ident{{Name: "ctx"}}, Type: &ast.Ident{Name: "context.Context"}}, - {Names: []*ast.Ident{{Name: "query"}}, Type: &ast.Ident{Name: "string"}}, - {Names: []*ast.Ident{{Name: "args"}}, Type: &ast.Ellipsis{Elt: &ast.Ident{Name: "interface{}"}}}, - }}, - Results: &ast.FieldList{List: []*ast.Field{ - {Type: &ast.StarExpr{X: &ast.SelectorExpr{X: &ast.Ident{Name: "sql"}, Sel: &ast.Ident{Name: "Row"}}}}, - }}, - }, + }, + { + Names: []*ast.Ident{{Name: "PrepareContext"}}, + Type: &ast.FuncType{ + Params: &ast.FieldList{List: []*ast.Field{ + {Names: []*ast.Ident{{Name: "ctx"}}, Type: &ast.Ident{Name: "context.Context"}}, + {Names: []*ast.Ident{{Name: "query"}}, Type: &ast.Ident{Name: "string"}}, + }}, + Results: &ast.FieldList{List: []*ast.Field{ + {Type: &ast.StarExpr{X: &ast.SelectorExpr{X: &ast.Ident{Name: "sql"}, Sel: &ast.Ident{Name: "Stmt"}}}}, + {Type: &ast.Ident{Name: "error"}}, + }}, }, - { - Names: []*ast.Ident{{Name: "ExecContext"}}, - Type: &ast.FuncType{ - Params: &ast.FieldList{List: []*ast.Field{ - {Names: []*ast.Ident{{Name: "ctx"}}, Type: &ast.Ident{Name: "context.Context"}}, - {Names: []*ast.Ident{{Name: "query"}}, Type: &ast.Ident{Name: "string"}}, - {Names: []*ast.Ident{{Name: "args"}}, Type: &ast.Ellipsis{Elt: &ast.Ident{Name: "interface{}"}}}, - }}, - Results: &ast.FieldList{List: []*ast.Field{ - {Type: &ast.SelectorExpr{X: &ast.Ident{Name: "sql"}, Sel: &ast.Ident{Name: "Result"}}}, - {Type: &ast.Ident{Name: "error"}}, - }}, - }, + }, + { + Names: []*ast.Ident{{Name: "QueryContext"}}, + Type: &ast.FuncType{ + Params: &ast.FieldList{List: []*ast.Field{ + {Names: []*ast.Ident{{Name: "ctx"}}, Type: &ast.Ident{Name: "context.Context"}}, + {Names: []*ast.Ident{{Name: "query"}}, Type: &ast.Ident{Name: "string"}}, + {Names: []*ast.Ident{{Name: "args"}}, Type: &ast.Ellipsis{Elt: &ast.Ident{Name: "interface{}"}}}, + }}, + Results: &ast.FieldList{List: []*ast.Field{ + {Type: &ast.StarExpr{X: &ast.SelectorExpr{X: &ast.Ident{Name: "sql"}, Sel: &ast.Ident{Name: "Rows"}}}}, + {Type: &ast.Ident{Name: "error"}}, + }}, }, }, - }, + { + Names: []*ast.Ident{{Name: "QueryRowContext"}}, + Type: &ast.FuncType{ + Params: &ast.FieldList{List: []*ast.Field{ + {Names: []*ast.Ident{{Name: "ctx"}}, Type: &ast.Ident{Name: "context.Context"}}, + {Names: []*ast.Ident{{Name: "query"}}, Type: &ast.Ident{Name: "string"}}, + {Names: []*ast.Ident{{Name: "args"}}, Type: &ast.Ellipsis{Elt: &ast.Ident{Name: "interface{}"}}}, + }}, + Results: &ast.FieldList{List: []*ast.Field{ + {Type: &ast.StarExpr{X: &ast.SelectorExpr{X: &ast.Ident{Name: "sql"}, Sel: &ast.Ident{Name: "Row"}}}}, + }}, + }, + }, + }}, }, }, }, }, ) - // type _CRUD struct { + // type _ORM struct { // } astFile.Decls = append(astFile.Decls, &ast.GenDecl{ @@ -221,7 +233,7 @@ func generateCRUDCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice, }, ) - // type CRUD interface { + // type ORM interface { // Create{StructName}(ctx context.Context, queryerContext QueryerContext, s *{Struct}) error // ... // } @@ -271,8 +283,8 @@ func generateCRUDCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice, }, ) - // func NewCRUD() CRUD { - // return &_CRUD{} + // func NewORM() ORM { + // return &_ORM{} // } astFile.Decls = append(astFile.Decls, &ast.FuncDecl{ diff --git a/internal/arcgen/lang/go/generate_crud_create.go b/internal/arcgen/lang/go/generate_orm_create.go similarity index 100% rename from internal/arcgen/lang/go/generate_crud_create.go rename to internal/arcgen/lang/go/generate_orm_create.go diff --git a/internal/arcgen/lang/go/generate_crud_delete.go b/internal/arcgen/lang/go/generate_orm_delete.go similarity index 100% rename from internal/arcgen/lang/go/generate_crud_delete.go rename to internal/arcgen/lang/go/generate_orm_delete.go diff --git a/internal/arcgen/lang/go/generate_crud_read.go b/internal/arcgen/lang/go/generate_orm_read.go similarity index 100% rename from internal/arcgen/lang/go/generate_crud_read.go rename to internal/arcgen/lang/go/generate_orm_read.go diff --git a/internal/arcgen/lang/go/generate_crud_update.go b/internal/arcgen/lang/go/generate_orm_update.go similarity index 100% rename from internal/arcgen/lang/go/generate_crud_update.go rename to internal/arcgen/lang/go/generate_orm_update.go