Skip to content

Commit 1830fe0

Browse files
committed
Add support for json format from process plugins
1 parent 9cf5dbc commit 1830fe0

File tree

12 files changed

+151
-11
lines changed

12 files changed

+151
-11
lines changed

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+
);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"version": "2",
3+
"sql": [
4+
{
5+
"schema": "schema.sql",
6+
"queries": "query.sql",
7+
"engine": "postgresql",
8+
"codegen": [
9+
{
10+
"out": "gen",
11+
"plugin": "jsonb"
12+
}
13+
]
14+
}
15+
],
16+
"plugins": [
17+
{
18+
"name": "jsonb",
19+
"process": {
20+
"cmd": "test_json_process_plugin",
21+
"format": "json"
22+
}
23+
}
24+
]
25+
}

0 commit comments

Comments
 (0)