-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathgenerate.go
More file actions
413 lines (365 loc) · 10.9 KB
/
generate.go
File metadata and controls
413 lines (365 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package cmd
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"runtime/trace"
"strings"
"sync"
"google.golang.org/grpc"
"google.golang.org/grpc/status"
"github.com/sqlc-dev/sqlc/internal/codegen/golang"
genjson "github.com/sqlc-dev/sqlc/internal/codegen/json"
"github.com/sqlc-dev/sqlc/internal/compiler"
"github.com/sqlc-dev/sqlc/internal/config"
"github.com/sqlc-dev/sqlc/internal/config/convert"
"github.com/sqlc-dev/sqlc/internal/debug"
"github.com/sqlc-dev/sqlc/internal/ext"
"github.com/sqlc-dev/sqlc/internal/ext/process"
"github.com/sqlc-dev/sqlc/internal/ext/wasm"
"github.com/sqlc-dev/sqlc/internal/info"
"github.com/sqlc-dev/sqlc/internal/multierr"
"github.com/sqlc-dev/sqlc/internal/opts"
"github.com/sqlc-dev/sqlc/internal/plugin"
"github.com/sqlc-dev/sqlc/internal/remote"
"github.com/sqlc-dev/sqlc/internal/sql/sqlpath"
)
const errMessageNoVersion = `The configuration file must have a version number.
Set the version to 1 or 2 at the top of sqlc.json:
{
"version": "1"
...
}
`
const errMessageUnknownVersion = `The configuration file has an invalid version number.
The supported version can only be "1" or "2".
`
const errMessageNoPackages = `No packages are configured`
func printFileErr(stderr io.Writer, dir string, fileErr *multierr.FileError) {
filename, err := filepath.Rel(dir, fileErr.Filename)
if err != nil {
filename = fileErr.Filename
}
fmt.Fprintf(stderr, "%s:%d:%d: %s\n", filename, fileErr.Line, fileErr.Column, fileErr.Err)
}
func findPlugin(conf config.Config, name string) (*config.Plugin, error) {
for _, plug := range conf.Plugins {
if plug.Name == name {
return &plug, nil
}
}
return nil, fmt.Errorf("plugin not found")
}
func readConfig(stderr io.Writer, dir, filename string) (string, *config.Config, error) {
configPath := ""
if filename != "" {
configPath = filepath.Join(dir, filename)
} else {
var yamlMissing, jsonMissing, ymlMissing bool
yamlPath := filepath.Join(dir, "sqlc.yaml")
ymlPath := filepath.Join(dir, "sqlc.yml")
jsonPath := filepath.Join(dir, "sqlc.json")
if _, err := os.Stat(yamlPath); os.IsNotExist(err) {
yamlMissing = true
}
if _, err := os.Stat(jsonPath); os.IsNotExist(err) {
jsonMissing = true
}
if _, err := os.Stat(ymlPath); os.IsNotExist(err) {
ymlMissing = true
}
if yamlMissing && ymlMissing && jsonMissing {
fmt.Fprintln(stderr, "error parsing configuration files. sqlc.(yaml|yml) or sqlc.json: file does not exist")
return "", nil, errors.New("config file missing")
}
if (!yamlMissing || !ymlMissing) && !jsonMissing {
fmt.Fprintln(stderr, "error: both sqlc.json and sqlc.(yaml|yml) files present")
return "", nil, errors.New("sqlc.json and sqlc.(yaml|yml) present")
}
if jsonMissing {
if yamlMissing {
configPath = ymlPath
} else {
configPath = yamlPath
}
} else {
configPath = jsonPath
}
}
base := filepath.Base(configPath)
file, err := os.Open(configPath)
if err != nil {
fmt.Fprintf(stderr, "error parsing %s: file does not exist\n", base)
return "", nil, err
}
defer file.Close()
conf, err := config.ParseConfig(file)
if err != nil {
switch err {
case config.ErrMissingVersion:
fmt.Fprint(stderr, errMessageNoVersion)
case config.ErrUnknownVersion:
fmt.Fprint(stderr, errMessageUnknownVersion)
case config.ErrNoPackages:
fmt.Fprint(stderr, errMessageNoPackages)
}
fmt.Fprintf(stderr, "error parsing %s: %s\n", base, err)
return "", nil, err
}
return configPath, &conf, nil
}
func Generate(ctx context.Context, dir, filename string, o *Options) (map[string]string, error) {
e := o.Env
stderr := o.Stderr
configPath, conf, err := o.ReadConfig(dir, filename)
if err != nil {
return nil, err
}
base := filepath.Base(configPath)
if err := config.Validate(conf); err != nil {
fmt.Fprintf(stderr, "error validating %s: %s\n", base, err)
return nil, err
}
if err := e.Validate(conf); err != nil {
fmt.Fprintf(stderr, "error validating %s: %s\n", base, err)
return nil, err
}
// Comment on why these two methods exist
if conf.Cloud.Project != "" && e.Remote && !e.NoRemote {
return remoteGenerate(ctx, configPath, conf, dir, stderr)
}
g := &generator{
dir: dir,
output: map[string]string{},
}
if err := processQuerySets(ctx, g, conf, dir, o); err != nil {
return nil, err
}
return g.output, nil
}
type generator struct {
m sync.Mutex
dir string
output map[string]string
}
func (g *generator) Pairs(ctx context.Context, conf *config.Config) []OutputPair {
var pairs []OutputPair
for _, sql := range conf.SQL {
if sql.Gen.Go != nil {
pairs = append(pairs, OutputPair{
SQL: sql,
Gen: config.SQLGen{Go: sql.Gen.Go},
})
}
if sql.Gen.JSON != nil {
pairs = append(pairs, OutputPair{
SQL: sql,
Gen: config.SQLGen{JSON: sql.Gen.JSON},
})
}
for i := range sql.Codegen {
pairs = append(pairs, OutputPair{
SQL: sql,
Plugin: &sql.Codegen[i],
})
}
}
return pairs
}
func (g *generator) ProcessResult(ctx context.Context, combo config.CombinedSettings, sql OutputPair, result *compiler.Result) error {
out, resp, err := codegen(ctx, combo, sql, result)
if err != nil {
return err
}
files := map[string]string{}
for _, file := range resp.Files {
files[file.Name] = string(file.Contents)
}
g.m.Lock()
// out is specified by the user, not a plugin
absout := filepath.Join(g.dir, out)
for n, source := range files {
filename := filepath.Join(g.dir, out, n)
// filepath.Join calls filepath.Clean which should remove all "..", but
// double check to make sure
if strings.Contains(filename, "..") {
return fmt.Errorf("invalid file output path: %s", filename)
}
// The output file must be contained inside the output directory
if !strings.HasPrefix(filename, absout) {
return fmt.Errorf("invalid file output path: %s", filename)
}
g.output[filename] = source
}
g.m.Unlock()
return nil
}
func remoteGenerate(ctx context.Context, configPath string, conf *config.Config, dir string, stderr io.Writer) (map[string]string, error) {
rpcClient, err := remote.NewClient(conf.Cloud)
if err != nil {
fmt.Fprintf(stderr, "error creating rpc client: %s\n", err)
return nil, err
}
configBytes, err := os.ReadFile(configPath)
if err != nil {
fmt.Fprintf(stderr, "error reading config file %s: %s\n", configPath, err)
return nil, err
}
rpcReq := remote.GenerateRequest{
Version: info.Version,
Inputs: []*remote.File{{Path: filepath.Base(configPath), Bytes: configBytes}},
}
for _, pkg := range conf.SQL {
for _, paths := range []config.Paths{pkg.Schema, pkg.Queries} {
for i, relFilePath := range paths {
paths[i] = filepath.Join(dir, relFilePath)
}
files, err := sqlpath.Glob(paths)
if err != nil {
fmt.Fprintf(stderr, "error globbing paths: %s\n", err)
return nil, err
}
for _, filePath := range files {
fileBytes, err := os.ReadFile(filePath)
if err != nil {
fmt.Fprintf(stderr, "error reading file %s: %s\n", filePath, err)
return nil, err
}
fileRelPath, _ := filepath.Rel(dir, filePath)
rpcReq.Inputs = append(rpcReq.Inputs, &remote.File{Path: fileRelPath, Bytes: fileBytes})
}
}
}
rpcResp, err := rpcClient.Generate(ctx, &rpcReq)
if err != nil {
rpcStatus, ok := status.FromError(err)
if !ok {
return nil, err
}
fmt.Fprintf(stderr, "rpc error: %s", rpcStatus.Message())
return nil, rpcStatus.Err()
}
if rpcResp.ExitCode != 0 {
fmt.Fprintf(stderr, "%s", rpcResp.Stderr)
return nil, errors.New("remote execution returned with non-zero exit code")
}
output := map[string]string{}
for _, file := range rpcResp.Outputs {
output[filepath.Join(dir, file.Path)] = string(file.Bytes)
}
return output, nil
}
func parse(ctx context.Context, name, dir string, sql config.SQL, combo config.CombinedSettings, parserOpts opts.Parser, stderr io.Writer) (*compiler.Result, bool) {
defer trace.StartRegion(ctx, "parse").End()
c, err := compiler.NewCompiler(sql, combo, parserOpts)
defer func() {
if c != nil {
c.Close(ctx)
}
}()
if err != nil {
fmt.Fprintf(stderr, "error creating compiler: %s\n", err)
return nil, true
}
if err := c.ParseCatalog(sql.Schema); err != nil {
fmt.Fprintf(stderr, "# package %s\n", name)
if parserErr, ok := err.(*multierr.Error); ok {
for _, fileErr := range parserErr.Errs() {
printFileErr(stderr, dir, fileErr)
}
} else {
fmt.Fprintf(stderr, "error parsing schema: %s\n", err)
}
return nil, true
}
if parserOpts.Debug.DumpCatalog {
debug.Dump(c.Catalog())
}
if err := c.ParseQueries(sql.Queries, parserOpts); err != nil {
fmt.Fprintf(stderr, "# package %s\n", name)
if parserErr, ok := err.(*multierr.Error); ok {
for _, fileErr := range parserErr.Errs() {
printFileErr(stderr, dir, fileErr)
}
} else {
fmt.Fprintf(stderr, "error parsing queries: %s\n", err)
}
return nil, true
}
return c.Result(), false
}
func codegen(ctx context.Context, combo config.CombinedSettings, sql OutputPair, result *compiler.Result) (string, *plugin.GenerateResponse, error) {
defer trace.StartRegion(ctx, "codegen").End()
req := codeGenRequest(result, combo)
var handler grpc.ClientConnInterface
var out string
switch {
case sql.Plugin != nil:
out = sql.Plugin.Out
plug, err := findPlugin(combo.Global, sql.Plugin.Plugin)
if err != nil {
return "", nil, fmt.Errorf("plugin not found: %s", err)
}
switch {
case plug.Process != nil:
handler = &process.Runner{
Cmd: plug.Process.Cmd,
Env: plug.Env,
Format: plug.Process.Format,
}
case plug.WASM != nil:
handler = &wasm.Runner{
URL: plug.WASM.URL,
SHA256: plug.WASM.SHA256,
Env: plug.Env,
}
default:
return "", nil, fmt.Errorf("unsupported plugin type")
}
opts, err := convert.YAMLtoJSON(sql.Plugin.Options)
if err != nil {
return "", nil, fmt.Errorf("invalid plugin options: %w", err)
}
req.PluginOptions = opts
global, found := combo.Global.Options[plug.Name]
if found {
opts, err := convert.YAMLtoJSON(global)
if err != nil {
return "", nil, fmt.Errorf("invalid global options: %w", err)
}
req.GlobalOptions = opts
}
case sql.Gen.Go != nil:
out = combo.Go.Out
handler = ext.HandleFunc(golang.Generate)
opts, err := json.Marshal(sql.Gen.Go)
if err != nil {
return "", nil, fmt.Errorf("opts marshal failed: %w", err)
}
req.PluginOptions = opts
if combo.Global.Overrides.Go != nil {
opts, err := json.Marshal(combo.Global.Overrides.Go)
if err != nil {
return "", nil, fmt.Errorf("opts marshal failed: %w", err)
}
req.GlobalOptions = opts
}
case sql.Gen.JSON != nil:
out = combo.JSON.Out
handler = ext.HandleFunc(genjson.Generate)
opts, err := json.Marshal(sql.Gen.JSON)
if err != nil {
return "", nil, fmt.Errorf("opts marshal failed: %w", err)
}
req.PluginOptions = opts
default:
return "", nil, fmt.Errorf("missing language backend")
}
client := plugin.NewCodegenServiceClient(handler)
resp, err := client.Generate(ctx, req)
return out, resp, err
}