Skip to content

Commit bd1f56e

Browse files
committed
throw error on wrong external plugin options
1 parent 2f48010 commit bd1f56e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

internal/cmd/generate.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,9 @@ func codegen(ctx context.Context, combo config.CombinedSettings, sql outputPair,
438438
if err != nil {
439439
return "", nil, fmt.Errorf("invalid plugin options: %w", err)
440440
}
441+
if err := validateExternalPluginOptions(opts, sql.Plugin.Plugin); err != nil {
442+
return "", nil, err
443+
}
441444
req.PluginOptions = opts
442445

443446
case sql.Gen.Go != nil:
@@ -473,3 +476,28 @@ func codegen(ctx context.Context, combo config.CombinedSettings, sql outputPair,
473476
resp, err := client.Generate(ctx, req)
474477
return out, resp, err
475478
}
479+
480+
// driverOnlyOptions are options that apply only to built-in Go codegen, not to external plugins.
481+
var driverOnlyOptions = []string{"sql_package", "sql_driver"}
482+
483+
// validateExternalPluginOptions returns an error if plugin options contain sql_package or sql_driver.
484+
// External codegen plugins define their own database driver; these options are not supported.
485+
func validateExternalPluginOptions(opts []byte, pluginName string) error {
486+
if len(opts) == 0 {
487+
return nil
488+
}
489+
var m map[string]interface{}
490+
if err := json.Unmarshal(opts, &m); err != nil {
491+
return nil
492+
}
493+
var invalid []string
494+
for _, key := range driverOnlyOptions {
495+
if _, ok := m[key]; ok {
496+
invalid = append(invalid, key)
497+
}
498+
}
499+
if len(invalid) == 0 {
500+
return nil
501+
}
502+
return fmt.Errorf("plugin %q: options %q are not supported for external codegen plugins; the plugin defines its own database driver (these options only apply to built-in Go codegen)", pluginName, invalid)
503+
}

0 commit comments

Comments
 (0)