Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleconroy committed Nov 2, 2023
1 parent e3d7827 commit 8931d51
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 123 deletions.
16 changes: 15 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,18 @@ module github.com/sqlc-dev/sqlc-gen-go

go 1.21.3

require github.com/fatih/structtag v1.2.0 // indirect
require (
github.com/fatih/structtag v1.2.0
github.com/google/go-cmp v0.5.9
github.com/jinzhu/inflection v1.0.0
)

require (
github.com/golang/protobuf v1.5.3 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
25 changes: 25 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/sqlc-dev/sqlc-go v1.22.1-0.20231102170509-ee94c710d92e h1:fgvOWVSJLVvl1ETExPEw2okhTj2kbxvAQ5ECLrAtWbQ=
github.com/sqlc-dev/sqlc-go v1.22.1-0.20231102170509-ee94c710d92e/go.mod h1:SXs+GYtPBUfDJQWSOvcJesiAwj4HNfigK7EwzwpHNPk=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M=
google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
2 changes: 1 addition & 1 deletion internal/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/sqlc-dev/sqlc-gen-go/internal/opts"
"buf.build/gen/go/sqlc/sqlc/protocolbuffers/go/protos/plugin"
"github.com/sqlc-dev/sqlc-go/plugin"
)

type Field struct {
Expand Down
13 changes: 5 additions & 8 deletions internal/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import (
"errors"
"fmt"
"go/format"
"log"
"strings"
"text/template"

"buf.build/gen/go/sqlc/sqlc/protocolbuffers/go/protos/plugin"
"github.com/sqlc-dev/sqlc-gen-go/internal/opts"
"github.com/sqlc-dev/sqlc-go/metadata"
"github.com/sqlc-dev/sqlc-go/sdk"
"github.com/sqlc-dev/sqlc-go/metadata"
"github.com/sqlc-dev/sqlc-go/plugin"
)

type tmplCtx struct {
Expand Down Expand Up @@ -104,7 +103,7 @@ func (t *tmplCtx) codegenQueryRetval(q Query) (string, error) {
}
}

func Generate(ctx context.Context, req *plugin.CodeGenRequest) (*plugin.CodeGenResponse, error) {
func Generate(ctx context.Context, req *plugin.GenerateRequest) (*plugin.GenerateResponse, error) {
options, err := opts.Parse(req)
if err != nil {
return nil, err
Expand All @@ -125,12 +124,10 @@ func Generate(ctx context.Context, req *plugin.CodeGenRequest) (*plugin.CodeGenR
enums, structs = filterUnusedStructs(enums, structs, queries)
}

log.Println("HELLO")

return generate(req, options, enums, structs, queries)
}

func generate(req *plugin.CodeGenRequest, options *opts.Options, enums []Enum, structs []Struct, queries []Query) (*plugin.CodeGenResponse, error) {
func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum, structs []Struct, queries []Query) (*plugin.GenerateResponse, error) {
i := &importer{
Options: options,
Queries: queries,
Expand Down Expand Up @@ -285,7 +282,7 @@ func generate(req *plugin.CodeGenRequest, options *opts.Options, enums []Enum, s
return nil, err
}
}
resp := plugin.CodeGenResponse{}
resp := plugin.GenerateResponse{}

for filename, code := range output {
resp.Files = append(resp.Files, &plugin.File{
Expand Down
14 changes: 7 additions & 7 deletions internal/go_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (

"github.com/sqlc-dev/sqlc-gen-go/internal/opts"
"github.com/sqlc-dev/sqlc-go/sdk"
"buf.build/gen/go/sqlc/sqlc/protocolbuffers/go/protos/plugin"
"github.com/sqlc-dev/sqlc-go/plugin"
)

func addExtraGoStructTags(tags map[string]string, req *plugin.CodeGenRequest, options *opts.Options, col *plugin.Column) {
func addExtraGoStructTags(tags map[string]string, req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) {
for _, override := range options.Overrides {
oride := override.Plugin
oride := override.ShimOverride
if oride.GoType.StructTags == nil {
continue
}
Expand All @@ -33,10 +33,10 @@ func addExtraGoStructTags(tags map[string]string, req *plugin.CodeGenRequest, op
}
}

func goType(req *plugin.CodeGenRequest, options *opts.Options, col *plugin.Column) string {
func goType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) string {
// Check if the column's type has been overridden
for _, override := range options.Overrides {
oride := override.Plugin
oride := override.ShimOverride

if oride.GoType.TypeName == "" {
continue
Expand All @@ -63,13 +63,13 @@ func goType(req *plugin.CodeGenRequest, options *opts.Options, col *plugin.Colum
return typ
}

func goInnerType(req *plugin.CodeGenRequest, options *opts.Options, col *plugin.Column) string {
func goInnerType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) string {
columnType := sdk.DataType(col.Type)
notNull := col.NotNull || col.IsArray

// package overrides have a higher precedence
for _, override := range options.Overrides {
oride := override.Plugin
oride := override.ShimOverride
if oride.GoType.TypeName == "" {
continue
}
Expand Down
4 changes: 2 additions & 2 deletions internal/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func buildImports(options *opts.Options, queries []Query, uses func(string) bool

overrideTypes := map[string]string{}
for _, override := range options.Overrides {
o := override.Plugin
o := override.ShimOverride
if o.GoType.BasicType || o.GoType.TypeName == "" {
continue
}
Expand All @@ -226,7 +226,7 @@ func buildImports(options *opts.Options, queries []Query, uses func(string) bool

// Custom imports
for _, override := range options.Overrides {
o := override.Plugin
o := override.ShimOverride

if o.GoType.BasicType || o.GoType.TypeName == "" {
continue
Expand Down
4 changes: 2 additions & 2 deletions internal/mysql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"github.com/sqlc-dev/sqlc-gen-go/internal/opts"
"github.com/sqlc-dev/sqlc-go/sdk"
"github.com/sqlc-dev/sqlc-gen-go/internal/debug"
"buf.build/gen/go/sqlc/sqlc/protocolbuffers/go/protos/plugin"
"github.com/sqlc-dev/sqlc-go/plugin"
)

func mysqlType(req *plugin.CodeGenRequest, options *opts.Options, col *plugin.Column) string {
func mysqlType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) string {
columnType := sdk.DataType(col.Type)
notNull := col.NotNull || col.IsArray
unsigned := col.Unsigned
Expand Down
38 changes: 17 additions & 21 deletions internal/opts/options.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package opts

import (
"bytes"
"encoding/json"
"fmt"
"maps"

"buf.build/gen/go/sqlc/sqlc/protocolbuffers/go/protos/plugin"
"github.com/sqlc-dev/sqlc-go/plugin"
)

type Options struct {
Expand Down Expand Up @@ -48,7 +47,7 @@ type GlobalOptions struct {
Rename map[string]string `json:"rename,omitempty" yaml:"rename"`
}

func Parse(req *plugin.CodeGenRequest) (*Options, error) {
func Parse(req *plugin.GenerateRequest) (*Options, error) {
options, err := parseOpts(req)
if err != nil {
return nil, err
Expand All @@ -69,14 +68,13 @@ func Parse(req *plugin.CodeGenRequest) (*Options, error) {
return options, nil
}

func parseOpts(req *plugin.CodeGenRequest) (*Options, error) {
func parseOpts(req *plugin.GenerateRequest) (*Options, error) {
var options Options
if len(req.PluginOptions) == 0 {
return &options, nil
}
dec := json.NewDecoder(bytes.NewReader(req.PluginOptions))
if err := dec.Decode(&options); err != nil {
return &options, fmt.Errorf("unmarshalling options: %w", err)
if err := json.Unmarshal(req.PluginOptions, &options); err != nil {
return nil, fmt.Errorf("unmarshalling plugin options: %w", err)
}

for i := range options.Overrides {
Expand All @@ -93,21 +91,19 @@ func parseOpts(req *plugin.CodeGenRequest) (*Options, error) {
return &options, nil
}

func parseGlobalOpts(req *plugin.CodeGenRequest) (*GlobalOptions, error) {
func parseGlobalOpts(req *plugin.GenerateRequest) (*GlobalOptions, error) {
var options GlobalOptions
return &options, nil
// if len(req.GlobalOptions) == 0 {
// return &options, nil
// }
// dec := json.NewDecoder(bytes.NewReader(req.GlobalOptions))
// if err := dec.Decode(&options); err != nil {
// return &options, fmt.Errorf("unmarshalling options: %w", err)
// }
// for i := range options.Overrides {
// if err := options.Overrides[i].parse(req); err != nil {
// return nil, err
// }
// }
if len(req.GlobalOptions) == 0 {
return &options, nil
}
if err := json.Unmarshal(req.GlobalOptions, &options); err != nil {
return nil, fmt.Errorf("unmarshalling global options: %w", err)
}
for i := range options.Overrides {
if err := options.Overrides[i].parse(req); err != nil {
return nil, err
}
}
return &options, nil
}

Expand Down
73 changes: 4 additions & 69 deletions internal/opts/override.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,9 @@ import (
"strings"

"github.com/sqlc-dev/sqlc-go/pattern"
"buf.build/gen/go/sqlc/sqlc/protocolbuffers/go/protos/plugin"
"github.com/sqlc-dev/sqlc-go/plugin"
)

type PluginGoType struct {
ImportPath string
Package string
TypeName string
BasicType bool
StructTags map[string]string
}

func pluginGoType(o *Override) *PluginGoType {
// Note that there is a slight mismatch between this and the
// proto api. The GoType on the override is the unparsed type,
// which could be a qualified path or an object, as per
// https://docs.sqlc.dev/en/v1.18.0/reference/config.html#type-overriding
return &PluginGoType{
ImportPath: o.GoImportPath,
Package: o.GoPackage,
TypeName: o.GoTypeName,
BasicType: o.GoBasicType,
StructTags: o.GoStructTags,
}
}

type PluginOverride struct {
DbType string
Nullable bool
Column string
Table *plugin.Identifier
ColumnName string
Unsigned bool
GoType *PluginGoType
}

func pluginOverride(req *plugin.CodeGenRequest, o *Override) *PluginOverride {
var column string
var table plugin.Identifier

if o.Column != "" {
colParts := strings.Split(o.Column, ".")
switch len(colParts) {
case 2:
table.Schema = req.Catalog.DefaultSchema
table.Name = colParts[0]
column = colParts[1]
case 3:
table.Schema = colParts[0]
table.Name = colParts[1]
column = colParts[2]
case 4:
table.Catalog = colParts[0]
table.Schema = colParts[1]
table.Name = colParts[2]
column = colParts[3]
}
}
return &PluginOverride{
DbType: o.DBType,
Nullable: o.Nullable,
Unsigned: o.Unsigned,
Column: o.Column,
ColumnName: column,
Table: &table,
GoType: pluginGoType(o),
}
}

type Override struct {
// name of the golang type to use, e.g. `github.com/segmentio/ksuid.KSUID`
GoType GoType `json:"go_type" yaml:"go_type"`
Expand Down Expand Up @@ -112,7 +47,7 @@ type Override struct {

// Parsed form of GoStructTag, e.g. {"validate:", "required"}
GoStructTags map[string]string `json:"-"`
Plugin *PluginOverride `json:"-"`
ShimOverride *ShimOverride `json:"-"`
}

func (o *Override) Matches(n *plugin.Identifier, defaultSchema string) bool {
Expand Down Expand Up @@ -141,7 +76,7 @@ func (o *Override) Matches(n *plugin.Identifier, defaultSchema string) bool {
return true
}

func (o *Override) parse(req *plugin.CodeGenRequest) (err error) {
func (o *Override) parse(req *plugin.GenerateRequest) (err error) {
// validate deprecated postgres_type field
if o.Deprecated_PostgresType != "" {
fmt.Fprintf(os.Stderr, "WARNING: \"postgres_type\" is deprecated. Instead, use \"db_type\" to specify a type override.\n")
Expand Down Expand Up @@ -229,6 +164,6 @@ func (o *Override) parse(req *plugin.CodeGenRequest) (err error) {
}
o.GoStructTags = tags

o.Plugin = pluginOverride(req, o)
o.ShimOverride = shimOverride(req, o)
return nil
}
Loading

0 comments on commit 8931d51

Please sign in to comment.