diff --git a/internal/arcgen/lang/go/dialect.go b/internal/arcgen/lang/go/dialect.go new file mode 100644 index 0000000..b017e14 --- /dev/null +++ b/internal/arcgen/lang/go/dialect.go @@ -0,0 +1,94 @@ +package arcgengo + +import ( + "strconv" + "strings" + + "github.com/kunitsucom/arcgen/internal/config" +) + +func columnValuesPlaceholder(columns []string) string { + switch config.Dialect() { + case "mysql", "sqlite3": + // ?, ?, ?, ... + return "?" + strings.Repeat(", ?", len(columns)-1) + case "postgres", "cockroach": + // $1, $2, $3, ... + var s strings.Builder + s.WriteString("$1") + for i := 2; i <= len(columns); i++ { + s.WriteString(", $") + s.WriteString(strconv.Itoa(i)) + } + return s.String() + case "spanner": + // @column_1, @column_2, @column_3, ... + var s strings.Builder + s.WriteString("@" + columns[0]) + for i := 2; i <= len(columns); i++ { + s.WriteString(", @") + s.WriteString(columns[i-1]) + } + return s.String() + case "oracle": + // :column_1, :column_2, :column_3, ... + var s strings.Builder + s.WriteString(":" + columns[0]) + for i := 2; i <= len(columns); i++ { + s.WriteString(", :") + s.WriteString(columns[i-1]) + } + return s.String() + default: + // ?, ?, ?, ... + return "?" + strings.Repeat(", ?", len(columns)-1) + } +} + +//nolint:unparam,cyclop +func whereColumnsPlaceholder(columns []string, op string) string { + switch config.Dialect() { + case "mysql", "sqlite3": + // column1 = ? AND column2 = ? AND column3 = ... + return strings.Join(columns, " = ? "+op+" ") + " = ?" + case "postgres", "cockroach": + // column1 = $1 AND column2 = $2 AND column3 = ... + var s strings.Builder + for i, column := range columns { + if i > 0 { + s.WriteString(" " + op + " ") + } + s.WriteString(column) + s.WriteString(" = $") + s.WriteString(strconv.Itoa(i + 1)) + } + return s.String() + case "spanner": + // column1 = @column_1 AND column2 = @column_2 AND column3 = ... + var s strings.Builder + for i, column := range columns { + if i > 0 { + s.WriteString(" " + op + " ") + } + s.WriteString(column) + s.WriteString(" = @") + s.WriteString(column) + } + return s.String() + case "oracle": + // column1 = :column_1 AND column2 = :column_2 AND column3 = ... + var s strings.Builder + for i, column := range columns { + if i > 0 { + s.WriteString(" " + op + " ") + } + s.WriteString(column) + s.WriteString(" = :") + s.WriteString(column) + } + return s.String() + default: + // column1 = ? AND column2 = ? AND column3 = ... + return strings.Join(columns, " = ? "+op+" ") + " = ?" + } +} diff --git a/internal/arcgen/lang/go/generate.go b/internal/arcgen/lang/go/generate.go index db0d6d2..940e421 100644 --- a/internal/arcgen/lang/go/generate.go +++ b/internal/arcgen/lang/go/generate.go @@ -54,7 +54,7 @@ func generate(arcSrcSetSlice ARCSourceSetSlice) error { } } - if config.GenerateGoCRUDPackage() { + if config.GenerateGoORMPackage() { crudFileExt := ".crud" + genFileExt crudFiles := make([]string, 0) @@ -62,7 +62,7 @@ func generate(arcSrcSetSlice ARCSourceSetSlice) error { // closure for defer if err := func() error { filePathWithoutExt := strings.TrimSuffix(filepath.Base(arcSrcSet.Filename), fileExt) - filename := filepath.Join(config.GoCRUDPackagePath(), filePathWithoutExt+crudFileExt) + filename := filepath.Join(config.GoORMPackagePath(), filePathWithoutExt+crudFileExt) f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, rw_r__r__) if err != nil { return errorz.Errorf("os.OpenFile: %w", err) @@ -84,7 +84,7 @@ func generate(arcSrcSetSlice ARCSourceSetSlice) error { } if err := func() error { - filename := filepath.Join(config.GoCRUDPackagePath(), "common"+crudFileExt) + filename := filepath.Join(config.GoORMPackagePath(), "common"+crudFileExt) f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, rw_r__r__) if err != nil { return errorz.Errorf("os.OpenFile: %w", err) diff --git a/internal/arcgen/lang/go/generate_crud.go b/internal/arcgen/lang/go/generate_crud.go index cb4ad7e..077b537 100644 --- a/internal/arcgen/lang/go/generate_crud.go +++ b/internal/arcgen/lang/go/generate_crud.go @@ -38,7 +38,7 @@ func generateCRUDFileContent(buf buffer, arcSrcSet *ARCSourceSet) (string, error astFile := &ast.File{ // package Name: &ast.Ident{ - Name: config.GoCRUDPackageName(), + Name: config.GoORMPackageName(), }, // methods Decls: []ast.Decl{}, @@ -55,7 +55,7 @@ func generateCRUDFileContent(buf buffer, arcSrcSet *ARCSourceSet) (string, error // "context" // "fmt" // - // dao "path/to/your/dao" + // orm "path/to/your/orm" // ) &ast.GenDecl{ Tok: token.IMPORT, @@ -67,7 +67,7 @@ func generateCRUDFileContent(buf buffer, arcSrcSet *ARCSourceSet) (string, error Path: &ast.BasicLit{Kind: token.STRING, Value: strconv.Quote("fmt")}, }, &ast.ImportSpec{ - Name: &ast.Ident{Name: "dao"}, + Name: &ast.Ident{Name: importName}, Path: &ast.BasicLit{Kind: token.STRING, Value: strconv.Quote(structPackagePath)}, }, }, diff --git a/internal/arcgen/lang/go/generate_crud_common.go b/internal/arcgen/lang/go/generate_crud_common.go index c532113..6ab094f 100644 --- a/internal/arcgen/lang/go/generate_crud_common.go +++ b/internal/arcgen/lang/go/generate_crud_common.go @@ -31,8 +31,9 @@ func fprintCRUDCommon(osFile osFile, buf buffer, arcSrcSetSlice ARCSourceSetSlic } const ( - sqlQueryerContextVarName = "sqlContext" - sqlQueryerContextTypeName = "sqlQueryerContext" + importName = "orm" + queryerContextVarName = "queryerContext" + queryerContextTypeName = "QueryerContext" ) //nolint:cyclop,funlen,gocognit,maintidx @@ -40,7 +41,7 @@ func generateCRUDCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice, astFile := &ast.File{ // package Name: &ast.Ident{ - Name: config.GoCRUDPackageName(), + Name: config.GoORMPackageName(), }, // methods Decls: []ast.Decl{}, @@ -60,7 +61,7 @@ func generateCRUDCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice, // "database/sql" // "log/slog" // - // dao "path/to/your/dao" + // orm "path/to/your/orm" // ) &ast.GenDecl{ Tok: token.IMPORT, @@ -75,14 +76,14 @@ func generateCRUDCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice, Path: &ast.BasicLit{Kind: token.STRING, Value: strconv.Quote("log/slog")}, }, &ast.ImportSpec{ - Name: &ast.Ident{Name: "dao"}, + Name: &ast.Ident{Name: importName}, Path: &ast.BasicLit{Kind: token.STRING, Value: strconv.Quote(structPackagePath)}, }, }, }, ) - // type sqlQueryerContext interface { + // type QueryerContext interface { // 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) @@ -93,7 +94,7 @@ func generateCRUDCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice, Specs: []ast.Spec{ &ast.TypeSpec{ // Assign: token.Pos(1), - Name: &ast.Ident{Name: sqlQueryerContextTypeName}, + Name: &ast.Ident{Name: queryerContextTypeName}, Type: &ast.InterfaceType{ Methods: &ast.FieldList{ List: []*ast.Field{ @@ -153,7 +154,7 @@ func generateCRUDCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice, Tok: token.TYPE, Specs: []ast.Spec{ &ast.TypeSpec{ - Name: &ast.Ident{Name: config.GoCRUDTypeNameUnexported()}, + Name: &ast.Ident{Name: config.GoORMStructName()}, Type: &ast.StructType{Fields: &ast.FieldList{}}, }, }, @@ -221,7 +222,7 @@ func generateCRUDCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice, ) // type CRUD interface { - // Create{StructName}(ctx context.Context, sqlQueryer sqlQueryerContext, s *{Struct}) error + // Create{StructName}(ctx context.Context, queryerContext QueryerContext, s *{Struct}) error // ... // } methods := make([]*ast.Field, 0) @@ -241,7 +242,7 @@ func generateCRUDCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice, if n.Recv != nil && len(n.Recv.List) > 0 { if t, ok := n.Recv.List[0].Type.(*ast.StarExpr); ok { if ident, ok := t.X.(*ast.Ident); ok { - if ident.Name == config.GoCRUDTypeNameUnexported() { + if ident.Name == config.GoORMStructName() { methods = append(methods, &ast.Field{ Names: []*ast.Ident{{Name: n.Name.Name}}, Type: n.Type, @@ -261,7 +262,7 @@ func generateCRUDCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice, Tok: token.TYPE, Specs: []ast.Spec{ &ast.TypeSpec{ - Name: &ast.Ident{Name: config.GoCRUDTypeName()}, + Name: &ast.Ident{Name: config.GoORMTypeName()}, Type: &ast.InterfaceType{ Methods: &ast.FieldList{List: methods}, }, @@ -275,9 +276,9 @@ func generateCRUDCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice, // } astFile.Decls = append(astFile.Decls, &ast.FuncDecl{ - Name: &ast.Ident{Name: "New" + config.GoCRUDTypeName()}, - Type: &ast.FuncType{Results: &ast.FieldList{List: []*ast.Field{{Type: &ast.Ident{Name: config.GoCRUDTypeName()}}}}}, - Body: &ast.BlockStmt{List: []ast.Stmt{&ast.ReturnStmt{Results: []ast.Expr{&ast.UnaryExpr{Op: token.AND, X: &ast.Ident{Name: config.GoCRUDTypeNameUnexported() + "{}"}}}}}}, + Name: &ast.Ident{Name: "New" + config.GoORMTypeName()}, + Type: &ast.FuncType{Results: &ast.FieldList{List: []*ast.Field{{Type: &ast.Ident{Name: config.GoORMTypeName()}}}}}, + Body: &ast.BlockStmt{List: []ast.Stmt{&ast.ReturnStmt{Results: []ast.Expr{&ast.UnaryExpr{Op: token.AND, X: &ast.Ident{Name: config.GoORMStructName() + "{}"}}}}}}, }, ) diff --git a/internal/arcgen/lang/go/generate_crud_create.go b/internal/arcgen/lang/go/generate_crud_create.go index 9ecf4c5..fd68939 100644 --- a/internal/arcgen/lang/go/generate_crud_create.go +++ b/internal/arcgen/lang/go/generate_crud_create.go @@ -19,10 +19,10 @@ func generateCREATEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { // const Create{StructName}Query = `INSERT INTO {table_name} ({column_name1}, {column_name2}) VALUES ($1, $2)` // - // func (q *query) Create{StructName}(ctx context.Context, queryer sqlQueryerContext, s *{Struct}) error { + // func (q *query) Create{StructName}(ctx context.Context, queryer QueryerContext, s *{Struct}) error { // LoggerFromContext(ctx).Debug(Create{StructName}Query) - // if _, err := sqlContext.ExecContext(ctx, Create{StructName}Query, s.{ColumnName1}, s.{ColumnName2}); err != nil { - // return fmt.Errorf("sqlContext.ExecContext: %w", err) + // if _, err := queryerContext.ExecContext(ctx, Create{StructName}Query, s.{ColumnName1}, s.{ColumnName2}); err != nil { + // return fmt.Errorf("queryerContext.ExecContext: %w", err) // } // return nil // } @@ -42,13 +42,13 @@ func generateCREATEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { }, }, &ast.FuncDecl{ - Recv: &ast.FieldList{List: []*ast.Field{{Names: []*ast.Ident{{Name: "q"}}, Type: &ast.StarExpr{X: &ast.Ident{Name: config.GoCRUDTypeNameUnexported()}}}}}, + Recv: &ast.FieldList{List: []*ast.Field{{Names: []*ast.Ident{{Name: "q"}}, Type: &ast.StarExpr{X: &ast.Ident{Name: config.GoORMStructName()}}}}}, Name: &ast.Ident{Name: funcName}, Type: &ast.FuncType{ Params: &ast.FieldList{List: []*ast.Field{ {Names: []*ast.Ident{{Name: "ctx"}}, Type: &ast.Ident{Name: "context.Context"}}, - {Names: []*ast.Ident{{Name: sqlQueryerContextVarName}}, Type: &ast.Ident{Name: sqlQueryerContextTypeName}}, - {Names: []*ast.Ident{{Name: "s"}}, Type: &ast.StarExpr{X: &ast.Ident{Name: "dao." + structName}}}, + {Names: []*ast.Ident{{Name: queryerContextVarName}}, Type: &ast.Ident{Name: queryerContextTypeName}}, + {Names: []*ast.Ident{{Name: "s"}}, Type: &ast.StarExpr{X: &ast.Ident{Name: importName + "." + structName}}}, }}, Results: &ast.FieldList{List: []*ast.Field{ {Type: &ast.Ident{Name: "error"}}, @@ -67,13 +67,13 @@ func generateCREATEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { }, }, &ast.IfStmt{ - // if _, err := sqlQueryer.ExecContext(ctx, Create{StructName}Query, s.{ColumnName1}, s.{ColumnName2}); err != nil { + // if _, err := queryerContext.ExecContext(ctx, Create{StructName}Query, s.{ColumnName1}, s.{ColumnName2}); err != nil { Init: &ast.AssignStmt{ Lhs: []ast.Expr{&ast.Ident{Name: "_"}, &ast.Ident{Name: "err"}}, Tok: token.DEFINE, Rhs: []ast.Expr{&ast.CallExpr{ Fun: &ast.SelectorExpr{ - X: &ast.Ident{Name: sqlQueryerContextVarName}, + X: &ast.Ident{Name: queryerContextVarName}, Sel: &ast.Ident{Name: "ExecContext"}, }, Args: append( @@ -93,10 +93,10 @@ func generateCREATEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { // err != nil { Cond: &ast.BinaryExpr{X: &ast.Ident{Name: "err"}, Op: token.NEQ, Y: &ast.Ident{Name: "nil"}}, Body: &ast.BlockStmt{List: []ast.Stmt{ - // return fmt.Errorf("sqlContext.ExecContext: %w", err) + // return fmt.Errorf("queryerContext.ExecContext: %w", err) &ast.ReturnStmt{Results: []ast.Expr{&ast.CallExpr{ Fun: &ast.SelectorExpr{X: &ast.Ident{Name: "fmt"}, Sel: &ast.Ident{Name: "Errorf"}}, - Args: []ast.Expr{&ast.Ident{Name: strconv.Quote(sqlQueryerContextVarName + ".ExecContext: %w")}, &ast.Ident{Name: "err"}}, + Args: []ast.Expr{&ast.Ident{Name: strconv.Quote(queryerContextVarName + ".ExecContext: %w")}, &ast.Ident{Name: "err"}}, }}}, }}, }, diff --git a/internal/arcgen/lang/go/generate_crud_delete.go b/internal/arcgen/lang/go/generate_crud_delete.go index 125f890..a76fa3a 100644 --- a/internal/arcgen/lang/go/generate_crud_delete.go +++ b/internal/arcgen/lang/go/generate_crud_delete.go @@ -18,9 +18,9 @@ func generateDELETEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { // const Delete{StructName}Query = `DELETE FROM {table_name} WHERE {pk1} = ? [AND {pk2} = ?]` // - // func (q *query) Delete{StructName}(ctx context.Context, queryer sqlQueryerContext, pk1 pk1type [, pk2 pk2type]) error { - // if _, err := sqlContext.ExecContext(ctx, Delete{StructName}Query, pk1 [, pk2]); err != nil { - // return fmt.Errorf("sqlContext.ExecContext: %w", err) + // func (q *query) Delete{StructName}(ctx context.Context, queryer QueryerContext, pk1 pk1type [, pk2 pk2type]) error { + // if _, err := queryerContext.ExecContext(ctx, Delete{StructName}Query, pk1 [, pk2]); err != nil { + // return fmt.Errorf("queryerContext.ExecContext: %w", err) // } // return nil // } @@ -47,13 +47,13 @@ func generateDELETEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { }, }, &ast.FuncDecl{ - Recv: &ast.FieldList{List: []*ast.Field{{Names: []*ast.Ident{{Name: "q"}}, Type: &ast.StarExpr{X: &ast.Ident{Name: config.GoCRUDTypeNameUnexported()}}}}}, + Recv: &ast.FieldList{List: []*ast.Field{{Names: []*ast.Ident{{Name: "q"}}, Type: &ast.StarExpr{X: &ast.Ident{Name: config.GoORMStructName()}}}}}, Name: &ast.Ident{Name: funcName}, Type: &ast.FuncType{ Params: &ast.FieldList{List: append( []*ast.Field{ {Names: []*ast.Ident{{Name: "ctx"}}, Type: &ast.Ident{Name: "context.Context"}}, - {Names: []*ast.Ident{{Name: sqlQueryerContextVarName}}, Type: &ast.Ident{Name: sqlQueryerContextTypeName}}, + {Names: []*ast.Ident{{Name: queryerContextVarName}}, Type: &ast.Ident{Name: queryerContextTypeName}}, }, func() []*ast.Field { var fields []*ast.Field @@ -80,13 +80,13 @@ func generateDELETEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { }, }, &ast.IfStmt{ - // if _, err := sqlContext.ExecContext(ctx, Delete{StructName}Query, pk1 [, pk2]); err != nil { + // if _, err := queryerContext.ExecContext(ctx, Delete{StructName}Query, pk1 [, pk2]); err != nil { Init: &ast.AssignStmt{ Lhs: []ast.Expr{&ast.Ident{Name: "_"}, &ast.Ident{Name: "err"}}, Tok: token.DEFINE, Rhs: []ast.Expr{&ast.CallExpr{ Fun: &ast.SelectorExpr{ - X: &ast.Ident{Name: sqlQueryerContextVarName}, + X: &ast.Ident{Name: queryerContextVarName}, Sel: &ast.Ident{Name: "ExecContext"}, }, Args: append( @@ -107,10 +107,10 @@ func generateDELETEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { // err != nil { Cond: &ast.BinaryExpr{X: &ast.Ident{Name: "err"}, Op: token.NEQ, Y: &ast.Ident{Name: "nil"}}, Body: &ast.BlockStmt{List: []ast.Stmt{ - // return fmt.Errorf("sqlContext.ExecContext: %w", err) + // return fmt.Errorf("queryerContext.ExecContext: %w", err) &ast.ReturnStmt{Results: []ast.Expr{&ast.CallExpr{ Fun: &ast.SelectorExpr{X: &ast.Ident{Name: "fmt"}, Sel: &ast.Ident{Name: "Errorf"}}, - Args: []ast.Expr{&ast.Ident{Name: strconv.Quote(sqlQueryerContextVarName + ".ExecContext: %w")}, &ast.Ident{Name: "err"}}, + Args: []ast.Expr{&ast.Ident{Name: strconv.Quote(queryerContextVarName + ".ExecContext: %w")}, &ast.Ident{Name: "err"}}, }}}, }}, }, diff --git a/internal/arcgen/lang/go/generate_crud_read.go b/internal/arcgen/lang/go/generate_crud_read.go index 56ba83f..92d5b3d 100644 --- a/internal/arcgen/lang/go/generate_crud_read.go +++ b/internal/arcgen/lang/go/generate_crud_read.go @@ -21,7 +21,7 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { // const Find{StructName}ByPKQuery = `SELECT {column_name1}, {column_name2} FROM {table_name} WHERE {pk1} = ? [AND ...]` // - // func (q *query) Find{StructName}ByPK(ctx context.Context, queryer sqlQueryerContext, pk1 pk1type, ...) ({Struct}, error) { + // func (q *query) Find{StructName}ByPK(ctx context.Context, queryer QueryerContext, pk1 pk1type, ...) ({Struct}, error) { // row := queryer.QueryRowContext(ctx, Find{StructName}Query, pk1, ...) // var s {Struct} // if err := row.Scan( @@ -51,7 +51,7 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { Name: &ast.Ident{Name: byPKFuncName}, Recv: &ast.FieldList{List: []*ast.Field{{ Names: []*ast.Ident{{Name: "q"}}, - Type: &ast.StarExpr{X: &ast.Ident{Name: config.GoCRUDTypeNameUnexported()}}, + Type: &ast.StarExpr{X: &ast.Ident{Name: config.GoORMStructName()}}, }}}, Type: &ast.FuncType{ Params: &ast.FieldList{ @@ -61,8 +61,8 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { Type: &ast.Ident{Name: "context.Context"}, }, { - Names: []*ast.Ident{{Name: sqlQueryerContextVarName}}, - Type: &ast.Ident{Name: sqlQueryerContextTypeName}, + Names: []*ast.Ident{{Name: queryerContextVarName}}, + Type: &ast.Ident{Name: queryerContextTypeName}, }, }, func() []*ast.Field { @@ -77,7 +77,7 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { }()...), }, Results: &ast.FieldList{List: []*ast.Field{ - {Type: &ast.StarExpr{X: &ast.Ident{Name: "dao." + structName}}}, + {Type: &ast.StarExpr{X: &ast.Ident{Name: importName + "." + structName}}}, {Type: &ast.Ident{Name: "error"}}, }}, }, @@ -98,7 +98,7 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { Lhs: []ast.Expr{&ast.Ident{Name: "row"}}, Tok: token.DEFINE, Rhs: []ast.Expr{&ast.CallExpr{ - Fun: &ast.SelectorExpr{X: &ast.Ident{Name: sqlQueryerContextVarName}, Sel: &ast.Ident{Name: "QueryRowContext"}}, + Fun: &ast.SelectorExpr{X: &ast.Ident{Name: queryerContextVarName}, Sel: &ast.Ident{Name: "QueryRowContext"}}, Args: append( []ast.Expr{ &ast.Ident{Name: "ctx"}, @@ -118,7 +118,7 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { Tok: token.VAR, Specs: []ast.Spec{&ast.ValueSpec{ Names: []*ast.Ident{{Name: "s"}}, - Type: &ast.Ident{Name: "dao." + structName}, + Type: &ast.Ident{Name: importName + "." + structName}, }}, }}, // if err := row.Scan(&s.{ColumnName1}, &s.{ColumnName2}); err != nil { @@ -167,7 +167,7 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { for _, hasOneTag := range tableInfo.HasOneTags { // const Find{StructName}By{FieldName}Query = `SELECT {column_name1}, {column_name2} FROM {table_name} WHERE {column} = ? [AND ...]` // - // func (q *queryer) Find{StructName}ByColumn1[AndColumn2](ctx context.Context, queryer sqlQueryerContext, {ColumnName} {ColumnType} [, {Column2Name} {Column2Type}]) ({Struct}Slice, error) { + // func (q *queryer) Find{StructName}ByColumn1[AndColumn2](ctx context.Context, queryer QueryerContext, {ColumnName} {ColumnType} [, {Column2Name} {Column2Type}]) ({Struct}Slice, error) { // row := queryer.QueryRowContext(ctx, Find{StructName}Query, {ColumnName}, {Column2Name}) // var s {Struct} // if err := row.Scan( @@ -198,7 +198,7 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { Name: &ast.Ident{Name: byHasOneTagFuncName}, Recv: &ast.FieldList{List: []*ast.Field{{ Names: []*ast.Ident{{Name: "q"}}, - Type: &ast.StarExpr{X: &ast.Ident{Name: config.GoCRUDTypeNameUnexported()}}, + Type: &ast.StarExpr{X: &ast.Ident{Name: config.GoORMStructName()}}, }}}, Type: &ast.FuncType{ Params: &ast.FieldList{ @@ -208,8 +208,8 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { Type: &ast.Ident{Name: "context.Context"}, }, { - Names: []*ast.Ident{{Name: sqlQueryerContextVarName}}, - Type: &ast.Ident{Name: sqlQueryerContextTypeName}, + Names: []*ast.Ident{{Name: queryerContextVarName}}, + Type: &ast.Ident{Name: queryerContextTypeName}, }, }, func() []*ast.Field { @@ -224,7 +224,7 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { }()...), }, Results: &ast.FieldList{List: []*ast.Field{ - {Type: &ast.StarExpr{X: &ast.Ident{Name: "dao." + structName}}}, + {Type: &ast.StarExpr{X: &ast.Ident{Name: importName + "." + structName}}}, {Type: &ast.Ident{Name: "error"}}, }}, }, @@ -245,7 +245,7 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { Lhs: []ast.Expr{&ast.Ident{Name: "row"}}, Tok: token.DEFINE, Rhs: []ast.Expr{&ast.CallExpr{ - Fun: &ast.SelectorExpr{X: &ast.Ident{Name: sqlQueryerContextVarName}, Sel: &ast.Ident{Name: "QueryRowContext"}}, + Fun: &ast.SelectorExpr{X: &ast.Ident{Name: queryerContextVarName}, Sel: &ast.Ident{Name: "QueryRowContext"}}, Args: append( []ast.Expr{ &ast.Ident{Name: "ctx"}, @@ -265,7 +265,7 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { Tok: token.VAR, Specs: []ast.Spec{&ast.ValueSpec{ Names: []*ast.Ident{{Name: "s"}}, - Type: &ast.Ident{Name: "dao." + structName}, + Type: &ast.Ident{Name: importName + "." + structName}, }}, }}, // if err := row.Scan(&s.{ColumnName1}, &s.{ColumnName2}); err != nil { @@ -315,7 +315,7 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { for _, hasManyTag := range tableInfo.HasManyTags { // const List{StructName}By{FieldName}Query = `SELECT {column_name1}, {column_name2} FROM {table_name} WHERE {pk1} = ? [AND ...]` // - // func (q *query) List{StructName}ByColumn1[AndColumn2](ctx context.Context, queryer sqlQueryerContext, {ColumnName} {ColumnType} [, {Column2Name} {Column2Type}]) ({Struct}Slice, error) { + // func (q *query) List{StructName}ByColumn1[AndColumn2](ctx context.Context, queryer QueryerContext, {ColumnName} {ColumnType} [, {Column2Name} {Column2Type}]) ({Struct}Slice, error) { // rows, err := queryer.QueryContext(ctx, List{StructName}Query, {ColumnName}, {Column2Name}) // if err != nil { // return nil, fmt.Errorf("queryer.QueryContext: %w", err) @@ -339,9 +339,9 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { // } // return ss, nil // } - structSliceType := "[]*dao." + structName + structSliceType := "[]*" + importName + "." + structName if sliceSuffix := config.GoSliceTypeSuffix(); sliceSuffix != "" { - structSliceType = "dao." + structName + sliceSuffix + structSliceType = importName + "." + structName + sliceSuffix } byHasOneTagFuncName := "List" + structName + "By" + hasManyTag byHasOneTagQueryName := byHasOneTagFuncName + "Query" @@ -363,14 +363,14 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { Name: &ast.Ident{Name: byHasOneTagFuncName}, Recv: &ast.FieldList{List: []*ast.Field{{ Names: []*ast.Ident{{Name: "q"}}, - Type: &ast.StarExpr{X: &ast.Ident{Name: config.GoCRUDTypeNameUnexported()}}, + Type: &ast.StarExpr{X: &ast.Ident{Name: config.GoORMStructName()}}, }}}, Type: &ast.FuncType{ Params: &ast.FieldList{ List: append( []*ast.Field{ {Names: []*ast.Ident{{Name: "ctx"}}, Type: &ast.Ident{Name: "context.Context"}}, - {Names: []*ast.Ident{{Name: sqlQueryerContextVarName}}, Type: &ast.Ident{Name: sqlQueryerContextTypeName}}, + {Names: []*ast.Ident{{Name: queryerContextVarName}}, Type: &ast.Ident{Name: queryerContextTypeName}}, }, func() []*ast.Field { fields := make([]*ast.Field, 0) @@ -402,7 +402,7 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { Lhs: []ast.Expr{&ast.Ident{Name: "rows"}, &ast.Ident{Name: "err"}}, Tok: token.DEFINE, Rhs: []ast.Expr{&ast.CallExpr{ - Fun: &ast.SelectorExpr{X: &ast.Ident{Name: sqlQueryerContextVarName}, Sel: &ast.Ident{Name: "QueryContext"}}, + Fun: &ast.SelectorExpr{X: &ast.Ident{Name: queryerContextVarName}, Sel: &ast.Ident{Name: "QueryContext"}}, Args: append( []ast.Expr{ &ast.Ident{Name: "ctx"}, @@ -452,7 +452,7 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { Tok: token.VAR, Specs: []ast.Spec{&ast.ValueSpec{ Names: []*ast.Ident{{Name: "s"}}, - Type: &ast.Ident{Name: "dao." + structName}, + Type: &ast.Ident{Name: importName + "." + structName}, }}, }}, // if err := rows.Scan(&s.{ColumnName1}, &s.{ColumnName2}); err != nil { diff --git a/internal/arcgen/lang/go/generate_crud_update.go b/internal/arcgen/lang/go/generate_crud_update.go index da41a73..5802901 100644 --- a/internal/arcgen/lang/go/generate_crud_update.go +++ b/internal/arcgen/lang/go/generate_crud_update.go @@ -18,9 +18,9 @@ func generateUPDATEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { // const Update{StructName}Query = `UPDATE {table_name} SET ({column_name1}, {column_name2}) = (?, ?) WHERE {pk1} = ? [AND {pk2} = ?]` // - // func (q *query) Update{StructName}(ctx context.Context, queryer sqlQueryerContext, s *{Struct}) error { - // if _, err := sqlContext.ExecContext(ctx, Update{StructName}Query, s.{ColumnName1}, s.{ColumnName2}, s.{PK1} [, s.{PK2}]); err != nil { - // return fmt.Errorf("sqlContext.ExecContext: %w", err) + // func (q *query) Update{StructName}(ctx context.Context, queryer QueryerContext, s *{Struct}) error { + // if _, err := queryerContext.ExecContext(ctx, Update{StructName}Query, s.{ColumnName1}, s.{ColumnName2}, s.{PK1} [, s.{PK2}]); err != nil { + // return fmt.Errorf("queryerContext.ExecContext: %w", err) // } // return nil // } @@ -48,13 +48,13 @@ func generateUPDATEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { }, }, &ast.FuncDecl{ - Recv: &ast.FieldList{List: []*ast.Field{{Names: []*ast.Ident{{Name: "q"}}, Type: &ast.StarExpr{X: &ast.Ident{Name: config.GoCRUDTypeNameUnexported()}}}}}, + Recv: &ast.FieldList{List: []*ast.Field{{Names: []*ast.Ident{{Name: "q"}}, Type: &ast.StarExpr{X: &ast.Ident{Name: config.GoORMStructName()}}}}}, Name: &ast.Ident{Name: funcName}, Type: &ast.FuncType{ Params: &ast.FieldList{List: []*ast.Field{ {Names: []*ast.Ident{{Name: "ctx"}}, Type: &ast.Ident{Name: "context.Context"}}, - {Names: []*ast.Ident{{Name: sqlQueryerContextVarName}}, Type: &ast.Ident{Name: sqlQueryerContextTypeName}}, - {Names: []*ast.Ident{{Name: "s"}}, Type: &ast.StarExpr{X: &ast.Ident{Name: "dao." + structName}}}, + {Names: []*ast.Ident{{Name: queryerContextVarName}}, Type: &ast.Ident{Name: queryerContextTypeName}}, + {Names: []*ast.Ident{{Name: "s"}}, Type: &ast.StarExpr{X: &ast.Ident{Name: importName + "." + structName}}}, }}, Results: &ast.FieldList{List: []*ast.Field{ {Type: &ast.Ident{Name: "error"}}, @@ -73,13 +73,13 @@ func generateUPDATEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { }, }, &ast.IfStmt{ - // if _, err := sqlContext.ExecContext(ctx, Update{StructName}Query, s.{ColumnName1}, s.{ColumnName2}, s.{PK1} [, s.{PK2}]); err != nil { + // if _, err := queryerContext.ExecContext(ctx, Update{StructName}Query, s.{ColumnName1}, s.{ColumnName2}, s.{PK1} [, s.{PK2}]); err != nil { Init: &ast.AssignStmt{ Lhs: []ast.Expr{&ast.Ident{Name: "_"}, &ast.Ident{Name: "err"}}, Tok: token.DEFINE, Rhs: []ast.Expr{&ast.CallExpr{ Fun: &ast.SelectorExpr{ - X: &ast.Ident{Name: sqlQueryerContextVarName}, + X: &ast.Ident{Name: queryerContextVarName}, Sel: &ast.Ident{Name: "ExecContext"}, }, Args: append( @@ -108,10 +108,10 @@ func generateUPDATEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) { // err != nil { Cond: &ast.BinaryExpr{X: &ast.Ident{Name: "err"}, Op: token.NEQ, Y: &ast.Ident{Name: "nil"}}, Body: &ast.BlockStmt{List: []ast.Stmt{ - // return fmt.Errorf("sqlContext.ExecContext: %w", err) + // return fmt.Errorf("queryerContext.ExecContext: %w", err) &ast.ReturnStmt{Results: []ast.Expr{&ast.CallExpr{ Fun: &ast.SelectorExpr{X: &ast.Ident{Name: "fmt"}, Sel: &ast.Ident{Name: "Errorf"}}, - Args: []ast.Expr{&ast.Ident{Name: strconv.Quote(sqlQueryerContextVarName + ".ExecContext: %w")}, &ast.Ident{Name: "err"}}, + Args: []ast.Expr{&ast.Ident{Name: strconv.Quote(queryerContextVarName + ".ExecContext: %w")}, &ast.Ident{Name: "err"}}, }}}, }}, }, diff --git a/internal/arcgen/lang/go/source.go b/internal/arcgen/lang/go/source.go index cc0b7ce..e92bd14 100644 --- a/internal/arcgen/lang/go/source.go +++ b/internal/arcgen/lang/go/source.go @@ -7,7 +7,6 @@ import ( "reflect" "regexp" "slices" - "strconv" "strings" "sync" @@ -152,48 +151,6 @@ type ColumnInfo struct { HasManyTags []string } -func columnValuesPlaceholder(columns []string) string { - switch config.Dialect() { - case "mysql": - // ?, ?, ?, ... - return "?" + strings.Repeat(", ?", len(columns)-1) - default: - return func() string { - // $1, $2, $3, ... - var s strings.Builder - s.WriteString("$1") - for i := 2; i <= len(columns); i++ { - s.WriteString(", $") - s.WriteString(strconv.Itoa(i)) - } - return s.String() - }() - } -} - -//nolint:unparam -func whereColumnsPlaceholder(columns []string, op string) string { - switch config.Dialect() { - case "mysql": - // column1 = ? AND column2 = ? AND column3 = ... - return strings.Join(columns, " = ? "+op+" ") + " = ?" - default: - return func() string { - // column1 = $1 AND column2 = $2 AND column3 = ... - var s strings.Builder - for i, column := range columns { - if i > 0 { - s.WriteString(" " + op + " ") - } - s.WriteString(column) - s.WriteString(" = $") - s.WriteString(strconv.Itoa(i + 1)) - } - return s.String() - }() - } -} - func fieldName(x ast.Expr) *ast.Ident { switch t := x.(type) { case *ast.Ident: diff --git a/internal/config/config.go b/internal/config/config.go index e08661c..2717bd8 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -23,15 +23,16 @@ type config struct { Language string `json:"language"` // Golang GoColumnTag string `json:"go_column_tag"` - GoCRUDPackagePath string `json:"go_crud_package_path"` - GoCRUDPackageName string `json:"go_crud_package_name"` - GoCRUDTypeName string `json:"go_crud_type_name"` - GoHasManyTag string `json:"go_has_many_tag"` - GoHasOneTag string `json:"go_has_one_tag"` GoMethodNameTable string `json:"go_method_name_table"` GoMethodNameColumns string `json:"go_method_name_columns"` GoMethodPrefixColumn string `json:"go_method_prefix_column"` GoPKTag string `json:"go_pk_tag"` + GoORMPackagePath string `json:"go_orm_package_path"` + GoORMPackageName string `json:"go_orm_package_name"` + GoORMTypeName string `json:"go_orm_type_name"` + GoORMStructName string `json:"go_orm_struct_name"` + GoHasManyTag string `json:"go_has_many_tag"` + GoHasOneTag string `json:"go_has_one_tag"` GoSliceTypeSuffix string `json:"go_slice_type_suffix"` } @@ -72,54 +73,27 @@ func Load(ctx context.Context) (rollback func(), remainingArgs []string, err err } const ( - _OptionVersion = "version" - - _OptionTrace = "trace" - _EnvKeyTrace = "ARCGEN_TRACE" - - _OptionDebug = "debug" - _EnvKeyDebug = "ARCGEN_DEBUG" - - _OptionDialect = "dialect" - _EnvKeyDialect = "ARCGEN_DIALECT" - - _OptionLanguage = "lang" - _EnvKeyLanguage = "ARCGEN_LANGUAGE" + _OptionVersion = "version" + _OptionTrace, _EnvKeyTrace = "trace", "ARCGEN_TRACE" + _OptionDebug, _EnvKeyDebug = "debug", "ARCGEN_DEBUG" + _OptionDialect, _EnvKeyDialect = "dialect", "ARCGEN_DIALECT" + _OptionLanguage, _EnvKeyLanguage = "lang", "ARCGEN_LANGUAGE" + // // Golang - - _OptionGoColumnTag = "go-column-tag" - _EnvKeyGoColumnTag = "ARCGEN_GO_COLUMN_TAG" - - _OptionGoCRUDPackagePath = "go-crud-package-path" - _EnvKeyGoCRUDPackagePath = "ARCGEN_GO_CRUD_PACKAGE_PATH" - - _OptionGoCRUDPackageName = "go-crud-package-name" - _EnvKeyGoCRUDPackageName = "ARCGEN_GO_CRUD_PACKAGE_NAME" - - _OptionGoCRUDTypeName = "go-crud-type-name" - _EnvKeyGoCRUDTypeName = "ARCGEN_GO_CRUD_TYPE_NAME" - - _OptionGoHasManyTag = "go-has-many-tag" - _EnvKeyGoHasManyTag = "ARCGEN_GO_HAS_MANY_TAG" - - _OptionGoHasOneTag = "go-has-one-tag" - _EnvKeyGoHasOneTag = "ARCGEN_GO_HAS_ONE_TAG" - - _OptionGoMethodNameTable = "go-method-name-table" - _EnvKeyGoMethodNameTable = "ARCGEN_GO_METHOD_NAME_TABLE" - - _OptionGoMethodNameColumns = "go-method-name-columns" - _EnvKeyGoMethodNameColumns = "ARCGEN_GO_METHOD_NAME_COLUMNS" - - _OptionGoMethodPrefixColumn = "go-method-prefix-column" - _EnvKeyGoMethodPrefixColumn = "ARCGEN_GO_METHOD_PREFIX_COLUMN" - - _OptionGoPKTag = "go-pk-tag" - _EnvKeyGoPKTag = "ARCGEN_GO_PK_TAG" - - _OptionGoSliceTypeSuffix = "go-slice-type-suffix" - _EnvKeyGoSliceTypeSuffix = "ARCGEN_GO_SLICE_TYPE_SUFFIX" + // + _OptionGoColumnTag, _EnvKeyGoColumnTag = "go-column-tag", "ARCGEN_GO_COLUMN_TAG" + _OptionGoMethodNameTable, _EnvKeyGoMethodNameTable = "go-method-name-table", "ARCGEN_GO_METHOD_NAME_TABLE" + _OptionGoMethodNameColumns, _EnvKeyGoMethodNameColumns = "go-method-name-columns", "ARCGEN_GO_METHOD_NAME_COLUMNS" + _OptionGoMethodPrefixColumn, _EnvKeyGoMethodPrefixColumn = "go-method-prefix-column", "ARCGEN_GO_METHOD_PREFIX_COLUMN" + _OptionGoPKTag, _EnvKeyGoPKTag = "go-pk-tag", "ARCGEN_GO_PK_TAG" + _OptionGoSliceTypeSuffix, _EnvKeyGoSliceTypeSuffix = "go-slice-type-suffix", "ARCGEN_GO_SLICE_TYPE_SUFFIX" + _OptionGoORMPackagePath, _EnvKeyGoORMPackagePath = "go-orm-package-path", "ARCGEN_GO_ORM_PACKAGE_PATH" + _OptionGoORMPackageName, _EnvKeyGoORMPackageName = "go-orm-package-name", "ARCGEN_GO_ORM_PACKAGE_NAME" + _OptionGoORMTypeName, _EnvKeyGoORMTypeName = "go-orm-type-name", "ARCGEN_GO_ORM_TYPE_NAME" + _OptionGoORMStructName, _EnvKeyGoORMStructName = "go-orm-struct-name", "ARCGEN_GO_ORM_STRUCT_NAME" + _OptionGoHasManyTag, _EnvKeyGoHasManyTag = "go-has-many-tag", "ARCGEN_GO_HAS_MANY_TAG" + _OptionGoHasOneTag, _EnvKeyGoHasOneTag = "go-has-one-tag", "ARCGEN_GO_HAS_ONE_TAG" ) // MEMO: Since there is a possibility of returning some kind of error in the future, the signature is made to return an error. @@ -132,85 +106,75 @@ func load(ctx context.Context) (cfg *config, remainingArgs []string, err error) Description: "Generate methods that return information such as DB table names and column names from Go struct tags.", Options: []cliz.Option{ &cliz.BoolOption{ - Name: _OptionVersion, - Description: "show version information and exit", - Default: cliz.Default(false), + Name: _OptionVersion, Description: "show version information and exit", + Default: cliz.Default(false), }, &cliz.BoolOption{ - Name: _OptionTrace, Environment: _EnvKeyTrace, - Description: "trace mode enabled", - Default: cliz.Default(false), + Name: _OptionTrace, Environment: _EnvKeyTrace, Description: "trace mode enabled", + Default: cliz.Default(false), }, &cliz.BoolOption{ - Name: _OptionDebug, Environment: _EnvKeyDebug, - Description: "debug mode", - Default: cliz.Default(false), + Name: _OptionDebug, Environment: _EnvKeyDebug, Description: "debug mode", + Default: cliz.Default(false), }, &cliz.StringOption{ - Name: _OptionDialect, Environment: _EnvKeyDialect, - Description: "dialect for DML", - Default: cliz.Default("postgres"), + Name: _OptionDialect, Environment: _EnvKeyDialect, Description: "dialect for DML", + Default: cliz.Default("postgres"), }, &cliz.StringOption{ - Name: _OptionLanguage, Environment: _EnvKeyLanguage, - Description: "programming language to generate DDL", - Default: cliz.Default("go"), + Name: _OptionLanguage, Environment: _EnvKeyLanguage, Description: "programming language to generate DDL", + Default: cliz.Default("go"), }, + // // Golang + // &cliz.StringOption{ - Name: _OptionGoColumnTag, Environment: _EnvKeyGoColumnTag, - Description: "column annotation key for Go struct tag", - Default: cliz.Default("db"), + Name: _OptionGoColumnTag, Environment: _EnvKeyGoColumnTag, Description: "column annotation key for Go struct tag", + Default: cliz.Default("db"), }, &cliz.StringOption{ - Name: _OptionGoCRUDPackagePath, Environment: _EnvKeyGoCRUDPackagePath, - Description: "package path for CRUD", - Default: cliz.Default(""), + Name: _OptionGoMethodNameTable, Environment: _EnvKeyGoMethodNameTable, Description: "method name for table", + Default: cliz.Default("TableName"), }, &cliz.StringOption{ - Name: _OptionGoCRUDPackageName, Environment: _EnvKeyGoCRUDPackageName, - Description: "package name for CRUD", - Default: cliz.Default(""), + Name: _OptionGoMethodNameColumns, Environment: _EnvKeyGoMethodNameColumns, Description: "method name for columns", + Default: cliz.Default("ColumnNames"), }, &cliz.StringOption{ - Name: _OptionGoCRUDTypeName, Environment: _EnvKeyGoCRUDTypeName, - Description: "type name for CRUD", - Default: cliz.Default("CRUD"), + Name: _OptionGoMethodPrefixColumn, Environment: _EnvKeyGoMethodPrefixColumn, Description: "method prefix for column name", + Default: cliz.Default("ColumnName_"), }, &cliz.StringOption{ - Name: _OptionGoHasManyTag, Environment: _EnvKeyGoHasManyTag, - Description: "\"hasMany\" annotation key for Go struct tag", - Default: cliz.Default("hasMany"), + Name: _OptionGoPKTag, Environment: _EnvKeyGoPKTag, Description: "primary key annotation key for Go struct tag", + Default: cliz.Default("pk"), }, &cliz.StringOption{ - Name: _OptionGoHasOneTag, Environment: _EnvKeyGoHasOneTag, - Description: "\"hasOne\" annotation key for Go struct tag", - Default: cliz.Default("hasOne"), + Name: _OptionGoSliceTypeSuffix, Environment: _EnvKeyGoSliceTypeSuffix, Description: "suffix for slice type", + Default: cliz.Default("Slice"), }, &cliz.StringOption{ - Name: _OptionGoMethodNameTable, Environment: _EnvKeyGoMethodNameTable, - Description: "method name for table", - Default: cliz.Default("TableName"), + Name: _OptionGoORMPackagePath, Environment: _EnvKeyGoORMPackagePath, Description: "package path for ORM", + Default: cliz.Default(""), }, &cliz.StringOption{ - Name: _OptionGoMethodNameColumns, Environment: _EnvKeyGoMethodNameColumns, - Description: "method name for columns", - Default: cliz.Default("ColumnNames"), + Name: _OptionGoORMPackageName, Environment: _EnvKeyGoORMPackageName, Description: "package name for ORM", + Default: cliz.Default(""), }, &cliz.StringOption{ - Name: _OptionGoPKTag, Environment: _EnvKeyGoPKTag, - Description: "primary key annotation key for Go struct tag", - Default: cliz.Default("pk"), + Name: _OptionGoORMTypeName, Environment: _EnvKeyGoORMTypeName, Description: "interface type name for ORM", + Default: cliz.Default("ORM"), }, &cliz.StringOption{ - Name: _OptionGoMethodPrefixColumn, Environment: _EnvKeyGoMethodPrefixColumn, - Description: "method prefix for column name", - Default: cliz.Default("ColumnName_"), + Name: _OptionGoORMStructName, Environment: _EnvKeyGoORMStructName, Description: "struct name for ORM", + Default: cliz.Default(""), }, &cliz.StringOption{ - Name: _OptionGoSliceTypeSuffix, Environment: _EnvKeyGoSliceTypeSuffix, - Description: "suffix for slice type", - Default: cliz.Default("Slice"), + Name: _OptionGoHasOneTag, Environment: _EnvKeyGoHasOneTag, Description: "\"hasOne\" annotation key for Go struct tag", + Default: cliz.Default("hasOne"), + }, + &cliz.StringOption{ + Name: _OptionGoHasManyTag, Environment: _EnvKeyGoHasManyTag, Description: "\"hasMany\" annotation key for Go struct tag", + Default: cliz.Default("hasMany"), }, }, } @@ -228,16 +192,17 @@ func load(ctx context.Context) (cfg *config, remainingArgs []string, err error) Language: loadLanguage(ctx, cmd), // Golang GoColumnTag: loadGoColumnTag(ctx, cmd), - GoCRUDPackagePath: loadGoCRUDPackagePath(ctx, cmd), - GoCRUDPackageName: loadGoCRUDPackageName(ctx, cmd), - GoCRUDTypeName: loadGoCRUDTypeName(ctx, cmd), - GoHasManyTag: loadGoHasManyTag(ctx, cmd), - GoHasOneTag: loadGoHasOneTag(ctx, cmd), GoMethodNameTable: loadGoMethodNameTable(ctx, cmd), GoMethodNameColumns: loadGoMethodNameColumns(ctx, cmd), GoMethodPrefixColumn: loadGoMethodPrefixColumn(ctx, cmd), GoPKTag: loadGoPKTag(ctx, cmd), GoSliceTypeSuffix: loadGoSliceTypeSuffix(ctx, cmd), + GoORMPackagePath: loadGoORMPackagePath(ctx, cmd), + GoORMPackageName: loadGoORMPackageName(ctx, cmd), + GoORMTypeName: loadGoORMTypeName(ctx, cmd), + GoORMStructName: loadGoORMStructName(ctx, cmd), + GoHasOneTag: loadGoHasOneTag(ctx, cmd), + GoHasManyTag: loadGoHasManyTag(ctx, cmd), } if c.Debug { diff --git a/internal/config/go_crud_package_name.go b/internal/config/go_crud_package_name.go deleted file mode 100644 index ad9752e..0000000 --- a/internal/config/go_crud_package_name.go +++ /dev/null @@ -1,24 +0,0 @@ -package config - -import ( - "context" - "path/filepath" - - cliz "github.com/kunitsucom/util.go/exp/cli" -) - -func loadGoCRUDPackageName(_ context.Context, cmd *cliz.Command) string { - v, _ := cmd.GetOptionString(_OptionGoCRUDPackageName) - return v -} - -func GoCRUDPackageName() string { - globalConfigMu.Lock() - defer globalConfigMu.Unlock() - - if globalConfig.GoCRUDPackageName == "" { - globalConfig.GoCRUDPackageName = filepath.Base(globalConfig.GoCRUDPackagePath) - } - - return globalConfig.GoCRUDPackageName -} diff --git a/internal/config/go_crud_package_path.go b/internal/config/go_crud_package_path.go deleted file mode 100644 index 3e1adc7..0000000 --- a/internal/config/go_crud_package_path.go +++ /dev/null @@ -1,22 +0,0 @@ -package config - -import ( - "context" - - cliz "github.com/kunitsucom/util.go/exp/cli" -) - -func loadGoCRUDPackagePath(_ context.Context, cmd *cliz.Command) string { - v, _ := cmd.GetOptionString(_OptionGoCRUDPackagePath) - return v -} - -func GoCRUDPackagePath() string { - globalConfigMu.RLock() - defer globalConfigMu.RUnlock() - return globalConfig.GoCRUDPackagePath -} - -func GenerateGoCRUDPackage() bool { - return GoCRUDPackagePath() != "" -} diff --git a/internal/config/go_crud_type_name.go b/internal/config/go_crud_type_name.go deleted file mode 100644 index 424c8a5..0000000 --- a/internal/config/go_crud_type_name.go +++ /dev/null @@ -1,23 +0,0 @@ -package config - -import ( - "context" - - cliz "github.com/kunitsucom/util.go/exp/cli" -) - -func loadGoCRUDTypeName(_ context.Context, cmd *cliz.Command) string { - v, _ := cmd.GetOptionString(_OptionGoCRUDTypeName) - return v -} - -func GoCRUDTypeName() string { - globalConfigMu.Lock() - defer globalConfigMu.Unlock() - - return globalConfig.GoCRUDTypeName -} - -func GoCRUDTypeNameUnexported() string { - return "_" + GoCRUDTypeName() -} diff --git a/internal/config/go_orm_package_name.go b/internal/config/go_orm_package_name.go new file mode 100644 index 0000000..35b12a1 --- /dev/null +++ b/internal/config/go_orm_package_name.go @@ -0,0 +1,24 @@ +package config + +import ( + "context" + "path/filepath" + + cliz "github.com/kunitsucom/util.go/exp/cli" +) + +func loadGoORMPackageName(_ context.Context, cmd *cliz.Command) string { + v, _ := cmd.GetOptionString(_OptionGoORMPackageName) + return v +} + +func GoORMPackageName() string { + globalConfigMu.Lock() + defer globalConfigMu.Unlock() + + if globalConfig.GoORMPackageName == "" { + globalConfig.GoORMPackageName = filepath.Base(globalConfig.GoORMPackagePath) + } + + return globalConfig.GoORMPackageName +} diff --git a/internal/config/go_orm_package_path.go b/internal/config/go_orm_package_path.go new file mode 100644 index 0000000..3b44641 --- /dev/null +++ b/internal/config/go_orm_package_path.go @@ -0,0 +1,22 @@ +package config + +import ( + "context" + + cliz "github.com/kunitsucom/util.go/exp/cli" +) + +func loadGoORMPackagePath(_ context.Context, cmd *cliz.Command) string { + v, _ := cmd.GetOptionString(_OptionGoORMPackagePath) + return v +} + +func GoORMPackagePath() string { + globalConfigMu.RLock() + defer globalConfigMu.RUnlock() + return globalConfig.GoORMPackagePath +} + +func GenerateGoORMPackage() bool { + return GoORMPackagePath() != "" +} diff --git a/internal/config/go_orm_struct_name.go b/internal/config/go_orm_struct_name.go new file mode 100644 index 0000000..6a13646 --- /dev/null +++ b/internal/config/go_orm_struct_name.go @@ -0,0 +1,23 @@ +package config + +import ( + "context" + + cliz "github.com/kunitsucom/util.go/exp/cli" +) + +func loadGoORMStructName(_ context.Context, cmd *cliz.Command) string { + v, _ := cmd.GetOptionString(_OptionGoORMStructName) + return v +} + +func GoORMStructName() string { + globalConfigMu.Lock() + defer globalConfigMu.Unlock() + + if globalConfig.GoORMStructName == "" { + globalConfig.GoORMStructName = "_" + globalConfig.GoORMTypeName + } + + return globalConfig.GoORMStructName +} diff --git a/internal/config/go_orm_type_name.go b/internal/config/go_orm_type_name.go new file mode 100644 index 0000000..af760b5 --- /dev/null +++ b/internal/config/go_orm_type_name.go @@ -0,0 +1,19 @@ +package config + +import ( + "context" + + cliz "github.com/kunitsucom/util.go/exp/cli" +) + +func loadGoORMTypeName(_ context.Context, cmd *cliz.Command) string { + v, _ := cmd.GetOptionString(_OptionGoORMTypeName) + return v +} + +func GoORMTypeName() string { + globalConfigMu.Lock() + defer globalConfigMu.Unlock() + + return globalConfig.GoORMTypeName +}