Skip to content

fix(codegen/golang): Move more Go-specific config validation into the plugin #2951

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
Nov 6, 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
14 changes: 13 additions & 1 deletion internal/codegen/golang/opts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"maps"
"path/filepath"

"github.com/sqlc-dev/sqlc/internal/plugin"
)
Expand All @@ -16,7 +17,7 @@ type Options struct {
EmitPreparedQueries bool `json:"emit_prepared_queries" yaml:"emit_prepared_queries"`
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`
EmitEmptySlices bool `json:"emit_empty_slices,omitempty" yaml:"emit_empty_slices"`
EmitExportedQueries bool `json:"emit_exported_queries" yaml:"emit_exported_queries`
EmitExportedQueries bool `json:"emit_exported_queries" yaml:"emit_exported_queries"`
EmitResultStructPointers bool `json:"emit_result_struct_pointers" yaml:"emit_result_struct_pointers"`
EmitParamsStructPointers bool `json:"emit_params_struct_pointers" yaml:"emit_params_struct_pointers"`
EmitMethodsWithDbArgument bool `json:"emit_methods_with_db_argument,omitempty" yaml:"emit_methods_with_db_argument"`
Expand Down Expand Up @@ -77,6 +78,14 @@ func parseOpts(req *plugin.GenerateRequest) (*Options, error) {
return nil, fmt.Errorf("unmarshalling plugin options: %w", err)
}

if options.Package == "" {
if options.Out != "" {
options.Package = filepath.Base(options.Out)
} else {
return nil, fmt.Errorf("invalid options: missing package name")
}
}

for i := range options.Overrides {
if err := options.Overrides[i].parse(req); err != nil {
return nil, err
Expand Down Expand Up @@ -111,6 +120,9 @@ 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")
}
if *opts.QueryParameterLimit < 0 {
return fmt.Errorf("invalid options: query parameter limit must not be negative")
}

return nil
}
2 changes: 0 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,11 @@ type SQLJSON struct {
var ErrMissingEngine = errors.New("unknown engine")
var ErrMissingVersion = errors.New("no version number")
var ErrNoOutPath = errors.New("no output path")
var ErrNoPackageName = errors.New("missing package name")
var ErrNoPackagePath = errors.New("missing package path")
var ErrNoPackages = errors.New("no packages")
var ErrNoQuerierType = errors.New("no querier emit type enabled")
var ErrUnknownEngine = errors.New("invalid engine")
var ErrUnknownVersion = errors.New("invalid version number")
var ErrInvalidQueryParameterLimit = errors.New("invalid query parameter limit")

var ErrPluginBuiltin = errors.New("a built-in plugin with that name already exists")
var ErrPluginNoName = errors.New("missing plugin name")
Expand Down
9 changes: 0 additions & 9 deletions internal/config/v_one.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,6 @@ func v1ParseConfig(rd io.Reader) (Config, error) {
return config, ErrNoPackagePath
}

if settings.Packages[j].QueryParameterLimit != nil && (*settings.Packages[j].QueryParameterLimit < 0) {
return config, ErrInvalidQueryParameterLimit
}

if settings.Packages[j].QueryParameterLimit == nil {
settings.Packages[j].QueryParameterLimit = new(int32)
*settings.Packages[j].QueryParameterLimit = 1
}

if settings.Packages[j].Name == "" {
settings.Packages[j].Name = filepath.Base(settings.Packages[j].Path)
}
Expand Down
13 changes: 0 additions & 13 deletions internal/config/v_two.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package config
import (
"fmt"
"io"
"path/filepath"

yaml "gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -64,18 +63,6 @@ func v2ParseConfig(rd io.Reader) (Config, error) {
if conf.SQL[j].Gen.Go.Out == "" {
return conf, ErrNoPackagePath
}
if conf.SQL[j].Gen.Go.Package == "" {
conf.SQL[j].Gen.Go.Package = filepath.Base(conf.SQL[j].Gen.Go.Out)
}

if conf.SQL[j].Gen.Go.QueryParameterLimit != nil && (*conf.SQL[j].Gen.Go.QueryParameterLimit < 0) {
return conf, ErrInvalidQueryParameterLimit
}

if conf.SQL[j].Gen.Go.QueryParameterLimit == nil {
conf.SQL[j].Gen.Go.QueryParameterLimit = new(int32)
*conf.SQL[j].Gen.Go.QueryParameterLimit = 1
}
}
if conf.SQL[j].Gen.JSON != nil {
if conf.SQL[j].Gen.JSON.Out == "" {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
-- Example queries for sqlc
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text,
country_code CHAR(2) NOT NULL
);

-- name: GetAuthor :one
SELECT * FROM authors
WHERE name = $1 AND country_code = $2 LIMIT 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text,
country_code CHAR(2) NOT NULL
);
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"path": "go",
"engine": "postgresql",
"name": "querytest",
"schema": "query.sql",
"schema": "schema.sql",
"queries": "query.sql",
"query_parameter_limit": -1
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
error parsing sqlc.json: invalid query parameter limit
# package querytest
error generating code: invalid options: query parameter limit must not be negative