Skip to content

Commit

Permalink
feat: Add --go-table-file-only option (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent authored Jan 26, 2025
2 parents 880fbaa + 41b1c8e commit 3e31b3d
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type GenerateConfig struct {
GoPKTag string `cli:"go-pk-tag, env=ORMGEN_GO_PK_TAG, default=pk, description=primary key annotation key for Go struct tag"`
GoHasOneTag string `cli:"go-has-one-tag, env=ORMGEN_GO_HAS_ONE_TAG, default=hasOne, description=\"hasOne\" annotation key for Go struct tag"`
GoHasManyTag string `cli:"go-has-many-tag, env=ORMGEN_GO_HAS_MANY_TAG, default=hasMany, description=\"hasMany\" annotation key for Go struct tag"`
GoTableFileOnly bool `cli:"go-table-file-only, env=ORMGEN_GO_TABLE_FILE_ONLY, default=false, description=generate only table file"`
GoTableNameMethod string `cli:"go-table-name-method, env=ORMGEN_GO_TABLE_NAME_METHOD, default=TableName, description=method name for table"`
GoColumnNameMethodPrefix string `cli:"go-column-name-method-prefix, env=ORMGEN_GO_COLUMN_NAME_METHOD_PREFIX, default=ColumnName_, description=method name for columns"`
GoColumnsNameMethod string `cli:"go-columns-name-method, env=ORMGEN_GO_COLUMNS_NAME_METHOD, default=ColumnsName, description=method prefix for column"`
Expand Down
3 changes: 3 additions & 0 deletions internal/lang/go/generator/generate_each_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func generateEachFile(ctx context.Context, ormoptPackageImportPath string, packa
defer span.End()

cfg := contexts.GenerateConfig(ctx)
if cfg.GoTableFileOnly {
return nil
}

// each file
eachFilePath := filepath.Join(cfg.GoORMOutputPath, fileSource.SourceRelativePath)
Expand Down
3 changes: 3 additions & 0 deletions internal/lang/go/generator/generate_each_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func generateEachPackage(ctx context.Context, ormoptPackageImportPath string, pa
defer span.End()

cfg := contexts.GenerateConfig(ctx)
if cfg.GoTableFileOnly {
return nil
}

// each package
eachPackageFileName := filepath.Base(strings.TrimSuffix(eachPackageTmpl, ".tmpl"))
Expand Down
3 changes: 3 additions & 0 deletions internal/lang/go/generator/generate_ormopt_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func generateORMOptFile(ctx context.Context) (ormoptPackageImportPath string, er
defer span.End()

cfg := contexts.GenerateConfig(ctx)
if cfg.GoTableFileOnly {
return "", nil
}

ormoptDirPath := filepath.Join(cfg.GoORMOutputPath, filepath.Base(filepath.Dir(ormoptTmpl)))
if err := tracez.StartFuncWithSpanNameSuffix(ctx, "os.MkdirAll", func(_ context.Context) (err error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/lang/go/generator/generate_table_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func generateTableFile(ctx context.Context, fileSource *source.FileSource) (err
defer dbGenFile.Close()

if err := fprintTableMethods(ctx, dbGenFile, fileSource); err != nil {
return errorz.Errorf("FprintTableMethods: %w", err)
return errorz.Errorf("fprintTableMethods: %w", err)
}

return nil
Expand Down
7 changes: 7 additions & 0 deletions internal/lang/go/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func templateFuncMap(cfg *config.GenerateConfig) template.FuncMap {
}
}

//nolint:cyclop
func Generate(ctx context.Context, packageSources source.PackageSourceSlice) error {
ctx, span := tracez.Start(ctx)
defer span.End()
Expand All @@ -126,6 +127,9 @@ func Generate(ctx context.Context, packageSources source.PackageSourceSlice) err

for _, packageSource := range packageSources {
if err := tracez.StartFuncWithSpanNameSuffix(ctx, "os.MkdirAll", func(_ context.Context) (err error) {
if cfg.GoTableFileOnly {
return nil
}
packageDirPath := filepath.Join(cfg.GoORMOutputPath, packageSource.SourceRelativePath)
return os.MkdirAll(packageDirPath, consts.Perm0o775)
}); err != nil {
Expand All @@ -145,6 +149,9 @@ func Generate(ctx context.Context, packageSources source.PackageSourceSlice) err

var tablesInFile []*TableInfo
for _, structSource := range fileSource.StructSources {
if cfg.GoTableFileOnly {
continue
}
tableInfo := BuildTableInfo(ctx, structSource)
tablesInFile = append(tablesInFile, tableInfo)
tablesInPackage = append(tablesInPackage, tableInfo)
Expand Down

0 comments on commit 3e31b3d

Please sign in to comment.