snapshot of https://github.com/sqlc-dev/sqlc.git (main, 2026-09-16; blobs > 131072B omitted)
Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
This commit is contained in:
68
internal/codegen/json/gen.go
Normal file
68
internal/codegen/json/gen.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
ejson "encoding/json"
|
||||
"fmt"
|
||||
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
|
||||
"github.com/sqlc-dev/sqlc/internal/plugin"
|
||||
)
|
||||
|
||||
func parseOptions(req *plugin.GenerateRequest) (*opts, error) {
|
||||
if len(req.PluginOptions) == 0 {
|
||||
return new(opts), nil
|
||||
}
|
||||
|
||||
var options *opts
|
||||
dec := ejson.NewDecoder(bytes.NewReader(req.PluginOptions))
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(&options); err != nil {
|
||||
return options, fmt.Errorf("unmarshalling options: %s", err)
|
||||
}
|
||||
return options, nil
|
||||
}
|
||||
|
||||
func Generate(ctx context.Context, req *plugin.GenerateRequest) (*plugin.GenerateResponse, error) {
|
||||
options, err := parseOptions(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
indent := " "
|
||||
if options.Indent != "" {
|
||||
indent = options.Indent
|
||||
}
|
||||
|
||||
filename := "codegen_request.json"
|
||||
if options.Filename != "" {
|
||||
filename = options.Filename
|
||||
}
|
||||
|
||||
// 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.GenerateResponse{
|
||||
Files: []*plugin.File{
|
||||
{
|
||||
Name: filename,
|
||||
Contents: append(blob, '\n'),
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
7
internal/codegen/json/opts.go
Normal file
7
internal/codegen/json/opts.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package json
|
||||
|
||||
type opts struct {
|
||||
Out string `json:"out"`
|
||||
Indent string `json:"indent,omitempty"`
|
||||
Filename string `json:"filename,omitempty"`
|
||||
}
|
||||
Reference in New Issue
Block a user