Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add JSON "codegen" output #1565

Merged
merged 4 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/kyleconroy/sqlc/internal/codegen/golang"
"github.com/kyleconroy/sqlc/internal/codegen/json"
"github.com/kyleconroy/sqlc/internal/codegen/kotlin"
"github.com/kyleconroy/sqlc/internal/codegen/python"
"github.com/kyleconroy/sqlc/internal/compiler"
Expand Down Expand Up @@ -138,6 +139,12 @@ func Generate(ctx context.Context, e Env, dir, filename string, stderr io.Writer
Gen: config.SQLGen{Python: sql.Gen.Python},
})
}
if sql.Gen.JSON != nil {
pairs = append(pairs, outPair{
SQL: sql,
Gen: config.SQLGen{JSON: sql.Gen.JSON},
})
}
}

for _, sql := range pairs {
Expand Down Expand Up @@ -205,6 +212,9 @@ func Generate(ctx context.Context, e Env, dir, filename string, stderr io.Writer
case sql.Gen.Python != nil:
out = combo.Python.Out
genfunc = python.Generate
case sql.Gen.JSON != nil:
out = combo.JSON.Out
genfunc = json.Generate
default:
panic("missing language backend")
}
Expand Down
8 changes: 8 additions & 0 deletions internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func pluginSettings(cs config.CombinedSettings) *plugin.Settings {
Python: pluginPythonCode(cs.Python),
Kotlin: pluginKotlinCode(cs.Kotlin),
Go: pluginGoCode(cs.Go),
Json: pluginJSONCode(cs.JSON),
}
}

Expand Down Expand Up @@ -124,6 +125,13 @@ func pluginKotlinCode(s config.SQLKotlin) *plugin.KotlinCode {
}
}

func pluginJSONCode(s config.SQLJSON) *plugin.JSONCode {
return &plugin.JSONCode{
Out: s.Out,
Indent: s.Indent,
}
}

func pluginCatalog(c *catalog.Catalog) *plugin.Catalog {
var schemas []*plugin.Schema
for _, s := range c.Schemas {
Expand Down
40 changes: 40 additions & 0 deletions internal/codegen/json/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package json

import (
ejson "encoding/json"

"google.golang.org/protobuf/encoding/protojson"

"github.com/kyleconroy/sqlc/internal/plugin"
)

func Generate(req *plugin.CodeGenRequest) (*plugin.CodeGenResponse, error) {
indent := ""
if req.Settings != nil && req.Settings.Json != nil {
indent = req.Settings.Json.Indent
}
// The output of protojson has randomized whitespace
// https://github.com/golang/protobuf/issues/1082
m := &protojson.MarshalOptions{
EmitUnpopulated: true,
Indent: "",
UseProtoNames: true,
}
data, err := m.Marshal(req)
if err != nil {
return nil, err
}
var rm ejson.RawMessage = data
blob, err := ejson.MarshalIndent(rm, "", indent)
if err != nil {
return nil, err
}
return &plugin.CodeGenResponse{
Files: []*plugin.File{
{
Name: "codegen_request.json",
Contents: append(blob, '\n'),
},
},
}, nil
}
10 changes: 10 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ type SQLGen struct {
Go *SQLGo `json:"go,omitempty" yaml:"go"`
Kotlin *SQLKotlin `json:"kotlin,omitempty" yaml:"kotlin"`
Python *SQLPython `json:"python,omitempty" yaml:"python"`
JSON *SQLJSON `json:"json,omitempty" yaml:"json"`
}

type SQLGo struct {
Expand Down Expand Up @@ -155,6 +156,11 @@ type SQLPython struct {
EmitPydanticModels bool `json:"emit_pydantic_models,omitempty" yaml:"emit_pydantic_models"`
}

type SQLJSON struct {
Out string `json:"out" yaml:"out"`
Indent string `json:"indent,omitempty" yaml:"indent"`
}

type Override struct {
// name of the golang type to use, e.g. `github.com/segmentio/ksuid.KSUID`
GoType GoType `json:"go_type" yaml:"go_type"`
Expand Down Expand Up @@ -352,6 +358,7 @@ type CombinedSettings struct {
Go SQLGo
Kotlin SQLKotlin
Python SQLPython
JSON SQLJSON
Rename map[string]string
Overrides []Override
}
Expand Down Expand Up @@ -379,5 +386,8 @@ func Combine(conf Config, pkg SQL) CombinedSettings {
cs.Python = *pkg.Gen.Python
cs.Overrides = append(cs.Overrides, pkg.Gen.Python.Overrides...)
}
if pkg.Gen.JSON != nil {
cs.JSON = *pkg.Gen.JSON
}
return cs
}
5 changes: 5 additions & 0 deletions internal/config/v_two.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func v2ParseConfig(rd io.Reader) (Config, error) {
}
}
}
if conf.SQL[j].Gen.JSON != nil {
if conf.SQL[j].Gen.JSON.Out == "" {
return conf, ErrNoOutPath
}
}
}
return conf, nil
}
Expand Down
8 changes: 6 additions & 2 deletions internal/endtoend/endtoend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ func cmpDirectory(t *testing.T, dir string, actual map[string]string) {
if file.IsDir() {
return nil
}
if !strings.HasSuffix(path, ".go") && !strings.HasSuffix(path, ".kt") && !strings.HasSuffix(path, ".py") {
if !strings.HasSuffix(path, ".go") && !strings.HasSuffix(path, ".kt") && !strings.HasSuffix(path, ".py") && !strings.HasSuffix(path, ".json") {
return nil
}
if filepath.Base(path) == "sqlc.json" {
return nil
}
if strings.Contains(path, "/kotlin/build") {
Expand All @@ -126,7 +129,8 @@ func cmpDirectory(t *testing.T, dir string, actual map[string]string) {
return nil
}
if strings.Contains(path, "/python/.venv") || strings.Contains(path, "/python/src/tests/") ||
strings.HasSuffix(path, "__init__.py") || strings.Contains(path, "/python/src/dbtest/") {
strings.HasSuffix(path, "__init__.py") || strings.Contains(path, "/python/src/dbtest/") ||
strings.Contains(path, "/python/.mypy_cache") {
return nil
}
blob, err := os.ReadFile(path)
Expand Down
Loading