-
Notifications
You must be signed in to change notification settings - Fork 835
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
1 parent
c576a07
commit 223fd03
Showing
13 changed files
with
154 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,9 @@ jobs: | |
- name: install sqlc-gen-test | ||
run: go install github.com/sqlc-dev/[email protected] | ||
|
||
- name: install test-json-process-plugin | ||
run: go install ./scripts/test-json-process-plugin/ | ||
|
||
- name: install ./... | ||
run: go install ./... | ||
env: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
internal/endtoend/testdata/process_plugin_format_json/exec.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"process": "test-json-process-plugin", | ||
"os": [ "darwin", "linux" ] | ||
} |
12 changes: 12 additions & 0 deletions
12
internal/endtoend/testdata/process_plugin_format_json/gen/hello.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
SELECT id, name, bio FROM authors | ||
WHERE id = $1 LIMIT 1 | ||
SELECT id, name, bio FROM authors | ||
ORDER BY name | ||
INSERT INTO authors ( | ||
name, bio | ||
) VALUES ( | ||
$1, $2 | ||
) | ||
RETURNING id, name, bio | ||
DELETE FROM authors | ||
WHERE id = $1 |
19 changes: 19 additions & 0 deletions
19
internal/endtoend/testdata/process_plugin_format_json/query.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
-- name: GetAuthor :one | ||
SELECT * FROM authors | ||
WHERE id = $1 LIMIT 1; | ||
|
||
-- name: ListAuthors :many | ||
SELECT * FROM authors | ||
ORDER BY name; | ||
|
||
-- name: CreateAuthor :one | ||
INSERT INTO authors ( | ||
name, bio | ||
) VALUES ( | ||
$1, $2 | ||
) | ||
RETURNING *; | ||
|
||
-- name: DeleteAuthor :exec | ||
DELETE FROM authors | ||
WHERE id = $1; |
5 changes: 5 additions & 0 deletions
5
internal/endtoend/testdata/process_plugin_format_json/schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CREATE TABLE authors ( | ||
id BIGSERIAL PRIMARY KEY, | ||
name text NOT NULL, | ||
bio text | ||
); |
25 changes: 25 additions & 0 deletions
25
internal/endtoend/testdata/process_plugin_format_json/sqlc.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"version": "2", | ||
"sql": [ | ||
{ | ||
"schema": "schema.sql", | ||
"queries": "query.sql", | ||
"engine": "postgresql", | ||
"codegen": [ | ||
{ | ||
"out": "gen", | ||
"plugin": "jsonb" | ||
} | ||
] | ||
} | ||
], | ||
"plugins": [ | ||
{ | ||
"name": "jsonb", | ||
"process": { | ||
"cmd": "test-json-process-plugin", | ||
"format": "json" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
type Out struct { | ||
Files []File `json:"files"` | ||
} | ||
|
||
type File struct { | ||
Name string `json:"name"` | ||
Contents []byte `json:"contents"` | ||
} | ||
|
||
func main() { | ||
in := make(map[string]interface{}) | ||
decoder := json.NewDecoder(os.Stdin) | ||
err := decoder.Decode(&in) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "error generating JSON: %s", err) | ||
os.Exit(2) | ||
} | ||
|
||
buf := bytes.NewBuffer(nil) | ||
queries := in["queries"].([]interface{}) | ||
for _, q := range queries { | ||
text := q.(map[string]interface{})["text"].(string) | ||
buf.WriteString(text) | ||
buf.WriteString("\n") | ||
} | ||
|
||
e := json.NewEncoder(os.Stdout) | ||
e.SetIndent("", " ") | ||
e.Encode(&Out{Files: []File{{Name: "hello.txt", Contents: buf.Bytes()}}}) | ||
} |