Skip to content

Commit

Permalink
BREAKING CHANGE: Add --go-orm-struct-package-import-path option and r…
Browse files Browse the repository at this point in the history
…ename --go-orm-package-path option to --go-orm-output-path option (#47)
  • Loading branch information
ginokent authored Aug 12, 2024
2 parents 98d50a6 + edbbf67 commit 556b09d
Show file tree
Hide file tree
Showing 25 changed files with 154 additions and 115 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 @@ -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.GoORMPackagePath(), filePathWithoutExt+crudFileExt)
filename := filepath.Join(config.GoORMOutputPath(), 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)
Expand All @@ -84,7 +84,7 @@ func generate(arcSrcSetSlice ARCSourceSetSlice) error {
}

if err := func() error {
filename := filepath.Join(config.GoORMPackagePath(), "common"+crudFileExt)
filename := filepath.Join(config.GoORMOutputPath(), "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)
Expand Down
12 changes: 8 additions & 4 deletions internal/arcgen/lang/go/generate_orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ func generateORMFileContent(buf buffer, arcSrcSet *ARCSourceSet) (string, error)
Decls: []ast.Decl{},
}

structPackagePath, err := util.GetPackagePath(filepath.Dir(arcSrcSet.Filename))
if err != nil {
return "", errorz.Errorf("GetPackagePath: %w", err)
structPackageImportPath := config.GoORMStructPackageImportPath()
if structPackageImportPath == "" {
var err error
structPackageImportPath, err = util.GetPackageImportPath(filepath.Dir(arcSrcSet.Filename))
if err != nil {
return "", errorz.Errorf("GetPackagePath: %w", err)
}
}

// import
Expand All @@ -68,7 +72,7 @@ func generateORMFileContent(buf buffer, arcSrcSet *ARCSourceSet) (string, error)
},
&ast.ImportSpec{
Name: &ast.Ident{Name: importName},
Path: &ast.BasicLit{Kind: token.STRING, Value: strconv.Quote(structPackagePath)},
Path: &ast.BasicLit{Kind: token.STRING, Value: strconv.Quote(structPackageImportPath)},
},
},
},
Expand Down
13 changes: 8 additions & 5 deletions internal/arcgen/lang/go/generate_orm_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ func generateORMCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice,

// Since all directories are the same from arcSrcSetSlice[0].Filename to arcSrcSetSlice[len(-1)].Filename,
// get the package path from arcSrcSetSlice[0].Filename.
dir := filepath.Dir(arcSrcSetSlice[0].Filename)
structPackagePath, err := util.GetPackagePath(dir)
if err != nil {
return "", errorz.Errorf("GetPackagePath: %w", err)
structPackageImportPath := config.GoORMStructPackageImportPath()
if structPackageImportPath == "" {
var err error
structPackageImportPath, err = util.GetPackageImportPath(filepath.Dir(arcSrcSetSlice[0].Filename))
if err != nil {
return "", errorz.Errorf("GetPackagePath: %w", err)
}
}

astFile.Decls = append(astFile.Decls,
Expand All @@ -80,7 +83,7 @@ func generateORMCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice,
},
&ast.ImportSpec{
Name: &ast.Ident{Name: importName},
Path: &ast.BasicLit{Kind: token.STRING, Value: strconv.Quote(structPackagePath)},
Path: &ast.BasicLit{Kind: token.STRING, Value: strconv.Quote(structPackageImportPath)},
},
},
},
Expand Down
13 changes: 7 additions & 6 deletions internal/arcgen/lang/util/package_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ import (
"fmt"
"go/build"
"path/filepath"

apperr "github.com/kunitsucom/arcgen/pkg/errors"
)

