Skip to content

Commit

Permalink
fix: Update QueryerContext interface (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent committed Aug 12, 2024
2 parents 14c5359 + 5946215 commit 344f34c
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 56 deletions.
4 changes: 2 additions & 2 deletions internal/arcgen/lang/go/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand Down Expand Up @@ -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
// ...
// }
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 344f34c

Please sign in to comment.