Skip to content

Commit bcbe477

Browse files
committed
attempt to do a better job passing plugin-specific override information
1 parent 9c7d17d commit bcbe477

File tree

8 files changed

+373
-309
lines changed

8 files changed

+373
-309
lines changed

internal/cmd/shim.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package cmd
22

33
import (
4+
"encoding/json"
5+
"fmt"
46
"strings"
57

8+
gopluginopts "github.com/sqlc-dev/sqlc/internal/codegen/golang/opts"
69
"github.com/sqlc-dev/sqlc/internal/compiler"
710
"github.com/sqlc-dev/sqlc/internal/config"
811
"github.com/sqlc-dev/sqlc/internal/config/convert"
@@ -34,18 +37,39 @@ func pluginOverride(r *compiler.Result, o config.Override) *plugin.Override {
3437
}
3538
}
3639

40+
var options []byte
41+
var err error
42+
if o.Options.IsZero() {
43+
// Send go-specific override information to the go codegen plugin
44+
options, err = json.Marshal(gopluginopts.OverrideOptions{
45+
GoType: o.GoType,
46+
GoStructTag: o.GoStructTag,
47+
})
48+
if err != nil {
49+
panic(err) // TODO don't panic, return err
50+
}
51+
} else {
52+
options, err = convert.YAMLtoJSON(o.Options)
53+
if err != nil {
54+
panic(err)
55+
}
56+
57+
fmt.Printf(">>> %s", string(options))
58+
}
59+
3760
return &plugin.Override{
38-
CodeType: o.CodeType,
3961
DbType: o.DBType,
4062
Nullable: o.Nullable,
4163
Unsigned: o.Unsigned,
4264
Column: o.Column,
4365
ColumnName: column,
4466
Table: &table,
67+
Options: options,
4568
}
4669
}
4770

