Skip to content

Commit

Permalink
try as override options type
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmbenton committed Oct 27, 2023
1 parent bcbe477 commit af4a202
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 13 deletions.
10 changes: 2 additions & 8 deletions internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"encoding/json"
"fmt"
"strings"

gopluginopts "github.com/sqlc-dev/sqlc/internal/codegen/golang/opts"
Expand Down Expand Up @@ -39,7 +38,7 @@ func pluginOverride(r *compiler.Result, o config.Override) *plugin.Override {

var options []byte
var err error
if o.Options.IsZero() {
if len(o.Options) == 0 {
// Send go-specific override information to the go codegen plugin
options, err = json.Marshal(gopluginopts.OverrideOptions{
GoType: o.GoType,
Expand All @@ -49,12 +48,7 @@ func pluginOverride(r *compiler.Result, o config.Override) *plugin.Override {
panic(err) // TODO don't panic, return err
}
} else {
options, err = convert.YAMLtoJSON(o.Options)
if err != nil {
panic(err)
}

fmt.Printf(">>> %s", string(options))
options = o.Options
}

return &plugin.Override{
Expand Down
1 change: 0 additions & 1 deletion internal/codegen/golang/opts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func ParseOpts(req *plugin.CodeGenRequest) (*Options, error) {
var options *Options
dec := json.NewDecoder(bytes.NewReader(req.PluginOptions))
dec.DisallowUnknownFields()
fmt.Printf("### %s", string(req.PluginOptions))
if err := dec.Decode(&options); err != nil {
return options, fmt.Errorf("unmarshalling options: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/config/override.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package config

import (
"encoding/json"
"fmt"
"os"
"strings"

gopluginopts "github.com/sqlc-dev/sqlc/internal/codegen/golang/opts"
"github.com/sqlc-dev/sqlc/internal/pattern"
"gopkg.in/yaml.v3"
)

type Override struct {
Expand Down Expand Up @@ -43,8 +43,8 @@ type Override struct {
TableRel *pattern.Match `json:"-"`

// For passing plugin-specific configuration
Plugin string `json:"plugin,omitempty"`
Options yaml.Node `json:"options,omitempty"`
Plugin string `json:"plugin,omitempty"`
Options json.RawMessage `json:"options,omitempty"`
}

func (o Override) hasGoOptions() bool {
Expand Down Expand Up @@ -78,7 +78,7 @@ func (o *Override) Parse() (err error) {
return fmt.Errorf("Override specifying both `column` (%q) and `db_type` (%q) is not valid.", o.Column, o.DBType)
case o.Column == "" && o.DBType == "":
return fmt.Errorf("Override must specify one of either `column` or `db_type`")
case o.hasGoOptions() && !o.Options.IsZero():
case o.hasGoOptions() && len(o.Options) > 0:
return fmt.Errorf("Override can specify go_type/go_struct_tag or options but not both")
}

Expand Down

0 comments on commit af4a202

Please sign in to comment.