Repository: sqlc-dev/sqlc
Pull request #2951 (2023-11-06 23:07:38)

fix(codegen/golang): Move more Go-specific config validation into the plugin
Resolves: https://github.com/sqlc-dev/sqlc/issues/2950


Patch:
diff --git a/internal/codegen/golang/opts/options.go b/internal/codegen/golang/opts/options.go
index 513d21ef2..343dd8ae0 100644
--- a/internal/codegen/golang/opts/options.go
+++ b/internal/codegen/golang/opts/options.go
@@ -4,6 +4,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"maps"
+	"path/filepath"
 
 	"github.com/sqlc-dev/sqlc/internal/plugin"
 )
@@ -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"`
@@ -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
@@ -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
 }
diff --git a/internal/config/config.go b/internal/config/config.go
index e11f40de4..7decfe369 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -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")
diff --git a/internal/config/v_one.go b/internal/config/v_one.go
index be3c3a9a3..a67511869 100644
--- a/internal/config/v_one.go
+++ b/internal/config/v_one.go
@@ -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)
 		}
diff --git a/internal/config/v_two.go b/internal/config/v_two.go
index 30a8f5837..1d3b224dd 100644
--- a/internal/config/v_two.go
+++ b/internal/config/v_two.go
@@ -3,7 +3,6 @@ package config
 import (
 	"fmt"
 	"io"
-	"path/filepath"
 
 	yaml "gopkg.in/yaml.v3"
 )
@@ -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 == "" {


Test patch:
diff --git a/internal/endtoend/testdata/query_parameter_limit_invalid/postgresql/query.sql b/internal/endtoend/testdata/query_parameter_limit_invalid/postgresql/query.sql
index eb10e5cb4..a71d66246 100644
--- a/internal/endtoend/testdata/query_parameter_limit_invalid/postgresql/query.sql
+++ b/internal/endtoend/testdata/query_parameter_limit_invalid/postgresql/query.sql
@@ -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;
diff --git a/internal/endtoend/testdata/query_parameter_limit_invalid/postgresql/schema.sql b/internal/endtoend/testdata/query_parameter_limit_invalid/postgresql/schema.sql
new file mode 100644
index 000000000..6027f56d9
--- /dev/null
+++ b/internal/endtoend/testdata/query_parameter_limit_invalid/postgresql/schema.sql
@@ -0,0 +1,6 @@
+CREATE TABLE authors (
+  id   BIGSERIAL PRIMARY KEY,
+  name text      NOT NULL,
+  bio  text,
+  country_code CHAR(2) NOT NULL
+);
diff --git a/internal/endtoend/testdata/query_parameter_limit_invalid/postgresql/sqlc.json b/internal/endtoend/testdata/query_parameter_limit_invalid/postgresql/sqlc.json
index 4d1fb3f94..c47327091 100644
--- a/internal/endtoend/testdata/query_parameter_limit_invalid/postgresql/sqlc.json
+++ b/internal/endtoend/testdata/query_parameter_limit_invalid/postgresql/sqlc.json
@@ -5,7 +5,7 @@
         "path": "go",
         "engine": "postgresql",
         "name": "querytest",
-        "schema": "query.sql",
+        "schema": "schema.sql",
         "queries": "query.sql",
         "query_parameter_limit": -1
       }
diff --git a/internal/endtoend/testdata/query_parameter_limit_invalid/postgresql/stderr.txt b/internal/endtoend/testdata/query_parameter_limit_invalid/postgresql/stderr.txt
index 2f0a7a559..9e1760ffc 100644
--- a/internal/endtoend/testdata/query_parameter_limit_invalid/postgresql/stderr.txt
+++ b/internal/endtoend/testdata/query_parameter_limit_invalid/postgresql/stderr.txt
@@ -1,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