4871
func pluginSettings(r *compiler.Result, cs config.CombinedSettings) *plugin.Settings {
72+
// TODO only send overrides meant for this plugin
4973
var over []*plugin.Override
5074
for _, o := range cs.Overrides {
5175
over = append(over, pluginOverride(r, o))

internal/codegen/golang/opts/options.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,29 @@ func ParseOpts(req *plugin.CodeGenRequest) (*Options, error) {
4949
var options *Options
5050
dec := json.NewDecoder(bytes.NewReader(req.PluginOptions))
5151
dec.DisallowUnknownFields()
52+
fmt.Printf("### %s", string(req.PluginOptions))
5253
if err := dec.Decode(&options); err != nil {
5354
return options, fmt.Errorf("unmarshalling options: %w", err)
5455
}
5556

5657
for _, override := range req.Settings.Overrides {
57-
var actualOverride Override
58-
if err := json.Unmarshal(override.CodeType, &actualOverride); err != nil {
58+
var overrideOpts OverrideOptions
59+
if err := json.Unmarshal(override.Options, &overrideOpts); err != nil {
5960
return options, err
6061
}
61-
if err := actualOverride.Parse(); err != nil {
62+
parsedGoType, err := overrideOpts.GoType.Parse()
63+
if err != nil {
6264
return options, err
6365
}
64-
options.Overrides = append(options.Overrides, NewGoOverride(
66+
parsedGoStructTags, err := overrideOpts.GoStructTag.Parse()
67+
if err != nil {
68+
return options, err
69+
}
70+
parsedGoType.StructTags = parsedGoStructTags
71+
options.Overrides = append(options.Overrides, GoOverride{
6572
override,
66-
actualOverride,
67-
))
73+
parsedGoType,
74+
})
6875
}
6976

7077
// in sqlc config.Combine() the "package"-level overrides were appended to

internal/codegen/golang/opts/override.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ type Override struct {
4747

4848
// Parsed form of GoStructTag, e.g. {"validate:", "required"}
4949
GoStructTags map[string]string `json:"-"`
50+
51+
// For passing plugin-specific configuration
52+
Plugin string `json:"plugin"` // Irrelevant here in the plugin context
53+
Options OverrideOptions `json:"options"`
54+
}
55+
56+
type OverrideOptions struct {
57+
GoType GoType `json:"go_type"`
58+
GoStructTag GoStructTag `json:"go_struct_tag"`
5059
}
5160

5261
func (o *Override) Parse() (err error) {

internal/config/go_type.go

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1 @@
11
package config
2-
3-
import (
4-
"encoding/json"
5-
)
6-
7-
type GoType struct {
8-
Path string `json:"import" yaml:"import"`
9-
Package string `json:"package" yaml:"package"`
10-
Name string `json:"type" yaml:"type"`
11-
Pointer bool `json:"pointer" yaml:"pointer"`
12-
Slice bool `json:"slice" yaml:"slice"`
13-
Spec string
14-
BuiltIn bool
15-
}
16-
17-
func (o *GoType) UnmarshalJSON(data []byte) error {
18-
var spec string
19-
if err := json.Unmarshal(data, &spec); err == nil {
20-
*o = GoType{Spec: spec}
21-
return nil
22-
}
23-
type alias GoType
24-
var a alias
25-
if err := json.Unmarshal(data, &a); err != nil {
26-
return err
27-
}
28-
*o = GoType(a)
29-
return nil
30-
}
31-
32-
func (o *GoType) UnmarshalYAML(unmarshal func(interface{}) error) error {
33-
var spec string
34-
if err := unmarshal(&spec); err == nil {
35-
*o = GoType{Spec: spec}
36-
return nil
37-
}
38-
type alias GoType
39-
var a alias
40-
if err := unmarshal(&a); err != nil {
41-
return err
42-
}
43-
*o = GoType(a)
44-
return nil
45-
}

internal/config/override.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
package config
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"os"
76
"strings"
87

8+
gopluginopts "github.com/sqlc-dev/sqlc/internal/codegen/golang/opts"
99
"github.com/sqlc-dev/sqlc/internal/pattern"
10+
"gopkg.in/yaml.v3"
1011
)
1112

1213
type Override struct {
1314
// name of the golang type to use, e.g. `github.com/segmentio/ksuid.KSUID`
14-
GoType GoType `json:"go_type" yaml:"go_type"`
15+
GoType gopluginopts.GoType `json:"go_type" yaml:"go_type"`
1516

1617
// additional Go struct tags to add to this field, in raw Go struct tag form, e.g. `validate:"required" x:"y,z"`
1718
// see https://github.com/sqlc-dev/sqlc/issues/534
18-
GoStructTag string `json:"go_struct_tag" yaml:"go_struct_tag"`
19+
GoStructTag gopluginopts.GoStructTag `json:"go_struct_tag" yaml:"go_struct_tag"`
1920

2021
// fully qualified name of the Go type, e.g. `github.com/segmentio/ksuid.KSUID`
2122
DBType string `json:"db_type" yaml:"db_type"`
@@ -42,7 +43,17 @@ type Override struct {
4243
TableRel *pattern.Match `json:"-"`
4344

4445
// For passing plugin-specific configuration
45-
CodeType []byte `json:"-"`
46+
Plugin string `json:"plugin,omitempty"`
47+
Options yaml.Node `json:"options,omitempty"`
48+
}
49+
50+
func (o Override) hasGoOptions() bool {
51+
hasGoTypePath := o.GoType.Path != ""
52+
hasGoTypePackage := o.GoType.Package != ""
53+
hasGoTypeName := o.GoType.Name != ""
54+
hasGoType := hasGoTypePath || hasGoTypePackage || hasGoTypeName
55+
hasGoStructTag := o.GoStructTag != ""
56+
return hasGoType || hasGoStructTag
4657
}
4758

4859
func (o *Override) Parse() (err error) {
@@ -67,6 +78,8 @@ func (o *Override) Parse() (err error) {
6778
return fmt.Errorf("Override specifying both `column` (%q) and `db_type` (%q) is not valid.", o.Column, o.DBType)
6879
case o.Column == "" && o.DBType == "":
6980
return fmt.Errorf("Override must specify one of either `column` or `db_type`")
81+
case o.hasGoOptions() && !o.Options.IsZero():
82+
return fmt.Errorf("Override can specify go_type/go_struct_tag or options but not both")
7083
}
7184

7285
// validate Column
@@ -111,14 +124,5 @@ func (o *Override) Parse() (err error) {
111124
}
112125
}
113126

114-
// A simple way to get stuff into the Go codegen plugin so that
115-
// we can just call Parse() again in there
116-
codeType, err := json.Marshal(o)
117-
if err != nil {
118-
return err
119-
}
120-
121-
o.CodeType = codeType
122-
123127
return nil
124128
}

0 commit comments

Comments
 (0)