Skip to content

Commit 223fd03

Browse files
rouzierkyleconroy
andauthored
Add support for json format from process plugins (#3827)
* Add support for json format from process plugins * Update gen.go * Fix test --------- Co-authored-by: Kyle Gray <[email protected]>
1 parent c576a07 commit 223fd03

File tree

13 files changed

+154
-11
lines changed

13 files changed

+154
-11
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ jobs:
3838
- name: install sqlc-gen-test
3939
run: go install github.com/sqlc-dev/[email protected]
4040

41+
- name: install test-json-process-plugin
42+
run: go install ./scripts/test-json-process-plugin/
43+
4144
- name: install ./...
4245
run: go install ./...
4346
env:

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ sqlc-pg-gen:
3232
sqlc-gen-json:
3333
go build -o ~/bin/sqlc-gen-json ./cmd/sqlc-gen-json
3434

35+
test-json-process-plugin:
36+
go build -o ~/bin/test-json-process-plugin ./scripts/test-json-process-plugin/
37+
3538
start:
3639
docker compose up -d
3740

docs/guides/plugins.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ For a complete working example see the following files:
7272
- A process-based plugin that serializes the CodeGenRequest to JSON
7373
- [process_plugin_sqlc_gen_json](https://github.com/sqlc-dev/sqlc/tree/main/internal/endtoend/testdata/process_plugin_sqlc_gen_json)
7474
- An example project showing how to use a process-based plugin
75+
- [process_plugin_sqlc_gen_json](https://github.com/sqlc-dev/sqlc/tree/main/internal/endtoend/testdata/process_plugin_format_json/)
76+
- An example project showing how to use a process-based plugin using json
7577

7678
## Environment variables
7779

@@ -99,4 +101,4 @@ plugins:
99101
```
100102

101103
A variable named `SQLC_VERSION` is always included in the plugin's
102-
environment, set to the version of the `sqlc` executable invoking it.
104+
environment, set to the version of the `sqlc` executable invoking it.

docs/reference/config.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ Each mapping in the `plugins` collection has the following keys:
273273
- `process`: A mapping with a single `cmd` key
274274
- `cmd`:
275275
- The executable to call when using this plugin
276+
- `format`:
277+
- The format expected. Supports `json` and `protobuf` formats. Defaults to `protobuf`.
276278
- `wasm`: A mapping with a two keys `url` and `sha256`
277279
- `url`:
278280
- The URL to fetch the WASM file. Supports the `https://` or `file://` schemes.

internal/cmd/generate.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,9 @@ func codegen(ctx context.Context, combo config.CombinedSettings, sql OutputPair,
349349
switch {
350350
case plug.Process != nil:
351351
handler = &process.Runner{
352-
Cmd: plug.Process.Cmd,
353-
Env: plug.Env,
352+
Cmd: plug.Process.Cmd,
353+
Env: plug.Env,
354+
Format: plug.Process.Format,
354355
}
355356
case plug.WASM != nil:
356357
handler = &wasm.Runner{

internal/config/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ type Plugin struct {
8989
Name string `json:"name" yaml:"name"`
9090
Env []string `json:"env" yaml:"env"`
9191
Process *struct {
92-
Cmd string `json:"cmd" yaml:"cmd"`
92+
Cmd string `json:"cmd" yaml:"cmd"`
93+
Format string `json:"format" yaml:"format"`
9394
} `json:"process" yaml:"process"`
9495
WASM *struct {
9596
URL string `json:"url" yaml:"url"`
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"process": "test-json-process-plugin",
3+
"os": [ "darwin", "linux" ]
4+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
SELECT id, name, bio FROM authors
2+
WHERE id = $1 LIMIT 1
3+
SELECT id, name, bio FROM authors
4+
ORDER BY name
5+
INSERT INTO authors (
6+
name, bio
7+
) VALUES (
8+
$1, $2
9+
)
10+
RETURNING id, name, bio
11+
DELETE FROM authors
12+
WHERE id = $1
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- name: GetAuthor :one
2+
SELECT * FROM authors
3+
WHERE id = $1 LIMIT 1;
4+
5+
-- name: ListAuthors :many
6+
SELECT * FROM authors
7+
ORDER BY name;
8+
9+
-- name: CreateAuthor :one
10+
INSERT INTO authors (
11+
name, bio
12+
) VALUES (
13+
$1, $2
14+
)
15+
RETURNING *;
16+
17+
-- name: DeleteAuthor :exec
18+
DELETE FROM authors
19+
WHERE id = $1;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE authors (
2+
id BIGSERIAL PRIMARY KEY,
3+
name text NOT NULL,
4+
bio text
5+
);

0 commit comments

Comments
 (0)