Skip to content

Commit 67800a9

Browse files
authored
codegen: Add the new codegen packages (#513)
1 parent d33571f commit 67800a9

File tree

13 files changed

+1106
-1077
lines changed

13 files changed

+1106
-1077
lines changed

internal/cmd/generate.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"path/filepath"
1111
"strings"
1212

13+
"github.com/kyleconroy/sqlc/internal/codegen/golang"
1314
"github.com/kyleconroy/sqlc/internal/compiler"
1415
"github.com/kyleconroy/sqlc/internal/config"
1516
"github.com/kyleconroy/sqlc/internal/dinosql"
@@ -112,7 +113,7 @@ func Generate(e Env, dir string, stderr io.Writer) (map[string]string, error) {
112113

113114
for _, sql := range pairs {
114115
combo := config.Combine(conf, sql.SQL)
115-
var result dinosql.Generateable
116+
var result golang.Generateable
116117

117118
// TODO: This feels like a hack that will bite us later
118119
joined := make([]string, 0, len(sql.Schema))
@@ -145,7 +146,7 @@ func Generate(e Env, dir string, stderr io.Writer) (map[string]string, error) {
145146
var out string
146147
if sql.Gen.Go != nil {
147148
out = combo.Go.Out
148-
files, err = dinosql.Generate(result, combo)
149+
files, err = golang.Generate(result, combo)
149150
} else if sql.Gen.Kotlin != nil {
150151
out = combo.Kotlin.Out
151152
ktRes, ok := result.(kotlin.KtGenerateable)
@@ -177,7 +178,7 @@ func Generate(e Env, dir string, stderr io.Writer) (map[string]string, error) {
177178
type postgreEngine interface {
178179
ParseCatalog([]string) error
179180
ParseQueries([]string, dinosql.ParserOpts) error
180-
Result() dinosql.Generateable
181+
Result() golang.Generateable
181182
}
182183

183184
type dinosqlEngine struct {
@@ -203,11 +204,11 @@ func (d *dinosqlEngine) ParseQueries(queries []string, opts dinosql.ParserOpts)
203204
return nil
204205
}
205206

206-
func (d *dinosqlEngine) Result() dinosql.Generateable {
207+
func (d *dinosqlEngine) Result() golang.Generateable {
207208
return &kotlin.Result{Result: d.result}
208209
}
209210

210-
func parse(e Env, name, dir string, sql config.SQL, combo config.CombinedSettings, parserOpts dinosql.ParserOpts, stderr io.Writer) (dinosql.Generateable, bool) {
211+
func parse(e Env, name, dir string, sql config.SQL, combo config.CombinedSettings, parserOpts dinosql.ParserOpts, stderr io.Writer) (golang.Generateable, bool) {
211212
switch sql.Engine {
212213
case config.EngineMySQL:
213214
// Experimental MySQL support

internal/codegen/golang/enum.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package golang
2+
3+
import "regexp"
4+
5+
var IdentPattern = regexp.MustCompile("[^a-zA-Z0-9_]+")
6+
7+
type Constant struct {
8+
Name string
9+
Type string
10+
Value string
11+
}
12+
13+
type Enum struct {
14+
Name string
15+
Comment string
16+
Constants []Constant
17+
}

internal/codegen/golang/field.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package golang
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
"strings"
7+
)
8+
9+
type Field struct {
10+
Name string
11+
Type string
12+
Tags map[string]string
13+
Comment string
14+
}
15+
16+
func (gf Field) Tag() string {
17+
tags := make([]string, 0, len(gf.Tags))
18+
for key, val := range gf.Tags {
19+
tags = append(tags, fmt.Sprintf("%s\"%s\"", key, val))
20+
}
21+
if len(tags) == 0 {
22+
return ""
23+
}
24+
sort.Strings(tags)
25+
return strings.Join(tags, ",")
26+
}

0 commit comments

Comments
 (0)