func GetPackagePath(dir string) (string, error) {
absDir, err := filepath.Abs(dir)
func GetPackageImportPath(path string) (string, error) {
absDir, err := filepath.Abs(path)
if err != nil {
return "", fmt.Errorf("failed to get absolute path: %w", err)
return "", fmt.Errorf("filepath.Abs: path=%s %w", path, err)
}

pkg, err := build.ImportDir(absDir, build.FindOnly)
if err != nil {
return "", fmt.Errorf("failed to import directory: %w", err)
return "", fmt.Errorf("build.ImportDir: path=%s: %w", path, err)
}

if pkg.ImportPath == "." {
// If ImportPath is ".", it means the directory is not in GOPATH or inside a module
// In this case, we'll use the last directory name as the package path
return filepath.Base(absDir), nil
return "", fmt.Errorf("path=%s: %w", absDir, apperr.ErrFailedToDetectPackageImportPath)
}

return pkg.ImportPath, nil
Expand Down
124 changes: 66 additions & 58 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@ type config struct {
Dialect string `json:"dialect"`
Language string `json:"language"`
// Golang
GoColumnTag string `json:"go_column_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"`
GoColumnTag string `json:"go_column_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"`
GoORMOutputPath string `json:"go_orm_output_path"`
GoORMPackageName string `json:"go_orm_package_name"`
GoORMStructPackageImportPath string `json:"go_orm_struct_package_import_path"`
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"`
}

//nolint:gochecknoglobals
Expand Down Expand Up @@ -73,28 +74,30 @@ func Load(ctx context.Context) (rollback func(), remainingArgs []string, err err
return rollback, remainingArgs, nil
}

//nolint:revive
const (
_OptionVersion = "version"
_OptionTrace, _EnvKeyTrace = "trace", "ARCGEN_TRACE"
_OptionDebug, _EnvKeyDebug = "debug", "ARCGEN_DEBUG"
_OptionDialect, _EnvKeyDialect = "dialect", "ARCGEN_DIALECT"
_OptionLanguage, _EnvKeyLanguage = "lang", "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, _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"
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"
OptionGoORMOutputPath, EnvKeyGoORMOutputPath = "go-orm-output-path", "ARCGEN_GO_ORM_OUTPUT_PATH"
OptionGoORMPackageName, EnvKeyGoORMPackageName = "go-orm-package-name", "ARCGEN_GO_ORM_PACKAGE_NAME"
OptionGoORMStructPackageImportPath, EnvKeyGoORMStructPackageImportPath = "go-orm-struct-package-import-path", "ARCGEN_GO_ORM_STRUCT_PACKAGE_IMPORT_PATH"
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.
Expand All @@ -107,74 +110,78 @@ 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",
Name: OptionVersion, Description: "show version information and exit",
Default: cliz.Default(false),
},
&cliz.BoolOption{
Name: _OptionTrace, Environment: _EnvKeyTrace, Description: "trace mode enabled",
Name: OptionTrace, Environment: EnvKeyTrace, Description: "trace mode enabled",
Default: cliz.Default(false),
},
&cliz.BoolOption{
Name: _OptionDebug, Environment: _EnvKeyDebug, Description: "debug mode",
Name: OptionDebug, Environment: EnvKeyDebug, Description: "debug mode",
Default: cliz.Default(false),
},
&cliz.StringOption{
Name: _OptionDialect, Environment: _EnvKeyDialect, Description: "dialect for DML",
Name: OptionDialect, Environment: EnvKeyDialect, Description: "dialect for DML",
Default: cliz.Default("postgres"),
},
&cliz.StringOption{
Name: _OptionLanguage, Environment: _EnvKeyLanguage, Description: "programming language to generate DDL",
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",
Name: OptionGoColumnTag, Environment: EnvKeyGoColumnTag, Description: "column annotation key for Go struct tag",
Default: cliz.Default("db"),
},
&cliz.StringOption{
Name: _OptionGoMethodNameTable, Environment: _EnvKeyGoMethodNameTable, Description: "method name for table",
Name: OptionGoMethodNameTable, Environment: EnvKeyGoMethodNameTable, Description: "method name for table",
Default: cliz.Default("TableName"),
},
&cliz.StringOption{
Name: _OptionGoMethodNameColumns, Environment: _EnvKeyGoMethodNameColumns, Description: "method name for columns",
Name: OptionGoMethodNameColumns, Environment: EnvKeyGoMethodNameColumns, Description: "method name for columns",
Default: cliz.Default("ColumnNames"),
},
&cliz.StringOption{
Name: _OptionGoMethodPrefixColumn, Environment: _EnvKeyGoMethodPrefixColumn, Description: "method prefix for column name",
Name: OptionGoMethodPrefixColumn, Environment: EnvKeyGoMethodPrefixColumn, Description: "method prefix for column name",
Default: cliz.Default("ColumnName_"),
},
&cliz.StringOption{
Name: _OptionGoPKTag, Environment: _EnvKeyGoPKTag, Description: "primary key annotation key for Go struct tag",
Name: OptionGoPKTag, Environment: EnvKeyGoPKTag, Description: "primary key annotation key for Go struct tag",
Default: cliz.Default("pk"),
},
&cliz.StringOption{
Name: _OptionGoSliceTypeSuffix, Environment: _EnvKeyGoSliceTypeSuffix, Description: "suffix for slice type",
Name: OptionGoSliceTypeSuffix, Environment: EnvKeyGoSliceTypeSuffix, Description: "suffix for slice type",
Default: cliz.Default("Slice"),
},
&cliz.StringOption{
Name: _OptionGoORMPackagePath, Environment: _EnvKeyGoORMPackagePath, Description: "package path for ORM",
Name: OptionGoORMOutputPath, Environment: EnvKeyGoORMOutputPath, Description: "output path of ORM. If empty, not output.",
Default: cliz.Default(""),
},
&cliz.StringOption{
Name: _OptionGoORMPackageName, Environment: _EnvKeyGoORMPackageName, Description: "package name for ORM",
Name: OptionGoORMPackageName, Environment: EnvKeyGoORMPackageName, Description: "package name for ORM. If empty, use the base name of the output path.",
Default: cliz.Default(""),
},
&cliz.StringOption{
Name: _OptionGoORMTypeName, Environment: _EnvKeyGoORMTypeName, Description: "interface type name for ORM",
Name: OptionGoORMStructPackageImportPath, Environment: EnvKeyGoORMStructPackageImportPath, Description: "package import path of ORM target struct. If empty, try to detect automatically.",
Default: cliz.Default(""),
},
&cliz.StringOption{
Name: OptionGoORMTypeName, Environment: EnvKeyGoORMTypeName, Description: "interface type name for ORM",
Default: cliz.Default("ORM"),
},
&cliz.StringOption{
Name: _OptionGoORMStructName, Environment: _EnvKeyGoORMStructName, Description: "struct name for ORM",
Name: OptionGoORMStructName, Environment: EnvKeyGoORMStructName, Description: "struct name for ORM",
Default: cliz.Default(""),
},
&cliz.StringOption{
Name: _OptionGoHasOneTag, Environment: _EnvKeyGoHasOneTag, Description: "\"hasOne\" annotation key for Go struct tag",
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",
Name: OptionGoHasManyTag, Environment: EnvKeyGoHasManyTag, Description: "\"hasMany\" annotation key for Go struct tag",
Default: cliz.Default("hasMany"),
},
},
Expand All @@ -196,18 +203,19 @@ func load(ctx context.Context) (cfg *config, remainingArgs []string, err error)
Dialect: loadDialect(ctx, cmd),
Language: loadLanguage(ctx, cmd),
// Golang
GoColumnTag: loadGoColumnTag(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),
GoColumnTag: loadGoColumnTag(ctx, cmd),
GoMethodNameTable: loadGoMethodNameTable(ctx, cmd),
GoMethodNameColumns: loadGoMethodNameColumns(ctx, cmd),
GoMethodPrefixColumn: loadGoMethodPrefixColumn(ctx, cmd),
GoPKTag: loadGoPKTag(ctx, cmd),
GoSliceTypeSuffix: loadGoSliceTypeSuffix(ctx, cmd),
GoORMOutputPath: loadGoORMOutputPath(ctx, cmd),
GoORMPackageName: loadGoORMPackageName(ctx, cmd),
GoORMStructPackageImportPath: loadGoORMStructPackageImportPath(ctx, cmd),
GoORMTypeName: loadGoORMTypeName(ctx, cmd),
GoORMStructName: loadGoORMStructName(ctx, cmd),
GoHasOneTag: loadGoHasOneTag(ctx, cmd),
GoHasManyTag: loadGoHasManyTag(ctx, cmd),
}

