Skip to content
This repository has been archived by the owner on Jan 14, 2025. It is now read-only.

Commit

Permalink
feat: Add --slice-type-suffix option
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent committed May 30, 2024
1 parent c81cf04 commit 581512d
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 3 deletions.
74 changes: 71 additions & 3 deletions internal/arcgen/lang/go/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()

Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)...)
Expand Down
1 change: 1 addition & 0 deletions internal/arcgen/lang/go/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
})
Expand Down
6 changes: 6 additions & 0 deletions internal/arcgen/lang/go/tests/common.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
}
Expand Down
11 changes: 11 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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(""),
},
},
}

Expand All @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions internal/config/slice_suffix.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 581512d

Please sign in to comment.