diff --git a/internal/arcgen/lang/go/generate.go b/internal/arcgen/lang/go/generate.go index 21ebf13..7804108 100644 --- a/internal/arcgen/lang/go/generate.go +++ b/internal/arcgen/lang/go/generate.go @@ -89,7 +89,7 @@ func fprint(osFile io.Writer, buf buffer, arcSrcSet *ARCSourceSet) error { } } - appendAST(astFile, structName, tableName, config.MethodNameTable(), config.MethodNameColumns(), config.MethodPrefixColumn(), fieldNames, columnNames) + appendAST(astFile, structName, config.SliceTypeSuffix(), tableName, config.MethodNameTable(), config.MethodNameColumns(), config.MethodPrefixColumn(), fieldNames, columnNames) } if err := printer.Fprint(buf, token.NewFileSet(), astFile); err != nil { @@ -100,7 +100,7 @@ func fprint(osFile io.Writer, buf buffer, arcSrcSet *ARCSourceSet) error { content := "" + "// Code generated by arcgen. DO NOT EDIT." + "\n" + "//" + "\n" + - fmt.Sprintf("// source: %s", filepathz.Short(arcSrcSet.Source.Filename)) + "\n" + + "// source: " + filepathz.Short(arcSrcSet.Source.Filename) + "\n" + "\n" + buf.String() @@ -129,8 +129,9 @@ func extractTableNameFromCommentGroup(commentGroup *ast.CommentGroup) string { } //nolint:funlen -func appendAST(file *ast.File, structName string, tableName string, methodNameTable string, methodNameColumns string, methodPrefixColumn string, fieldNames, columnNames []string) { +func appendAST(file *ast.File, structName string, sliceTypeSuffix string, tableName string, methodNameTable string, methodNameColumns string, methodPrefixColumn string, fieldNames, columnNames []string) { if tableName != "" { + // func (s *StructName) TableName() string { return "TableName" } file.Decls = append(file.Decls, &ast.FuncDecl{ Recv: &ast.FieldList{ List: []*ast.Field{ @@ -175,6 +176,73 @@ func appendAST(file *ast.File, structName string, tableName string, methodNameTa }, }, }) + + if sliceTypeSuffix != "" { + file.Decls = append( + file.Decls, + // type StructNameSlice []*StructName + &ast.GenDecl{ + Tok: token.TYPE, + Specs: []ast.Spec{ + &ast.TypeSpec{ + Name: &ast.Ident{ + Name: structName + sliceTypeSuffix, + }, + Type: &ast.ArrayType{ + Elt: &ast.StarExpr{ + X: &ast.Ident{ + Name: structName, // MEMO: struct name + }, + }, + }, + }, + }, + }, + // func (s StructNameSlice) TableName() string { return "TableName" } + &ast.FuncDecl{ + Recv: &ast.FieldList{ + List: []*ast.Field{ + { + Names: []*ast.Ident{ + { + Name: "s", + }, + }, + Type: &ast.Ident{ + Name: structName + sliceTypeSuffix, + }, + }, + }, + }, + Name: &ast.Ident{ + Name: methodNameTable, + }, + Type: &ast.FuncType{ + Params: &ast.FieldList{}, + Results: &ast.FieldList{ + List: []*ast.Field{ + { + Type: &ast.Ident{ + Name: "string", + }, + }, + }, + }, + }, + Body: &ast.BlockStmt{ + List: []ast.Stmt{ + &ast.ReturnStmt{ + Results: []ast.Expr{ + &ast.Ident{ + Name: strconv.Quote(tableName), + }, + }, + }, + }, + }, + }, + ) + } } file.Decls = append(file.Decls, generateASTColumnMethods(structName, methodNameColumns, methodPrefixColumn, fieldNames, columnNames)...) diff --git a/internal/arcgen/lang/go/generate_test.go b/internal/arcgen/lang/go/generate_test.go index b43610c..f389fff 100644 --- a/internal/arcgen/lang/go/generate_test.go +++ b/internal/arcgen/lang/go/generate_test.go @@ -26,6 +26,7 @@ func TestGenerate(t *testing.T) { "--method-name-table=GetTableName", "--method-name-columns=GetColumnNames", "--method-prefix-column=GetColumnName_", + "--slice-type-suffix=Slice", // "--src=tests/common.source", "--src=tests", }) diff --git a/internal/arcgen/lang/go/tests/common.golden b/internal/arcgen/lang/go/tests/common.golden index 05a4c23..d45d27a 100644 --- a/internal/arcgen/lang/go/tests/common.golden +++ b/internal/arcgen/lang/go/tests/common.golden @@ -8,6 +8,12 @@ func (s *User) GetTableName() string { return "Users" } +type UserSlice []*User + +func (s UserSlice) GetTableName() string { + return "Users" +} + func (s *User) GetColumnNames() []string { return []string{"Id", "Name", "Email", "Age"} } diff --git a/internal/config/config.go b/internal/config/config.go index 2d10be5..70ef94f 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -26,6 +26,7 @@ type config struct { MethodNameTable string `json:"method_name_table"` MethodNameColumns string `json:"method_name_columns"` MethodPrefixColumn string `json:"method_prefix_column"` + SliceTypeSuffix string `json:"slice_type_suffix"` } //nolint:gochecknoglobals @@ -92,6 +93,9 @@ const ( _OptionMethodPrefixColumn = "method-prefix-column" _EnvKeyMethodPrefixColumn = "ARCGEN_METHOD_PREFIX_COLUMN" + + _OptionSliceTypeSuffix = "slice-type-suffix" + _EnvKeySliceTypeSuffix = "ARCGEN_SLICE_TYPE_SUFFIX" ) // MEMO: Since there is a possibility of returning some kind of error in the future, the signature is made to return an error. @@ -156,6 +160,12 @@ func load(ctx context.Context) (cfg *config, err error) { //nolint:unparam Description: "method prefix for column name", Default: cliz.Default("ColumnName_"), }, + &cliz.StringOption{ + Name: _OptionSliceTypeSuffix, + Environment: _EnvKeySliceTypeSuffix, + Description: "suffix for slice type", + Default: cliz.Default(""), + }, }, } @@ -174,6 +184,7 @@ func load(ctx context.Context) (cfg *config, err error) { //nolint:unparam MethodNameTable: loadMethodNameTable(ctx, cmd), MethodNameColumns: loadMethodNameColumns(ctx, cmd), MethodPrefixColumn: loadMethodPrefixColumn(ctx, cmd), + SliceTypeSuffix: loadSliceTypeSuffix(ctx, cmd), } if c.Debug { diff --git a/internal/config/slice_suffix.go b/internal/config/slice_suffix.go new file mode 100644 index 0000000..6f8d99d --- /dev/null +++ b/internal/config/slice_suffix.go @@ -0,0 +1,18 @@ +package config + +import ( + "context" + + cliz "github.com/kunitsucom/util.go/exp/cli" +) + +func loadSliceTypeSuffix(_ context.Context, cmd *cliz.Command) string { + v, _ := cmd.GetOptionString(_OptionSliceTypeSuffix) + return v +} + +func SliceTypeSuffix() string { + globalConfigMu.RLock() + defer globalConfigMu.RUnlock() + return globalConfig.SliceTypeSuffix +}