if c.Debug {
Expand Down
2 changes: 1 addition & 1 deletion internal/config/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func loadDebug(_ context.Context, cmd *cliz.Command) bool {
v, _ := cmd.GetOptionBool(_OptionDebug)
v, _ := cmd.GetOptionBool(OptionDebug)
return v
}

Expand Down
2 changes: 1 addition & 1 deletion internal/config/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func loadDialect(_ context.Context, cmd *cliz.Command) string {
v, _ := cmd.GetOptionString(_OptionDialect)
v, _ := cmd.GetOptionString(OptionDialect)
return v
}

Expand Down
2 changes: 1 addition & 1 deletion internal/config/go_column_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func loadGoColumnTag(_ context.Context, cmd *cliz.Command) string {
v, _ := cmd.GetOptionString(_OptionGoColumnTag)
v, _ := cmd.GetOptionString(OptionGoColumnTag)
return v
}

Expand Down
2 changes: 1 addition & 1 deletion internal/config/go_has_many_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func loadGoHasManyTag(_ context.Context, cmd *cliz.Command) string {
v, _ := cmd.GetOptionString(_OptionGoHasManyTag)
v, _ := cmd.GetOptionString(OptionGoHasManyTag)
return v
}

Expand Down
2 changes: 1 addition & 1 deletion internal/config/go_has_one_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func loadGoHasOneTag(_ context.Context, cmd *cliz.Command) string {
v, _ := cmd.GetOptionString(_OptionGoHasOneTag)
v, _ := cmd.GetOptionString(OptionGoHasOneTag)
return v
}

Expand Down
2 changes: 1 addition & 1 deletion internal/config/go_method_name_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func loadGoMethodNameColumns(_ context.Context, cmd *cliz.Command) string {
v, _ := cmd.GetOptionString(_OptionGoMethodNameColumns)
v, _ := cmd.GetOptionString(OptionGoMethodNameColumns)
return v
}

Expand Down
2 changes: 1 addition & 1 deletion internal/config/go_method_name_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func loadGoMethodNameTable(_ context.Context, cmd *cliz.Command) string {
v, _ := cmd.GetOptionString(_OptionGoMethodNameTable)
v, _ := cmd.GetOptionString(OptionGoMethodNameTable)
return v
}

Expand Down
2 changes: 1 addition & 1 deletion internal/config/go_method_prefix_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func loadGoMethodPrefixColumn(_ context.Context, cmd *cliz.Command) string {
v, _ := cmd.GetOptionString(_OptionGoMethodPrefixColumn)
v, _ := cmd.GetOptionString(OptionGoMethodPrefixColumn)
return v
}

Expand Down
Loading

0 comments on commit 556b09d

Please sign in to comment.