Skip to content

Commit b902fab

Browse files
committed
Create a new ext package
1 parent 9092134 commit b902fab

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

internal/cmd/generate.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ import (
1414
"github.com/kyleconroy/sqlc/internal/codegen/golang"
1515
"github.com/kyleconroy/sqlc/internal/codegen/json"
1616
"github.com/kyleconroy/sqlc/internal/codegen/kotlin"
17-
"github.com/kyleconroy/sqlc/internal/codegen/process"
1817
"github.com/kyleconroy/sqlc/internal/codegen/python"
1918
"github.com/kyleconroy/sqlc/internal/compiler"
2019
"github.com/kyleconroy/sqlc/internal/config"
2120
"github.com/kyleconroy/sqlc/internal/debug"
21+
"github.com/kyleconroy/sqlc/internal/ext"
22+
"github.com/kyleconroy/sqlc/internal/ext/process"
2223
"github.com/kyleconroy/sqlc/internal/multierr"
2324
"github.com/kyleconroy/sqlc/internal/opts"
24-
"github.com/kyleconroy/sqlc/internal/plugin"
2525
)
2626

2727
const errMessageNoVersion = `The configuration file must have a version number.
@@ -220,37 +220,36 @@ func Generate(ctx context.Context, e Env, dir, filename string, stderr io.Writer
220220
if debug.Traced {
221221
region = trace.StartRegion(ctx, "codegen")
222222
}
223-
var genfunc func(req *plugin.CodeGenRequest) (*plugin.CodeGenResponse, error)
223+
var handler ext.Handler
224224
var out string
225225
switch {
226226
case sql.Gen.Go != nil:
227227
out = combo.Go.Out
228-
genfunc = golang.Generate
228+
handler = ext.HandleFunc(golang.Generate)
229229

230230
case sql.Gen.Kotlin != nil:
231231
out = combo.Kotlin.Out
232-
genfunc = kotlin.Generate
232+
handler = ext.HandleFunc(kotlin.Generate)
233233

234234
case sql.Gen.Python != nil:
235235
out = combo.Python.Out
236-
genfunc = python.Generate
236+
handler = ext.HandleFunc(python.Generate)
237237

238238
case sql.Gen.JSON != nil:
239239
out = combo.JSON.Out
240-
genfunc = json.Generate
240+
handler = ext.HandleFunc(json.Generate)
241241

242242
case sql.Plugin != nil:
243243
out = sql.Plugin.Out
244-
runner := process.Runner{
244+
handler = &process.Runner{
245245
Config: combo.Global,
246246
Plugin: sql.Plugin.Plugin,
247247
}
248-
genfunc = runner.Generate
249248

250249
default:
251250
panic("missing language backend")
252251
}
253-
resp, err := genfunc(codeGenRequest(result, combo))
252+
resp, err := handler.Generate(codeGenRequest(result, combo))
254253
if region != nil {
255254
region.End()
256255
}

internal/ext/handler.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package ext
2+
3+
import (
4+
"github.com/kyleconroy/sqlc/internal/plugin"
5+
)
6+
7+
type Handler interface {
8+
Generate(*plugin.CodeGenRequest) (*plugin.CodeGenResponse, error)
9+
}
10+
11+
type wrapper struct {
12+
fn func(*plugin.CodeGenRequest) (*plugin.CodeGenResponse, error)
13+
}
14+
15+
func (w *wrapper) Generate(req *plugin.CodeGenRequest) (*plugin.CodeGenResponse, error) {
16+
return w.fn(req)
17+
}
18+
19+
func HandleFunc(fn func(*plugin.CodeGenRequest) (*plugin.CodeGenResponse, error)) Handler {
20+
return &wrapper{fn}
21+
}
File renamed without changes.

0 commit comments

Comments
 (0)