Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add target-tables argument #118

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ func setRootOpts(cmd *cobra.Command, opts *internal.ArgType) {
cmd.Flags().BoolVar(&opts.SingleFile, "single-file", false, "toggle single file output")
cmd.Flags().StringVarP(&opts.Package, "package", "p", "", "package name used in generated Go code")
cmd.Flags().StringVar(&opts.CustomTypePackage, "custom-type-package", "", "Go package name to use for custom or unknown types")
cmd.Flags().StringArrayVar(&opts.IgnoreFields, "ignore-fields", nil, "fields to exclude from the generated Go code types")
cmd.Flags().StringArrayVar(&opts.IgnoreTables, "ignore-tables", nil, "tables to exclude from the generated Go code types")
cmd.Flags().StringArrayVar(&opts.TargetTables, "target-tables", nil, "tables to include from the generated Go code")
cmd.Flags().StringArrayVar(&opts.IgnoreFields, "ignore-fields", nil, "fields to exclude from the generated Go code")
cmd.Flags().StringArrayVar(&opts.IgnoreTables, "ignore-tables", nil, "tables to exclude from the generated Go code")
cmd.Flags().StringVar(&opts.TemplatePath, "template-path", "", "user supplied template path")
cmd.Flags().StringVar(&opts.Tags, "tags", "", "build tags to add to package header")
cmd.Flags().StringVar(&opts.InflectionRuleFile, "inflection-rule-file", "", "custom inflection rule file")
Expand Down
4 changes: 4 additions & 0 deletions internal/argtype.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ type ArgType struct {
// CustomTypePackage is the Go package name to use for unknown types.
CustomTypePackage string

// TargetTables allows the user to specify table names which should be
// handled by yo in the generated code.
TargetTables []string

// IgnoreFields allows the user to specify field names which should not be
// handled by yo in the generated code.
IgnoreFields []string
Expand Down
11 changes: 11 additions & 0 deletions internal/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func (tl *TypeLoader) LoadTable(args *ArgType) (map[string]*Type, error) {
for _, ti := range tableList {
ignore := false

// Ignore tables specified by IgnoreTables argument.
for _, ignoreTable := range args.IgnoreTables {
if ignoreTable == ti.TableName {
// Skip adding this table if user has specified they are not
Expand All @@ -112,6 +113,16 @@ func (tl *TypeLoader) LoadTable(args *ArgType) (map[string]*Type, error) {
}
}

// If the 'TargetTables' argument is passed, ignore any tables that are not specified in the array.
if len(args.TargetTables) != 0 {
ignore = true
for _, t := range args.TargetTables {
if t == ti.TableName {
ignore = false
}
}
}

if ignore {
continue
}
Expand Down