11package config
22
33import (
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
1213type 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
4859func (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