@@ -20,8 +20,10 @@ import (
2020 "github.com/kyleconroy/sqlc/internal/debug"
2121 "github.com/kyleconroy/sqlc/internal/ext"
2222 "github.com/kyleconroy/sqlc/internal/ext/process"
23+ "github.com/kyleconroy/sqlc/internal/ext/wasm"
2324 "github.com/kyleconroy/sqlc/internal/multierr"
2425 "github.com/kyleconroy/sqlc/internal/opts"
26+ "github.com/kyleconroy/sqlc/internal/plugin"
2527)
2628
2729const errMessageNoVersion = `The configuration file must have a version number.
@@ -51,6 +53,15 @@ type outPair struct {
5153 config.SQL
5254}
5355
56+ func findPlugin (conf config.Config , name string ) (* config.Plugin , error ) {
57+ for _ , plug := range conf .Plugins {
58+ if plug .Name == name {
59+ return & plug , nil
60+ }
61+ }
62+ return nil , fmt .Errorf ("plugin not found" )
63+ }
64+
5465func readConfig (stderr io.Writer , dir , filename string ) (string , * config.Config , error ) {
5566 configPath := ""
5667 if filename != "" {
@@ -216,43 +227,7 @@ func Generate(ctx context.Context, e Env, dir, filename string, stderr io.Writer
216227 break
217228 }
218229
219- var region * trace.Region
220- if debug .Traced {
221- region = trace .StartRegion (ctx , "codegen" )
222- }
223- var handler ext.Handler
224- var out string
225- switch {
226- case sql .Gen .Go != nil :
227- out = combo .Go .Out
228- handler = ext .HandleFunc (golang .Generate )
229-
230- case sql .Gen .Kotlin != nil :
231- out = combo .Kotlin .Out
232- handler = ext .HandleFunc (kotlin .Generate )
233-
234- case sql .Gen .Python != nil :
235- out = combo .Python .Out
236- handler = ext .HandleFunc (python .Generate )
237-
238- case sql .Gen .JSON != nil :
239- out = combo .JSON .Out
240- handler = ext .HandleFunc (json .Generate )
241-
242- case sql .Plugin != nil :
243- out = sql .Plugin .Out
244- handler = & process.Runner {
245- Config : combo .Global ,
246- Plugin : sql .Plugin .Plugin ,
247- }
248-
249- default :
250- panic ("missing language backend" )
251- }
252- resp , err := handler .Generate (codeGenRequest (result , combo ))
253- if region != nil {
254- region .End ()
255- }
230+ out , resp , err := codegen (ctx , combo , sql , result )
256231 if err != nil {
257232 fmt .Fprintf (stderr , "# package %s\n " , name )
258233 fmt .Fprintf (stderr , "error generating code: %s\n " , err )
@@ -262,6 +237,7 @@ func Generate(ctx context.Context, e Env, dir, filename string, stderr io.Writer
262237 }
263238 continue
264239 }
240+
265241 files := map [string ]string {}
266242 for _ , file := range resp .Files {
267243 files [file .Name ] = string (file .Contents )
@@ -313,3 +289,58 @@ func parse(ctx context.Context, e Env, name, dir string, sql config.SQL, combo c
313289 }
314290 return c .Result (), false
315291}
292+
293+ func codegen (ctx context.Context , combo config.CombinedSettings , sql outPair , result * compiler.Result ) (string , * plugin.CodeGenResponse , error ) {
294+ var region * trace.Region
295+ if debug .Traced {
296+ region = trace .StartRegion (ctx , "codegen" )
297+ }
298+ var handler ext.Handler
299+ var out string
300+ switch {
301+ case sql .Gen .Go != nil :
302+ out = combo .Go .Out
303+ handler = ext .HandleFunc (golang .Generate )
304+
305+ case sql .Gen .Kotlin != nil :
306+ out = combo .Kotlin .Out
307+ handler = ext .HandleFunc (kotlin .Generate )
308+
309+ case sql .Gen .Python != nil :
310+ out = combo .Python .Out
311+ handler = ext .HandleFunc (python .Generate )
312+
313+ case sql .Gen .JSON != nil :
314+ out = combo .JSON .Out
315+ handler = ext .HandleFunc (json .Generate )
316+
317+ case sql .Plugin != nil :
318+ out = sql .Plugin .Out
319+ plug , err := findPlugin (combo .Global , sql .Plugin .Plugin )
320+ if err != nil {
321+ return "" , nil , fmt .Errorf ("plugin not found: %s" , err )
322+ }
323+
324+ switch {
325+ case plug .Process != nil :
326+ handler = & process.Runner {
327+ Cmd : plug .Process .Cmd ,
328+ }
329+ case plug .WASM != nil :
330+ handler = & wasm.Runner {
331+ URL : plug .WASM .URL ,
332+ Checksum : plug .WASM .Checksum ,
333+ }
334+ default :
335+ return "" , nil , fmt .Errorf ("unsupported plugin type" )
336+ }
337+
338+ default :
339+ return "" , nil , fmt .Errorf ("missing language backend" )
340+ }
341+ resp , err := handler .Generate (ctx , codeGenRequest (result , combo ))
342+ if region != nil {
343+ region .End ()
344+ }
345+ return out , resp , err
346+ }
0 commit comments