From 9208ca48768fb97adc43cd438cdc6623d0ff4234 Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Thu, 23 Feb 2023 10:25:30 +0900 Subject: [PATCH] Update the v2 README.md --- v2/README.md | 577 +++++++++++++++++++++++++++---- v2/cmd/generate.go | 16 +- v2/generator/funcs.go | 12 +- v2/internal/reserved_keywords.go | 2 +- v2/internal/util.go | 4 +- 5 files changed, 532 insertions(+), 79 deletions(-) diff --git a/v2/README.md b/v2/README.md index 8d73936..f5689ff 100644 --- a/v2/README.md +++ b/v2/README.md @@ -3,7 +3,7 @@ `yo` is a command-line tool to generate Go code for [Google Cloud Spanner](https://cloud.google.com/spanner/), forked from [xo](https://github.com/xo/xo) :rose:. -`yo` uses database schema to generate code by using [Information Schema](https://cloud.google.com/spanner/docs/information-schema). `yo` runs SQL queries against tables in `INFORMATION_SCHEMA` to fetch metadata for a database, and applies the metadata to Go templates to generate code/models to acccess Cloud Spanner. +`yo` uses database schema to generate code by using [Information Schema](https://cloud.google.com/spanner/docs/information-schema). `yo` runs SQL queries against tables in `INFORMATION_SCHEMA` to fetch metadata for a database and applies the metadata to Go templates to generate code/models to access Cloud Spanner. Please feel free to report issues and send pull requests, but note that this application is not officially supported as part of the Cloud Spanner product. @@ -16,54 +16,96 @@ $ go get -u go.mercari.io/yo/v2 ## Quickstart -The following is a quick overview of using `yo` on the command-line: +The following is a quick overview of using `yo` on the command line: ```sh -# change to project directory +# change to the project directory $ cd $GOPATH/src/path/to/project # make an output directory $ mkdir -p models # generate code for a schema -$ yo $SPANNER_PROJECT_NAME $SPANNER_INSTANCE_NAME $SPANNER_DATABASE_NAME -o models +$ yo generate $SPANNER_PROJECT_NAME $SPANNER_INSTANCE_NAME $SPANNER_DATABASE_NAME -o models ``` -## Command line options +## Commands -The following are `yo`'s command-line arguments and options: +### `generate` + +The `generate` command generates the Go code from DDL. + +#### Examples + +```sh +# Generate models from DDL under the models directory +yo generate schema.sql --from-ddl -o models + +# Generate models from DDL under the models directory with custom types +yo generate schema.sql --from-ddl -o models --custom-types-file custom_column_types.yml + +# Generate models under the models directory +yo generate $SPANNER_PROJECT_NAME $SPANNER_INSTANCE_NAME $SPANNER_DATABASE_NAME -o models + +# Generate models under the models directory with custom types +yo generate $SPANNER_PROJECT_NAME $SPANNER_INSTANCE_NAME $SPANNER_DATABASE_NAME -o models --custom-types-file custom_column_types.yml +``` + +#### Flags + +``` +-c, --config string path to Yo config file + --disable-default-modules disable the default modules for code generation + --disable-format disable to apply gofmt to generated files + --from-ddl toggle using DDL file + --global-module stringArray add a user defined module to global modules + --header-module string replace the default header module by user defined module +-h, --help help for generate + --ignore-fields stringArray fields to exclude from the generated Go code types + --ignore-tables stringArray tables to exclude from the generated Go code types +-o, --out string output path or file name +-p, --package string package name used in generated Go code + --suffix string output file suffix (default ".yo.go") + --tags string build tags to add to a package header + --type-module stringArray add a user defined module to type modules + --use-legacy-index-module use legacy index func name +``` + +### `create-template` + +The `create-template` command generates default template files. + +#### Examples ```sh -$ yo --help -yo is a command-line tool to generate Go code for Google Cloud Spanner. +# Create default templates under the templates directory +yo create-template --template-path templates +``` -Usage: - yo PROJECT_NAME INSTANCE_NAME DATABASE_NAME [flags] +#### Flags -Examples: - # Generate models under models directory - yo $SPANNER_PROJECT_NAME $SPANNER_INSTANCE_NAME $SPANNER_DATABASE_NAME -o models +``` +-h, --help help for create-template + --template-path string destination template path +``` - # Generate models under models directory with custom types - yo $SPANNER_PROJECT_NAME $SPANNER_INSTANCE_NAME $SPANNER_DATABASE_NAME -o models --custom-types-file custom_column_types.yml +### `completion` -Flags: - --custom-type-package string Go package name to use for custom or unknown types - --custom-types-file string custom table field type definition file - -h, --help help for yo - --ignore-fields stringArray fields to exclude from the generated Go code types - --ignore-tables stringArray tables to exclude from the generated Go code types - --inflection-rule-file string custom inflection rule file - -o, --out string output path or file name - -p, --package string package name used in generated Go code - --suffix string output file suffix (default ".yo.go") - --tags string build tags to add to package header - --template-path string user supplied template path +The `completion` generates the autocompletion script for yo for the specified shell. +See each sub-command's help for details on how to use the generated script. + +#### Available sub-commands + +``` +bash Generate the autocompletion script for bash +fish Generate the autocompletion script for fish +powershell Generate the autocompletion script for powershell +zsh Generate the autocompletion script for zsh ``` ## Generated code -`yo` generates a file per a table by default. Each files has struct, metadata, methods for a table. +`yo` generates a file per table by default. Each file has a struct, metadata, and methods for a table. ### struct @@ -89,7 +131,7 @@ type Example struct { ### Mutation methods -An operation agaist a table is represented as mutation in Cloud Spanner. `yo` generates methods to create mutation to modify a table. +An operation against a table is represented as a mutation in Cloud Spanner. `yo` generates methods to create a mutation to modify a table. * Insert * A wrapper method of `spanner.Insert`, which embeds struct values implicitly to insert a new record with struct values. @@ -97,6 +139,8 @@ An operation agaist a table is represented as mutation in Cloud Spanner. `yo` ge * A wrapper method of `spanner.Update`, which embeds struct values implicitly to update all columns into struct values. * InsertOrUpdate * A wrapper method of `spanner.InsertOrUpdate`, which embeds struct values implicitly to insert a new record or update all columns to struct values. +* Replace + * A wrapper method of `spanner.Replace`, which inserts a record, deleting any existing row. Unlike InsertOrUpdate, this means any values not explicitly written become NULL. * UpdateColumns * A wrapper method of `spanner.Update`, which updates specified columns into struct values. @@ -104,7 +148,7 @@ An operation agaist a table is represented as mutation in Cloud Spanner. `yo` ge `yo` generates functions to read data from Cloud Spanner. The functions are generated based on index. -Naming convention of genearted functions is `FindXXXByYYY`. The XXX is table name and YYY is index name. XXX will be singular if the index is unique index, or plural if the index is not unique. +Naming convention of generated functions is `FindXXXByYYY`. The XXX is table name and YYY is index name. XXX will be singular if the index is unique index, or plural if the index is not unique. **TODO** @@ -136,58 +180,467 @@ if err != nil { } ``` -## Templates for code generation +## Modules -`yo` uses Go [template](https://golang.org/pkg/text/template/) package to generate code. You can use your own template for code generation by using `--template-path` option. +`yo` uses Go [template](https://golang.org/pkg/text/template/) package to generate code. You can customize the templates using modules. There are three types of modules in `yo`. -`yo` provides default templates and uses them when `--template-path` option is not specified. The templates exist in [templates](templates/) directory. The templates are embeded into `yo` binary. +### Global module +The global module is a component shared among various elements. `yo_db.yo.go` is the default global module. You may add your global module by specifying `--global-module` flag to the generate command. -### Custom Template Quickstart +### Header module +The header module defines the header template for each generated code. +See [the builtin default header template](https://github.com/cloudspannerecosystem/yo/blob/021c6c2f0f72be6004656898eb74bbf92a8e216f/v2/module/builtin/templates/header.go.tpl), or you may replace it by specifying `--header-module` flag to the generate command. -The following is a quick overview of copying the base templates contained in -the `yo` project's [`templates/`](templates) directory, editing to suit, and -using with `yo`: +### Type module +The type module is a template for each Spanner table. You can add your type module by using `--type-module` flag to the generate command. -```sh -# change to working project directory -$ cd $GOPATH/src/path/to/my/project +## Templates + +### Template files + +You can create template files by running the `create-template` command. See the section above for more details. + +| Template File | Type | Description | +|-----------------------|--------|--------------------------------------------------------| +| `header.go.tpl` | Header | Header template used for all the generated code | +| `yo_db.go.tpl` | Global | Template for components shared by different components | +| `type.go.tpl` | Type | Template for schema tables | +| `operation.go.tpl` | Type | Template for CRUD operations | +| `index.go.tpl` | Type | Template for schema indexes | +| `legacy_index.go.tpl` | Type | Legacy template for schema indexes | + +### Template functions + +`yo` provides helper functions for templates. Those functions are listed below. -# create a template directory -$ mkdir -p templates +#### [filterFields(fields []*models.Field, ignoreNames ...interface{}) []*models.Field](https://github.com/cloudspannerecosystem/yo/blob/64d13dc0e8aa2b0ac5eef549ebb395a0d79284c6/v2/generator/funcs.go#L77-L89) -# copy yo templates -$ cp "$GOPATH/src/github.com/cloudspannerecosystem/yo/templates/*" templates/ +`filterFields` filters out fields from the given list of fields. `ignoreNames` can be either a name of the field or a list of `models.Field` pointers. -# remove yo binary data -$ rm templates/*.go +##### Arguments -# edit base templates -$ vi templates/*.tpl.go +- `fields` - A list of `models.Field` pointers to filter through. +- `ignoreNames` - A list of names to ignore. Each element can be either a string or a list of `models.Field` pointers. -# use with yo -$ yo $SPANNER_PROJECT_NAME $SPANNER_INSTANCE_NAME $SPANNER_DATABASE_NAME -o models --template-path templates +##### Examples + +Ignore a field whose name is "field2". + +```gotemplate +{{/* .Fields = []*models.Field{{Name: "field1"}, {Name: "field2"}, {Name: "field3"}} */}} + +{{- $fields := (filterFields .Fields "field2") -}} + +{{/* returns []*models.Field{{Name: "field1"}, {Name: "field3"}} */}} ``` -See the Custom Template example below for more information on adapting the base -templates in the `yo` source tree for use within your own project. +#### [shortName(typ string, scopeConflicts ...interface{}) string](https://github.com/cloudspannerecosystem/yo/blob/64d13dc0e8aa2b0ac5eef549ebb395a0d79284c6/v2/generator/funcs.go#L131-L196) -### Template files +`shortName` generates a conflict-free Go identifier for the given `typ`. + +##### Arguments + +- `typ` - A name of the Go identifier. +- `scopeConflicts` - A list of the scope-specific reserved names. Each element can be either a string or a list of `models.Field` pointers. + +##### Examples + +No conflicts. + +```gotemplate +{{/* .Type = *models.Field{Name: "CustomField"} */}} + +{{- $short := (shortName .Type.Name "err" "db") -}} + +{{/* returns "cf" */}} +``` + +A conflict exists. + +```gotemplate +{{/* .Type = *models.Field{Name: "CustomField"} */}} + +{{- $short := (shortName .Type.Name "cf" "db") -}} + +{{/* returns "cfz" */}} +``` + +#### [nullcheck(field *models.Field) string](https://github.com/cloudspannerecosystem/yo/blob/64d13dc0e8aa2b0ac5eef549ebb395a0d79284c6/v2/generator/funcs.go#L395-L410) + +`nullcheck` generates Go code to check if the given field is null or not. + +##### Arguments + +- `field` - A `models.Field` pointer to generate null check Go code. + +##### Examples + +The field type is not Spanner Null type. + +```gotemplate +{{/* .Field = *models.Field{Type: "string", Name: "field1"} */}} + +{{ nullcheck .Field }} + +{{/* returns "yo, ok := field1.(yoIsNull); ok && yo.IsNull()" */}} +``` -| Template File | Description | -|--------------------------------|-------------------------------------------------------| -| `templates/type.go.tpl` | Template for schema tables | -| `templates/index.go.tpl` | Template for schema indexes | -| `templates/yo_db.go.tpl` | Package level template generated once per package | -| `templates/yo_package.go.tpl` | File header template generated once per file | +The field type is Spanner Null type. -### Helper functions +```gotemplate +{{/* .Field = *models.Field{Type: "spanner.NullInt64", Name: "field1"} */}} -**This is not a stable feature** +{{ nullcheck .Field }} -`yo` provides some helper functions which can be used in templates. Those are defined in [`generator/funcs.go`](generator/funcs.go). Those are not well documented and are likely to change. +{{/* returns "field1.IsNull()" */}} +``` + +#### [hasColumn(fields []*models.Field, name string) bool](https://github.com/cloudspannerecosystem/yo/blob/64d13dc0e8aa2b0ac5eef549ebb395a0d79284c6/v2/generator/funcs.go#L371-L381) + +`hasColumn` receives a list of fields and checks if it contains a field with the specified column name. + +##### Arguments + +- `fields` - A list of `models.Field` pointers to check against. +- `name` - A column name to check. + +##### Examples + +A field with the column name exists. + +```gotemplate +{{/* .Fields = []*models.Field{{ColumnName: "field1"}, {ColumnName: "field2"}, {ColumnName: "field3"}} */}} + +{{ hasColumn .Fields "field1" }} + +{{/* returns true */}} +``` + +A field with the column name does not exist. + +```gotemplate +{{/* .Fields = []*models.Field{{ColumnName: "field1"}, {ColumnName: "field2"}, {ColumnName: "field3"}} */}} + +{{ hasColumn .Fields "field0" }} + +{{/* returns false */}} +``` + +#### [columnNames(fields []*models.Field) string](https://github.com/cloudspannerecosystem/yo/blob/64d13dc0e8aa2b0ac5eef549ebb395a0d79284c6/v2/generator/funcs.go#L91-L109) + +`columnNames` receives a list of fields and converts it into comma-separated column names for SQL statements. + +##### Arguments + +- `fields` - A list of `models.Field` pointers converted from. + +##### Examples + +Generates an SQL column names from a list of fields. + +```gotemplate +{{/* .Fields = []*models.Field{{ColumnName: "field1"}, {ColumnName: "field2"}, {ColumnName: "field3"}} */}} + +var sqlstr = "SELECT {{ columnNames .Fields }} FROM Singers" + +{{/* returns "field1, field2, field3" */}} +``` + +#### [columnNamesQuery(fields []*models.Field, sep string) string](https://github.com/cloudspannerecosystem/yo/blob/64d13dc0e8aa2b0ac5eef549ebb395a0d79284c6/v2/generator/funcs.go#L111-L129) + +`columnNamesQuery` receives a list of fields and converts it into an SQL query for a WHERE clause, which is joined by `sep`. + +#### Arguments + +- `fields` - A list of `models.Field` pointers converted from. +- `sep` - A separator for the query. + +##### Examples + +Generates an SQL column names from a list of fields. + +```gotemplate +{{/* .Fields = []*models.Field{{ColumnName: "field1"}, {ColumnName: "field2"}, {ColumnName: "field3"}} */}} + +var sqlstr = "SELECT * FROM Singers WHERE {{ columnNamesQuery .Fields " AND " }}" + +{{/* returns "field1 = @param1 AND field2 = @param2 AND field3 = @param3" */}} +``` + +#### [columnPrefixNames(fields []*models.Field, prefix string) string](https://github.com/cloudspannerecosystem/yo/blob/64d13dc0e8aa2b0ac5eef549ebb395a0d79284c6/v2/generator/funcs.go#L198-L215) + +`columnPrefixNames` receives a list of fields and converts it into comma-separated column names with given `prefix`. + +#### Arguments + +- `fields` - A list of `models.Field` pointers converted from. +- `prefix` - A prefix attached to each column name. + +##### Examples + +```gotemplate +{{/* .Field = []*models.Field{{ColumnName: "field1"}, {ColumnName: "field2"}, {ColumnName: "field3"}} */}} + +{{ columnNamesQuery .Fields "t" }} + +{{/* returns "t.field1, t.field2, t.field3" */}} +``` + +#### [hasField(fields []*models.Field, name string) bool](https://github.com/cloudspannerecosystem/yo/blob/64d13dc0e8aa2b0ac5eef549ebb395a0d79284c6/v2/generator/funcs.go#L198-L215) + +`hasField` receives a list of fields and checks if it contains a filed whose `Name` is `name`. + +#### Arguments + +- `fields` - A list of `models.Field` pointers checked against. +- `name` - A name of the field to check. + +##### Examples + +The field exists. + +```gotemplate +{{/* .Fields = []*models.Field{{Name: "field1"}} */}} + +{{ hasField .Fields "field1" }} + +{{/* returns true */}} +``` + +The field does not exist. + +```gotemplate +{{/* .Fields = []*models.Field{{Name: "field1"}} */}} + +{{ hasField .Fields "field2" }} + +{{/* returns false */}} +``` + +#### [fieldNames(fields []*models.Field, prefix string) string](https://github.com/cloudspannerecosystem/yo/blob/64d13dc0e8aa2b0ac5eef549ebb395a0d79284c6/v2/generator/funcs.go#L217-L235) + +`fieldNames` receives a list of fields and converts into comma-separated names with given `prefix`. + +#### Arguments + +- `fields` - A list of `models.Field` pointers converted from. +- `prefix` - A prefix attached to each name. + +##### Examples + +```gotemplate +{{/* .Fields = []*models.Field{{Name: "field1"}, {Name: "field2"}, {Name: "field3"}} */}} + +{{ fieldNames .Fields "t" }} + +{{/* returns "t.field1, t.field2, t.field3" */}} +``` + +#### [goParam(name string) string](https://github.com/cloudspannerecosystem/yo/blob/64d13dc0e8aa2b0ac5eef549ebb395a0d79284c6/v2/generator/funcs.go#L288-L299) + +`goParam` converts the first character of the given string into lowercase. The function is supposed to be used for changing a field name to a Go parameter name. + +#### Arguments + +- `name` - A name of Go parameter converted from. + +##### Examples + +The `name` is camel case. + +```gotemplate +{{ goParam "NameOfSomething" }} + +{{/* returns "nameOfSomething" */}} +``` + +The `name` is snake case. + +```gotemplate +{{ goParam "name_of_something" }} + +{{/* returns "nameOfSomething" */}} +``` + +#### [goEncodedParam(name string) string](https://github.com/cloudspannerecosystem/yo/blob/64d13dc0e8aa2b0ac5eef549ebb395a0d79284c6/v2/generator/funcs.go#L301-L304) + +`goEncodedParam` generates Go code to encode a Go parameter. + +#### Arguments + +- `name` - A name of Go parameter encoded from. + +##### Examples + +The `name` is camel case. + +```gotemplate +{{ goEncodedParam "NameOfSomething" }} + +{{/* returns "yoEncode(nameOfSomething)" */}} +``` + +The `name` is snake case. + +```gotemplate +{{ goEncodedParam "name_of_something" }} + +{{/* returns "yoEncode(nameOfSomething)" */}} +``` + +#### [goParams(fields []*models.Field, addPrefix bool, addType bool) string](https://github.com/cloudspannerecosystem/yo/blob/64d13dc0e8aa2b0ac5eef549ebb395a0d79284c6/v2/generator/funcs.go#L306-L345) + +`goParams` converts a list of fields into named Go parameters. + +#### Arguments + +- `fields` - A list of `models.Field` pointers converted from. +- `addPrefix` - Whether prefixing ", " or not to the final result. +- `addType` - Whether adding Go type declaration or not. + +##### Examples + +Without any options. + +```gotemplate +{{/* .Field = []*models.Field{{Name: "field1"}, {Name: "field2"}, {Name: "field3"}} */}} + +{{ goParams .Field false false }} + +{{/* returns "field1, field2, field3" */}} +``` + +With the `addPrefix` option. + +```gotemplate +{{/* .Field = []*models.Field{{Name: "field1"}, {Name: "field2"}, {Name: "field3"}} */}} + +{{ goParams .Field true false }} + +{{/* returns ", field1, field2, field3" */}} +``` + +With the `addType` option. + +```gotemplate +{{/* .Field = []*models.Field{{Name: "field1", Type: "Type1"}, {Name: "field2", Type: "Type2"}, {Name: "field3", Type: "Type2"}} */}} + +{{ goParams .Field false true }} + +{{/* returns "field1 Type1, field2 Type2, field3 Type3" */}} +``` + +#### [goEncodedParams(fields []*models.Field, addPrefix bool) string](https://github.com/cloudspannerecosystem/yo/blob/64d13dc0e8aa2b0ac5eef549ebb395a0d79284c6/v2/generator/funcs.go#L347-L369) + +`goEncodedParams` generates Go code to encode Go parameters. + +#### Arguments + +- `fields` - A list of `models.Field` pointers encoded from. +- `addPrefix` - Whether prefixing ", " or not to the final result. + +##### Examples + +Without the `addPrefix` option. + +```gotemplate +{{/* .Field = []*models.Field{{Name: "field1"}, {Name: "field2"}, {Name: "field3"}} */}} + +{{ goEncodedParams .Field false }} + +{{/* returns "yoEncode(field1), yoEncode(field2), yoEncode(field3)" */}} +``` + +With the `addPrefix` option. + +```gotemplate +{{/* .Field = []*models.Field{{Name: "field1"}, {Name: "field2"}, {Name: "field3"}} */}} + +{{ goEncodedParams .Field true }} + +{{/* returns ", yoEncode(field1), yoEncode(field2), yoEncode(field3)" */}} +``` + +#### [escape(col string) string](https://github.com/cloudspannerecosystem/yo/blob/64d13dc0e8aa2b0ac5eef549ebb395a0d79284c6/v2/generator/funcs.go#L412-L415) + +`escape` escapes a column name with back quites if it conflicts with reserved keywords. For more information about reserved keywords, please refer to [the Spanner documentation](https://cloud.google.com/spanner/docs/reference/standard-sql/lexical#reserved_keywords). + +#### Arguments + +- `col` - A column name. + +##### Examples + +No conflicts. + +```gotemplate +{{ escape "Name" }} + +{{/* returns "Name" */}} +``` + +With a conflict with the reserved keywords. + +```gotemplate +{{ escape "JOIN" }} + +{{/* returns "`JOIN`" */}} +``` + +#### [toLower(s string) string](https://github.com/cloudspannerecosystem/yo/blob/64d13dc0e8aa2b0ac5eef549ebb395a0d79284c6/v2/generator/funcs.go#L417-L420) + +`toLower` converts the given string into lower case. + +#### Arguments + +- `s` - A string to convert to lower case. + +##### Examples + +```gotemplate +{{ toLower "NAME" }} + +{{/* returns "name" */}} +``` + +#### [pluralize(s string) string](https://github.com/cloudspannerecosystem/yo/blob/64d13dc0e8aa2b0ac5eef549ebb395a0d79284c6/v2/generator/funcs.go#L422-L425) + +`pluralize` pluralizes the given string. + +#### Arguments + +- `s` - A string to pluralize. + +##### Examples + +```gotemplate +{{ pluralize "name" }} + +{{/* returns "names" */}} +``` ## Configuration +You may customize some configurations via a config file. Use the `--config` flag to specify the config file path. + +### Custom type definitions + +You may define custom type rules to overwrite the original Go types in a config file. + +``` +tables: + - name: "Singers" + columns: + - name: Id + customType: "uint64" + - name: "Musics" + columns: + - name: Id + customType: "uint64" + - name: MusicType + customType: "MusicType" +``` + ### Custom inflection rules `yo` uses inflection to convert singular or plural name each other. You can add inflection rules with config file. diff --git a/v2/cmd/generate.go b/v2/cmd/generate.go index a220d65..18233d8 100644 --- a/v2/cmd/generate.go +++ b/v2/cmd/generate.go @@ -112,16 +112,16 @@ var ( } return nil }, - Example: ` # Generate models from ddl under models directory + Example: ` # Generate models from DDL under the models directory yo generate schema.sql --from-ddl -o models - # Generate models from ddl under models directory with custom types + # Generate models from DDL under the models directory with custom types yo generate schema.sql --from-ddl -o models --custom-types-file custom_column_types.yml - # Generate models under models directory + # Generate models under the models directory yo generate $SPANNER_PROJECT_NAME $SPANNER_INSTANCE_NAME $SPANNER_DATABASE_NAME -o models - # Generate models under models directory with custom types + # Generate models under the models directory with custom types yo generate $SPANNER_PROJECT_NAME $SPANNER_INSTANCE_NAME $SPANNER_DATABASE_NAME -o models --custom-types-file custom_column_types.yml `, RunE: func(cmd *cobra.Command, args []string) error { @@ -195,18 +195,18 @@ var ( func init() { generateCmd.Flags().StringVarP(&generateCmdOpts.ConfigFile, "config", "c", "", "path to Yo config file") - generateCmd.Flags().BoolVar(&generateCmdOpts.FromDDL, "from-ddl", false, "toggle using ddl file") + generateCmd.Flags().BoolVar(&generateCmdOpts.FromDDL, "from-ddl", false, "toggle using DDL file") generateCmd.Flags().StringVarP(&generateCmdOpts.Out, "out", "o", "", "output path or file name") generateCmd.Flags().StringVar(&generateCmdOpts.Suffix, "suffix", defaultSuffix, "output file suffix") generateCmd.Flags().StringVarP(&generateCmdOpts.Package, "package", "p", "", "package name used in generated Go code") generateCmd.Flags().StringArrayVar(&generateCmdOpts.IgnoreFields, "ignore-fields", nil, "fields to exclude from the generated Go code types") generateCmd.Flags().StringArrayVar(&generateCmdOpts.IgnoreTables, "ignore-tables", nil, "tables to exclude from the generated Go code types") - generateCmd.Flags().StringVar(&generateCmdOpts.Tags, "tags", "", "build tags to add to package header") + generateCmd.Flags().StringVar(&generateCmdOpts.Tags, "tags", "", "build tags to add to a package header") generateCmd.Flags().BoolVar(&generateCmdOpts.DisableDefaultModules, "disable-default-modules", false, "disable the default modules for code generation") generateCmd.Flags().BoolVar(&generateCmdOpts.DisableFormat, "disable-format", false, "disable to apply gofmt to generated files") generateCmd.Flags().StringVar(&generateCmdOpts.HeaderModule, "header-module", "", "replace the default header module by user defined module") - generateCmd.Flags().StringArrayVar(&generateCmdOpts.AdditionalGlobalModules, "global-module", nil, "add user defined module to global modules") - generateCmd.Flags().StringArrayVar(&generateCmdOpts.AdditionalTypeModules, "type-module", nil, "add user defined module to type modules") + generateCmd.Flags().StringArrayVar(&generateCmdOpts.AdditionalGlobalModules, "global-module", nil, "add a user defined module to global modules") + generateCmd.Flags().StringArrayVar(&generateCmdOpts.AdditionalTypeModules, "type-module", nil, "add a user defined module to type modules") generateCmd.Flags().BoolVar(&generateCmdOpts.UseLegacyIndexModule, "use-legacy-index-module", false, "use legacy index func name") helpFn := generateCmd.HelpFunc() diff --git a/v2/generator/funcs.go b/v2/generator/funcs.go index 62410e1..4de8372 100644 --- a/v2/generator/funcs.go +++ b/v2/generator/funcs.go @@ -90,10 +90,10 @@ func (a *Generator) filterFields(fields []*models.Field, ignoreNames ...interfac // columnNames creates a list of the column names found in fields. // -// When escaped is true, if the column name is a reserved word, it's escaped with backquotes. +// When escaped is true, if the column name is a reserved word, it's escaped with back quotes. // // Used to present a comma separated list of column names, that can be used in -// a SELECT, or UPDATE, or other SQL clause requiring an list of identifiers +// a SELECT, or UPDATE, or other SQL clause requiring a list of identifiers // (ie, "field_1, field_2, field_3, ..."). func (a *Generator) columnNames(fields []*models.Field) string { str := "" @@ -132,8 +132,8 @@ func (a *Generator) columnNamesQuery(fields []*models.Field, sep string) string // against ShortNameTypeMap, and if not found, then the value is // calculated and stored in the ShortNameTypeMap for future use. // -// A shortname is the concatentation of the lowercase of the first character in -// the words comprising the name. For example, "MyCustomName" will have have +// A shortname is the concatenation of the lowercase of the first character in +// the words comprising the name. For example, "MyCustomName" will have // the shortname of "mcn". // // If a generated shortname conflicts with a Go reserved name, then the @@ -368,7 +368,7 @@ func (a *Generator) goEncodedParams(fields []*models.Field, addPrefix bool) stri return str } -// hascolumn takes a list of fields and determines if field with the specified +// hasColumn takes a list of fields and determines if field with the specified // column name is in the list. func (a *Generator) hasColumn(fields []*models.Field, name string) bool { for _, f := range fields { @@ -380,7 +380,7 @@ func (a *Generator) hasColumn(fields []*models.Field, name string) bool { return false } -// hasfield takes a list of fields and determines if field with the specified +// hasField takes a list of fields and determines if field with the specified // field name is in the list. func (a *Generator) hasField(fields []*models.Field, name string) bool { for _, f := range fields { diff --git a/v2/internal/reserved_keywords.go b/v2/internal/reserved_keywords.go index 1e7c878..a19780c 100644 --- a/v2/internal/reserved_keywords.go +++ b/v2/internal/reserved_keywords.go @@ -19,7 +19,7 @@ package internal -// This list was created with reference to https://cloud.google.com/spanner/docs/lexical#reserved-keywords +// This list was created with reference to https://cloud.google.com/spanner/docs/reference/standard-sql/lexical#reserved_keywords var reservedKeywords = map[string]struct{}{ "ALL": struct{}{}, "AND": struct{}{}, diff --git a/v2/internal/util.go b/v2/internal/util.go index 9e7aa51..5942fbc 100644 --- a/v2/internal/util.go +++ b/v2/internal/util.go @@ -65,10 +65,10 @@ func CamelToScake(s string) string { } // EscapeColumnName will escape a column name if using reserved keyword as column name, returning it in -// surrounded backquotes. +// surrounded back quotes. func EscapeColumnName(s string) string { if _, ok := reservedKeywords[strings.ToUpper(s)]; ok { - // return surrounded s with backquotes if reserved keyword + // return surrounded s with back quotes if reserved keyword return fmt.Sprintf("`%s`", s) } // return s if not reserved keyword