Skip to content

refactor(codegen/golang): Pull opts into its own package #2920

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

Merged
merged 1 commit into from
Oct 26, 2023
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
3 changes: 2 additions & 1 deletion internal/codegen/golang/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sort"
"strings"

"github.com/sqlc-dev/sqlc/internal/codegen/golang/opts"
"github.com/sqlc-dev/sqlc/internal/plugin"
)

Expand Down Expand Up @@ -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" {
Expand Down
7 changes: 4 additions & 3 deletions internal/codegen/golang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions internal/codegen/golang/go_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions internal/codegen/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package golang
package opts

import (
"bytes"
Expand All @@ -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"`
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
}
Expand Down
3 changes: 2 additions & 1 deletion internal/codegen/golang/postgresql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions internal/codegen/golang/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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,
}
Expand Down