diff --git a/internal/codegen/golang/field.go b/internal/codegen/golang/field.go index 2a8d9ccdfc..2a63b6d342 100644 --- a/internal/codegen/golang/field.go +++ b/internal/codegen/golang/field.go @@ -6,6 +6,7 @@ import ( "sort" "strings" + "github.com/sqlc-dev/sqlc/internal/codegen/golang/opts" "github.com/sqlc-dev/sqlc/internal/plugin" ) @@ -40,7 +41,7 @@ func TagsToString(tags map[string]string) string { return strings.Join(tagParts, " ") } -func JSONTagName(name string, options *opts) string { +func JSONTagName(name string, options *opts.Options) string { style := options.JsonTagsCaseStyle idUppercase := options.JsonTagsIdUppercase if style == "" || style == "none" { diff --git a/internal/codegen/golang/gen.go b/internal/codegen/golang/gen.go index ea3d8384cd..6daa977872 100644 --- a/internal/codegen/golang/gen.go +++ b/internal/codegen/golang/gen.go @@ -10,6 +10,7 @@ import ( "strings" "text/template" + "github.com/sqlc-dev/sqlc/internal/codegen/golang/opts" "github.com/sqlc-dev/sqlc/internal/codegen/sdk" "github.com/sqlc-dev/sqlc/internal/metadata" "github.com/sqlc-dev/sqlc/internal/plugin" @@ -103,12 +104,12 @@ func (t *tmplCtx) codegenQueryRetval(q Query) (string, error) { } func Generate(ctx context.Context, req *plugin.CodeGenRequest) (*plugin.CodeGenResponse, error) { - options, err := parseOpts(req) + options, err := opts.ParseOpts(req) if err != nil { return nil, err } - if err := validateOpts(options); err != nil { + if err := opts.ValidateOpts(options); err != nil { return nil, err } @@ -126,7 +127,7 @@ func Generate(ctx context.Context, req *plugin.CodeGenRequest) (*plugin.CodeGenR return generate(req, options, enums, structs, queries) } -func generate(req *plugin.CodeGenRequest, options *opts, enums []Enum, structs []Struct, queries []Query) (*plugin.CodeGenResponse, error) { +func generate(req *plugin.CodeGenRequest, options *opts.Options, enums []Enum, structs []Struct, queries []Query) (*plugin.CodeGenResponse, error) { i := &importer{ Settings: req.Settings, Options: options, diff --git a/internal/codegen/golang/go_type.go b/internal/codegen/golang/go_type.go index 2b5f75bcd4..6adb4b5058 100644 --- a/internal/codegen/golang/go_type.go +++ b/internal/codegen/golang/go_type.go @@ -3,6 +3,7 @@ package golang import ( "strings" + "github.com/sqlc-dev/sqlc/internal/codegen/golang/opts" "github.com/sqlc-dev/sqlc/internal/codegen/sdk" "github.com/sqlc-dev/sqlc/internal/plugin" ) @@ -31,7 +32,7 @@ func addExtraGoStructTags(tags map[string]string, req *plugin.CodeGenRequest, co } } -func goType(req *plugin.CodeGenRequest, options *opts, col *plugin.Column) string { +func goType(req *plugin.CodeGenRequest, options *opts.Options, col *plugin.Column) string { // Check if the column's type has been overridden for _, oride := range req.Settings.Overrides { if oride.GoType.TypeName == "" { @@ -59,7 +60,7 @@ func goType(req *plugin.CodeGenRequest, options *opts, col *plugin.Column) strin return typ } -func goInnerType(req *plugin.CodeGenRequest, options *opts, col *plugin.Column) string { +func goInnerType(req *plugin.CodeGenRequest, options *opts.Options, col *plugin.Column) string { columnType := sdk.DataType(col.Type) notNull := col.NotNull || col.IsArray diff --git a/internal/codegen/golang/imports.go b/internal/codegen/golang/imports.go index 11e3dc5b46..c436e68e99 100644 --- a/internal/codegen/golang/imports.go +++ b/internal/codegen/golang/imports.go @@ -5,6 +5,7 @@ import ( "sort" "strings" + "github.com/sqlc-dev/sqlc/internal/codegen/golang/opts" "github.com/sqlc-dev/sqlc/internal/metadata" "github.com/sqlc-dev/sqlc/internal/plugin" ) @@ -59,7 +60,7 @@ func mergeImports(imps ...fileImports) [][]ImportSpec { type importer struct { Settings *plugin.Settings - Options *opts + Options *opts.Options Queries []Query Enums []Enum Structs []Struct @@ -156,7 +157,7 @@ var pqtypeTypes = map[string]struct{}{ "pqtype.NullRawMessage": {}, } -func buildImports(settings *plugin.Settings, options *opts, queries []Query, uses func(string) bool) (map[string]struct{}, map[ImportSpec]struct{}) { +func buildImports(settings *plugin.Settings, options *opts.Options, queries []Query, uses func(string) bool) (map[string]struct{}, map[ImportSpec]struct{}) { pkg := make(map[ImportSpec]struct{}) std := make(map[string]struct{}) diff --git a/internal/codegen/golang/opts.go b/internal/codegen/golang/opts/options.go similarity index 94% rename from internal/codegen/golang/opts.go rename to internal/codegen/golang/opts/options.go index 53be536881..2e07b7a025 100644 --- a/internal/codegen/golang/opts.go +++ b/internal/codegen/golang/opts/options.go @@ -1,4 +1,4 @@ -package golang +package opts import ( "bytes" @@ -8,7 +8,7 @@ import ( "github.com/sqlc-dev/sqlc/internal/plugin" ) -type opts struct { +type Options struct { EmitInterface bool `json:"emit_interface"` EmitJsonTags bool `json:"emit_json_tags"` JsonTagsIdUppercase bool `json:"json_tags_id_uppercase"` @@ -44,8 +44,8 @@ type opts struct { Rename json.RawMessage `json:"rename,omitempty"` } -func parseOpts(req *plugin.CodeGenRequest) (*opts, error) { - var options *opts +func ParseOpts(req *plugin.CodeGenRequest) (*Options, error) { + var options *Options dec := json.NewDecoder(bytes.NewReader(req.PluginOptions)) dec.DisallowUnknownFields() if err := dec.Decode(&options); err != nil { @@ -60,7 +60,7 @@ func parseOpts(req *plugin.CodeGenRequest) (*opts, error) { return options, nil } -func validateOpts(opts *opts) error { +func ValidateOpts(opts *Options) error { if opts.EmitMethodsWithDbArgument && opts.EmitPreparedQueries { return fmt.Errorf("invalid options: emit_methods_with_db_argument and emit_prepared_queries options are mutually exclusive") } diff --git a/internal/codegen/golang/postgresql_type.go b/internal/codegen/golang/postgresql_type.go index 54a7f02d44..4b4cf25a0e 100644 --- a/internal/codegen/golang/postgresql_type.go +++ b/internal/codegen/golang/postgresql_type.go @@ -5,6 +5,7 @@ import ( "log" "strings" + "github.com/sqlc-dev/sqlc/internal/codegen/golang/opts" "github.com/sqlc-dev/sqlc/internal/codegen/sdk" "github.com/sqlc-dev/sqlc/internal/debug" "github.com/sqlc-dev/sqlc/internal/plugin" @@ -33,7 +34,7 @@ func parseIdentifierString(name string) (*plugin.Identifier, error) { } } -func postgresType(req *plugin.CodeGenRequest, options *opts, col *plugin.Column) string { +func postgresType(req *plugin.CodeGenRequest, options *opts.Options, col *plugin.Column) string { columnType := sdk.DataType(col.Type) notNull := col.NotNull || col.IsArray driver := parseDriver(options.SqlPackage) diff --git a/internal/codegen/golang/result.go b/internal/codegen/golang/result.go index 03e2d21fcd..e8c97df372 100644 --- a/internal/codegen/golang/result.go +++ b/internal/codegen/golang/result.go @@ -5,13 +5,14 @@ import ( "sort" "strings" + "github.com/sqlc-dev/sqlc/internal/codegen/golang/opts" "github.com/sqlc-dev/sqlc/internal/codegen/sdk" "github.com/sqlc-dev/sqlc/internal/inflection" "github.com/sqlc-dev/sqlc/internal/metadata" "github.com/sqlc-dev/sqlc/internal/plugin" ) -func buildEnums(req *plugin.CodeGenRequest, options *opts) []Enum { +func buildEnums(req *plugin.CodeGenRequest, options *opts.Options) []Enum { var enums []Enum for _, schema := range req.Catalog.Schemas { if schema.Name == "pg_catalog" || schema.Name == "information_schema" { @@ -58,7 +59,7 @@ func buildEnums(req *plugin.CodeGenRequest, options *opts) []Enum { return enums } -func buildStructs(req *plugin.CodeGenRequest, options *opts) []Struct { +func buildStructs(req *plugin.CodeGenRequest, options *opts.Options) []Struct { var structs []Struct for _, schema := range req.Catalog.Schemas { if schema.Name == "pg_catalog" || schema.Name == "information_schema" { @@ -181,7 +182,7 @@ func argName(name string) string { return out } -func buildQueries(req *plugin.CodeGenRequest, options *opts, structs []Struct) ([]Query, error) { +func buildQueries(req *plugin.CodeGenRequest, options *opts.Options, structs []Struct) ([]Query, error) { qs := make([]Query, 0, len(req.Queries)) for _, query := range req.Queries { if query.Name == "" { @@ -331,7 +332,7 @@ func putOutColumns(query *plugin.Query) bool { // JSON tags: count, count_2, count_2 // // This is unlikely to happen, so don't fix it yet -func columnsToStruct(req *plugin.CodeGenRequest, options *opts, name string, columns []goColumn, useID bool) (*Struct, error) { +func columnsToStruct(req *plugin.CodeGenRequest, options *opts.Options, name string, columns []goColumn, useID bool) (*Struct, error) { gs := Struct{ Name: name, }