Skip to content

Commit 02682bc

Browse files
authored
internal/config: Create new config package (#313)
1 parent 7ed2d3c commit 02682bc

File tree

12 files changed

+83
-70
lines changed

12 files changed

+83
-70
lines changed

internal/cmd/cmd.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import (
99
"os/exec"
1010
"path/filepath"
1111

12-
"github.com/kyleconroy/sqlc/internal/dinosql"
13-
1412
"github.com/davecgh/go-spew/spew"
1513
pg "github.com/lfittl/pg_query_go"
1614
"github.com/spf13/cobra"
15+
16+
"github.com/kyleconroy/sqlc/internal/config"
17+
"github.com/kyleconroy/sqlc/internal/dinosql"
1718
)
1819

1920
// Do runs the command logic.
@@ -74,7 +75,7 @@ var initCmd = &cobra.Command{
7475
if _, err := os.Stat("sqlc.json"); !os.IsNotExist(err) {
7576
return nil
7677
}
77-
blob, err := json.MarshalIndent(dinosql.GenerateSettings{Version: "1"}, "", " ")
78+
blob, err := json.MarshalIndent(config.GenerateSettings{Version: "1"}, "", " ")
7879
if err != nil {
7980
return err
8081
}
@@ -117,7 +118,7 @@ var checkCmd = &cobra.Command{
117118
return err
118119
}
119120

120-
settings, err := dinosql.ParseConfig(file)
121+
settings, err := config.ParseConfig(file)
121122
if err != nil {
122123
return err
123124
}

internal/cmd/generate.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"strings"
1010

11+
"github.com/kyleconroy/sqlc/internal/config"
1112
"github.com/kyleconroy/sqlc/internal/dinosql"
1213
"github.com/kyleconroy/sqlc/internal/mysql"
1314
)
@@ -39,14 +40,14 @@ func Generate(dir string, stderr io.Writer) (map[string]string, error) {
3940
return nil, err
4041
}
4142

42-
settings, err := dinosql.ParseConfig(bytes.NewReader(blob))
43+
settings, err := config.ParseConfig(bytes.NewReader(blob))
4344
if err != nil {
4445
switch err {
45-
case dinosql.ErrMissingVersion:
46+
case config.ErrMissingVersion:
4647
fmt.Fprintf(stderr, errMessageNoVersion)
47-
case dinosql.ErrUnknownVersion:
48+
case config.ErrUnknownVersion:
4849
fmt.Fprintf(stderr, errMessageUnknownVersion)
49-
case dinosql.ErrNoPackages:
50+
case config.ErrNoPackages:
5051
fmt.Fprintf(stderr, errMessageNoPackages)
5152
}
5253
fmt.Fprintf(stderr, "error parsing sqlc.json: %s\n", err)
@@ -58,7 +59,7 @@ func Generate(dir string, stderr io.Writer) (map[string]string, error) {
5859

5960
for _, pkg := range settings.Packages {
6061
name := pkg.Name
61-
combo := dinosql.Combine(settings, pkg)
62+
combo := config.Combine(settings, pkg)
6263
var result dinosql.Generateable
6364

6465
// TODO: This feels like a hack that will bite us later
@@ -67,7 +68,7 @@ func Generate(dir string, stderr io.Writer) (map[string]string, error) {
6768

6869
switch pkg.Engine {
6970

70-
case dinosql.EngineMySQL:
71+
case config.EngineMySQL:
7172
// Experimental MySQL support
7273
q, err := mysql.GeneratePkg(name, pkg.Schema, pkg.Queries, combo)
7374
if err != nil {
@@ -84,7 +85,7 @@ func Generate(dir string, stderr io.Writer) (map[string]string, error) {
8485
}
8586
result = q
8687

87-
case dinosql.EnginePostgreSQL:
88+
case config.EnginePostgreSQL:
8889
c, err := dinosql.ParseCatalog(pkg.Schema)
8990
if err != nil {
9091
fmt.Fprintf(stderr, "# package %s\n", name)

internal/dinosql/config.go renamed to internal/config/config.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dinosql
1+
package config
22

33
import (
44
"encoding/json"
@@ -74,8 +74,8 @@ type Override struct {
7474
ColumnName string
7575
Table pg.FQN
7676
GoTypeName string
77-
goPackage string
78-
goBasicType bool
77+
GoPackage string
78+
GoBasicType bool
7979
}
8080

8181
func (c *GenerateSettings) ValidateGlobalOverrides() error {
@@ -154,7 +154,7 @@ func (o *Override) Parse() error {
154154
if !found {
155155
return fmt.Errorf("Package override `go_type` specifier %q is not a Go basic type e.g. 'string'", o.GoType)
156156
}
157-
o.goBasicType = true
157+
o.GoBasicType = true
158158
} else {
159159
// assume the type lives in a Go package
160160
if lastDot == -1 {
@@ -174,12 +174,12 @@ func (o *Override) Parse() error {
174174
if strings.HasSuffix(typename, "-go") {
175175
typename = typename[:len(typename)-len("-go")]
176176
}
177-
o.goPackage = o.GoType[:lastDot]
177+
o.GoPackage = o.GoType[:lastDot]
178178
}
179179
o.GoTypeName = typename
180180
isPointer := o.GoType[0] == '*'
181181
if isPointer {
182-
o.goPackage = o.goPackage[1:]
182+
o.GoPackage = o.GoPackage[1:]
183183
o.GoTypeName = "*" + o.GoTypeName
184184
}
185185

internal/dinosql/config_test.go renamed to internal/config/config_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dinosql
1+
package config
22

33
import (
44
"strings"
@@ -107,10 +107,10 @@ func TestTypeOverrides(t *testing.T) {
107107
if diff := cmp.Diff(tt.typeName, tt.override.GoTypeName); diff != "" {
108108
t.Errorf("type name mismatch;\n%s", diff)
109109
}
110-
if diff := cmp.Diff(tt.pkg, tt.override.goPackage); diff != "" {
110+
if diff := cmp.Diff(tt.pkg, tt.override.GoPackage); diff != "" {
111111
t.Errorf("package mismatch;\n%s", diff)
112112
}
113-
if diff := cmp.Diff(tt.basic, tt.override.goBasicType); diff != "" {
113+
if diff := cmp.Diff(tt.basic, tt.override.GoBasicType); diff != "" {
114114
t.Errorf("basic mismatch;\n%s", diff)
115115
}
116116
})

internal/dinosql/gen.go

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"unicode"
1414

1515
"github.com/kyleconroy/sqlc/internal/catalog"
16+
"github.com/kyleconroy/sqlc/internal/config"
1617
core "github.com/kyleconroy/sqlc/internal/pg"
1718

1819
"github.com/jinzhu/inflection"
@@ -159,12 +160,12 @@ type GoQuery struct {
159160
}
160161

161162
type Generateable interface {
162-
Structs(settings CombinedSettings) []GoStruct
163-
GoQueries(settings CombinedSettings) []GoQuery
164-
Enums(settings CombinedSettings) []GoEnum
163+
Structs(settings config.CombinedSettings) []GoStruct
164+
GoQueries(settings config.CombinedSettings) []GoQuery
165+
Enums(settings config.CombinedSettings) []GoEnum
165166
}
166167

167-
func UsesType(r Generateable, typ string, settings CombinedSettings) bool {
168+
func UsesType(r Generateable, typ string, settings config.CombinedSettings) bool {
168169
for _, strct := range r.Structs(settings) {
169170
for _, f := range strct.Fields {
170171
fType := strings.TrimPrefix(f.Type, "[]")
@@ -176,7 +177,7 @@ func UsesType(r Generateable, typ string, settings CombinedSettings) bool {
176177
return false
177178
}
178179

179-
func UsesArrays(r Generateable, settings CombinedSettings) bool {
180+
func UsesArrays(r Generateable, settings config.CombinedSettings) bool {
180181
for _, strct := range r.Structs(settings) {
181182
for _, f := range strct.Fields {
182183
if strings.HasPrefix(f.Type, "[]") {
@@ -187,7 +188,7 @@ func UsesArrays(r Generateable, settings CombinedSettings) bool {
187188
return false
188189
}
189190

190-
func Imports(r Generateable, settings CombinedSettings) func(string) [][]string {
191+
func Imports(r Generateable, settings config.CombinedSettings) func(string) [][]string {
191192
return func(filename string) [][]string {
192193
if filename == "db.go" {
193194
imps := []string{"context", "database/sql"}
@@ -209,7 +210,7 @@ func Imports(r Generateable, settings CombinedSettings) func(string) [][]string
209210
}
210211
}
211212

212-
func InterfaceImports(r Generateable, settings CombinedSettings) [][]string {
213+
func InterfaceImports(r Generateable, settings config.CombinedSettings) [][]string {
213214
gq := r.GoQueries(settings)
214215
uses := func(name string) bool {
215216
for _, q := range gq {
@@ -246,10 +247,10 @@ func InterfaceImports(r Generateable, settings CombinedSettings) [][]string {
246247
pkg := make(map[string]struct{})
247248
overrideTypes := map[string]string{}
248249
for _, o := range settings.Overrides {
249-
if o.goBasicType {
250+
if o.GoBasicType {
250251
continue
251252
}
252-
overrideTypes[o.GoTypeName] = o.goPackage
253+
overrideTypes[o.GoTypeName] = o.GoPackage
253254
}
254255

255256
_, overrideNullTime := overrideTypes["pq.NullTime"]
@@ -283,7 +284,7 @@ func InterfaceImports(r Generateable, settings CombinedSettings) [][]string {
283284
return [][]string{stds, pkgs}
284285
}
285286

286-
func ModelImports(r Generateable, settings CombinedSettings) [][]string {
287+
func ModelImports(r Generateable, settings config.CombinedSettings) [][]string {
287288
std := make(map[string]struct{})
288289
if UsesType(r, "sql.Null", settings) {
289290
std["database/sql"] = struct{}{}
@@ -302,10 +303,10 @@ func ModelImports(r Generateable, settings CombinedSettings) [][]string {
302303
pkg := make(map[string]struct{})
303304
overrideTypes := map[string]string{}
304305
for _, o := range settings.Overrides {
305-
if o.goBasicType {
306+
if o.GoBasicType {
306307
continue
307308
}
308-
overrideTypes[o.GoTypeName] = o.goPackage
309+
overrideTypes[o.GoTypeName] = o.GoPackage
309310
}
310311

311312
_, overrideNullTime := overrideTypes["pq.NullTime"]
@@ -339,7 +340,7 @@ func ModelImports(r Generateable, settings CombinedSettings) [][]string {
339340
return [][]string{stds, pkgs}
340341
}
341342

342-
func QueryImports(r Generateable, settings CombinedSettings, filename string) [][]string {
343+
func QueryImports(r Generateable, settings config.CombinedSettings, filename string) [][]string {
343344
// for _, strct := range r.Structs() {
344345
// for _, f := range strct.Fields {
345346
// if strings.HasPrefix(f.Type, "[]") {
@@ -437,10 +438,10 @@ func QueryImports(r Generateable, settings CombinedSettings, filename string) []
437438
pkg := make(map[string]struct{})
438439
overrideTypes := map[string]string{}
439440
for _, o := range settings.Overrides {
440-
if o.goBasicType {
441+
if o.GoBasicType {
441442
continue
442443
}
443-
overrideTypes[o.GoTypeName] = o.goPackage
444+
overrideTypes[o.GoTypeName] = o.GoPackage
444445
}
445446

446447
if sliceScan() {
@@ -489,7 +490,7 @@ func enumValueName(value string) string {
489490
return name
490491
}
491492

492-
func (r Result) Enums(settings CombinedSettings) []GoEnum {
493+
func (r Result) Enums(settings config.CombinedSettings) []GoEnum {
493494
var enums []GoEnum
494495
for name, schema := range r.Catalog.Schemas {
495496
if name == "pg_catalog" {
@@ -522,7 +523,7 @@ func (r Result) Enums(settings CombinedSettings) []GoEnum {
522523
return enums
523524
}
524525

525-
func StructName(name string, settings CombinedSettings) string {
526+
func StructName(name string, settings config.CombinedSettings) string {
526527
if rename := settings.Global.Rename[name]; rename != "" {
527528
return rename
528529
}
@@ -537,7 +538,7 @@ func StructName(name string, settings CombinedSettings) string {
537538
return out
538539
}
539540

540-
func (r Result) Structs(settings CombinedSettings) []GoStruct {
541+
func (r Result) Structs(settings config.CombinedSettings) []GoStruct {
541542
var structs []GoStruct
542543
for name, schema := range r.Catalog.Schemas {
543544
if name == "pg_catalog" {
@@ -572,7 +573,7 @@ func (r Result) Structs(settings CombinedSettings) []GoStruct {
572573
return structs
573574
}
574575

575-
func (r Result) goType(col core.Column, settings CombinedSettings) string {
576+
func (r Result) goType(col core.Column, settings config.CombinedSettings) string {
576577
// package overrides have a higher precedence
577578
for _, oride := range settings.Overrides {
578579
if oride.Column != "" && oride.ColumnName == col.Name && oride.Table == col.Table {
@@ -586,7 +587,7 @@ func (r Result) goType(col core.Column, settings CombinedSettings) string {
586587
return typ
587588
}
588589

589-
func (r Result) goInnerType(col core.Column, settings CombinedSettings) string {
590+
func (r Result) goInnerType(col core.Column, settings config.CombinedSettings) string {
590591
columnType := col.DataType
591592
notNull := col.NotNull || col.IsArray
592593

@@ -741,7 +742,7 @@ func (r Result) goInnerType(col core.Column, settings CombinedSettings) string {
741742
// JSON tags: count, count_2, count_2
742743
//
743744
// This is unlikely to happen, so don't fix it yet
744-
func (r Result) columnsToStruct(name string, columns []core.Column, settings CombinedSettings) *GoStruct {
745+
func (r Result) columnsToStruct(name string, columns []core.Column, settings config.CombinedSettings) *GoStruct {
745746
gs := GoStruct{
746747
Name: name,
747748
}
@@ -801,7 +802,7 @@ func compareFQN(a *core.FQN, b *core.FQN) bool {
801802
return a.Catalog == b.Catalog && a.Schema == b.Schema && a.Rel == b.Rel
802803
}
803804

804-
func (r Result) GoQueries(settings CombinedSettings) []GoQuery {
805+
func (r Result) GoQueries(settings config.CombinedSettings) []GoQuery {
805806
structs := r.Structs(settings)
806807

807808
qs := make([]GoQuery, 0, len(r.Queries))
@@ -1182,7 +1183,7 @@ type tmplCtx struct {
11821183
Enums []GoEnum
11831184
Structs []GoStruct
11841185
GoQueries []GoQuery
1185-
Settings GenerateSettings
1186+
Settings config.GenerateSettings
11861187

11871188
// TODO: Race conditions
11881189
SourceName string
@@ -1198,7 +1199,7 @@ func LowerTitle(s string) string {
11981199
return string(a)
11991200
}
12001201

1201-
func Generate(r Generateable, settings CombinedSettings) (map[string]string, error) {
1202+
func Generate(r Generateable, settings config.CombinedSettings) (map[string]string, error) {
12021203
funcMap := template.FuncMap{
12031204
"lowerTitle": LowerTitle,
12041205
"imports": Imports(r, settings),

internal/dinosql/parser.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"unicode"
1414

1515
"github.com/kyleconroy/sqlc/internal/catalog"
16+
"github.com/kyleconroy/sqlc/internal/config"
1617
core "github.com/kyleconroy/sqlc/internal/pg"
1718
"github.com/kyleconroy/sqlc/internal/postgres"
1819

@@ -188,7 +189,7 @@ type Result struct {
188189
Catalog core.Catalog
189190
}
190191

191-
func ParseQueries(c core.Catalog, pkg PackageSettings) (*Result, error) {
192+
func ParseQueries(c core.Catalog, pkg config.PackageSettings) (*Result, error) {
192193
f, err := os.Stat(pkg.Queries)
193194
if err != nil {
194195
return nil, fmt.Errorf("path %s does not exist", pkg.Queries)

internal/mysql/errors_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import (
44
"testing"
55

66
"github.com/google/go-cmp/cmp"
7-
"github.com/kyleconroy/sqlc/internal/dinosql"
87
"vitess.io/vitess/go/vt/sqlparser"
8+
9+
"github.com/kyleconroy/sqlc/internal/config"
910
)
1011

1112
func TestCustomArgErr(t *testing.T) {
@@ -38,7 +39,7 @@ func TestCustomArgErr(t *testing.T) {
3839
},
3940
},
4041
}
41-
settings := dinosql.Combine(dinosql.GenerateSettings{}, dinosql.PackageSettings{})
42+
settings := config.Combine(config.GenerateSettings{}, config.PackageSettings{})
4243
generator := PackageGenerator{mockSchema, settings, "db"}
4344
for _, tcase := range tests {
4445
q, err := generator.parseContents("queries.sql", tcase.input)
@@ -82,7 +83,7 @@ func TestPositionedErr(t *testing.T) {
8283
},
8384
}
8485

85-
settings := dinosql.Combine(dinosql.GenerateSettings{}, dinosql.PackageSettings{})
86+
settings := config.Combine(config.GenerateSettings{}, config.PackageSettings{})
8687
for _, tcase := range tests {
8788
generator := PackageGenerator{mockSchema, settings, "db"}
8889
q, err := generator.parseContents("queries.sql", tcase.input)

0 commit comments

Comments
 (0)