diff --git a/Makefile b/Makefile index eb00412187..0a6db1fafa 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ build-endtoend: test-ci: test-examples build-endtoend -regen: sqlc-dev +regen: sqlc-dev sqlc-gen-json go run ./scripts/regenerate/ sqlc-dev: @@ -26,6 +26,9 @@ sqlc-dev: sqlc-pg-gen: go build -o ~/bin/sqlc-pg-gen ./internal/tools/sqlc-pg-gen +sqlc-gen-json: + go build -o ~/bin/sqlc-gen-json ./cmd/sqlc-gen-json + start: docker-compose up -d diff --git a/internal/cmd/generate.go b/internal/cmd/generate.go index 03cb8e6365..0dca81de06 100644 --- a/internal/cmd/generate.go +++ b/internal/cmd/generate.go @@ -17,6 +17,7 @@ import ( "github.com/kyleconroy/sqlc/internal/codegen/python" "github.com/kyleconroy/sqlc/internal/compiler" "github.com/kyleconroy/sqlc/internal/config" + "github.com/kyleconroy/sqlc/internal/config/convert" "github.com/kyleconroy/sqlc/internal/debug" "github.com/kyleconroy/sqlc/internal/ext" "github.com/kyleconroy/sqlc/internal/ext/process" @@ -295,6 +296,7 @@ func codegen(ctx context.Context, combo config.CombinedSettings, sql outPair, re if debug.Traced { region = trace.StartRegion(ctx, "codegen") } + req := codeGenRequest(result, combo) var handler ext.Handler var out string switch { @@ -335,10 +337,16 @@ func codegen(ctx context.Context, combo config.CombinedSettings, sql outPair, re return "", nil, fmt.Errorf("unsupported plugin type") } + opts, err := convert.YAMLtoJSON(sql.Plugin.Options) + if err != nil { + return "", nil, fmt.Errorf("invalid plugin options") + } + req.PluginOptions = opts + default: return "", nil, fmt.Errorf("missing language backend") } - resp, err := handler.Generate(ctx, codeGenRequest(result, combo)) + resp, err := handler.Generate(ctx, req) if region != nil { region.End() } diff --git a/internal/endtoend/testdata/codegen_json/gen/codegen.json b/internal/endtoend/testdata/codegen_json/gen/codegen.json index 812a9a1c2a..6a4372f9de 100644 --- a/internal/endtoend/testdata/codegen_json/gen/codegen.json +++ b/internal/endtoend/testdata/codegen_json/gen/codegen.json @@ -49697,5 +49697,6 @@ "insert_into_table": null } ], - "sqlc_version": "v1.15.0" + "sqlc_version": "v1.15.0", + "plugin_options": "" } diff --git a/internal/endtoend/testdata/process_plugin_sqlc_gen_json/gen/codegen.json b/internal/endtoend/testdata/process_plugin_sqlc_gen_json/gen/codegen.json index 2d36a8bcca..e362a889e6 100644 --- a/internal/endtoend/testdata/process_plugin_sqlc_gen_json/gen/codegen.json +++ b/internal/endtoend/testdata/process_plugin_sqlc_gen_json/gen/codegen.json @@ -49697,5 +49697,6 @@ "insert_into_table": null } ], - "sqlc_version": "v1.15.0" + "sqlc_version": "v1.15.0", + "plugin_options": "eyJmaWxlbmFtZSI6ImNvZGVnZW4uanNvbiIsImluZGVudCI6IiAgIn0=" } diff --git a/internal/plugin/codegen.pb.go b/internal/plugin/codegen.pb.go index 8806ca52c4..564024ef35 100644 --- a/internal/plugin/codegen.pb.go +++ b/internal/plugin/codegen.pb.go @@ -1,6 +1,6 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.27.1 +// protoc-gen-go v1.28.1 // protoc v3.19.4 // source: plugin/codegen.proto @@ -1636,10 +1636,11 @@ type CodeGenRequest struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Settings *Settings `protobuf:"bytes,1,opt,name=settings,proto3" json:"settings,omitempty"` - Catalog *Catalog `protobuf:"bytes,2,opt,name=catalog,proto3" json:"catalog,omitempty"` - Queries []*Query `protobuf:"bytes,3,rep,name=queries,proto3" json:"queries,omitempty"` - SqlcVersion string `protobuf:"bytes,4,opt,name=sqlc_version,proto3" json:"sqlc_version,omitempty"` + Settings *Settings `protobuf:"bytes,1,opt,name=settings,proto3" json:"settings,omitempty"` + Catalog *Catalog `protobuf:"bytes,2,opt,name=catalog,proto3" json:"catalog,omitempty"` + Queries []*Query `protobuf:"bytes,3,rep,name=queries,proto3" json:"queries,omitempty"` + SqlcVersion string `protobuf:"bytes,4,opt,name=sqlc_version,proto3" json:"sqlc_version,omitempty"` + PluginOptions []byte `protobuf:"bytes,5,opt,name=plugin_options,proto3" json:"plugin_options,omitempty"` } func (x *CodeGenRequest) Reset() { @@ -1702,6 +1703,13 @@ func (x *CodeGenRequest) GetSqlcVersion() string { return "" } +func (x *CodeGenRequest) GetPluginOptions() []byte { + if x != nil { + return x.PluginOptions + } + return nil +} + type CodeGenResponse struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -2022,7 +2030,7 @@ var file_plugin_codegen_proto_rawDesc = []byte{ 0x28, 0x05, 0x52, 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x12, 0x26, 0x0a, 0x06, 0x63, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x43, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x52, 0x06, 0x63, 0x6f, 0x6c, 0x75, - 0x6d, 0x6e, 0x22, 0xb6, 0x01, 0x0a, 0x0e, 0x43, 0x6f, 0x64, 0x65, 0x47, 0x65, 0x6e, 0x52, 0x65, + 0x6d, 0x6e, 0x22, 0xde, 0x01, 0x0a, 0x0e, 0x43, 0x6f, 0x64, 0x65, 0x47, 0x65, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x2c, 0x0a, 0x08, 0x73, 0x65, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x53, 0x65, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x73, 0x52, 0x08, 0x73, 0x65, 0x74, 0x74, 0x69, @@ -2033,14 +2041,16 @@ var file_plugin_codegen_proto_rawDesc = []byte{ 0x0d, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x51, 0x75, 0x65, 0x72, 0x79, 0x52, 0x07, 0x71, 0x75, 0x65, 0x72, 0x69, 0x65, 0x73, 0x12, 0x22, 0x0a, 0x0c, 0x73, 0x71, 0x6c, 0x63, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x73, - 0x71, 0x6c, 0x63, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x22, 0x35, 0x0a, 0x0f, 0x43, - 0x6f, 0x64, 0x65, 0x47, 0x65, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x22, - 0x0a, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, - 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x52, 0x05, 0x66, 0x69, 0x6c, - 0x65, 0x73, 0x42, 0x2c, 0x5a, 0x2a, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, - 0x2f, 0x6b, 0x79, 0x6c, 0x65, 0x63, 0x6f, 0x6e, 0x72, 0x6f, 0x79, 0x2f, 0x73, 0x71, 0x6c, 0x63, - 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, - 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x71, 0x6c, 0x63, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x26, 0x0a, 0x0e, 0x70, + 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x05, 0x20, + 0x01, 0x28, 0x0c, 0x52, 0x0e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x5f, 0x6f, 0x70, 0x74, 0x69, + 0x6f, 0x6e, 0x73, 0x22, 0x35, 0x0a, 0x0f, 0x43, 0x6f, 0x64, 0x65, 0x47, 0x65, 0x6e, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x22, 0x0a, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x18, + 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x46, + 0x69, 0x6c, 0x65, 0x52, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x42, 0x2c, 0x5a, 0x2a, 0x67, 0x69, + 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6b, 0x79, 0x6c, 0x65, 0x63, 0x6f, 0x6e, + 0x72, 0x6f, 0x79, 0x2f, 0x73, 0x71, 0x6c, 0x63, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, + 0x6c, 0x2f, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/internal/plugin/codegen_vtproto.pb.go b/internal/plugin/codegen_vtproto.pb.go index 5d8ce3a0cd..775e2ea04c 100644 --- a/internal/plugin/codegen_vtproto.pb.go +++ b/internal/plugin/codegen_vtproto.pb.go @@ -1623,6 +1623,13 @@ func (m *CodeGenRequest) MarshalToSizedBufferVT(dAtA []byte) (int, error) { i -= len(m.unknownFields) copy(dAtA[i:], m.unknownFields) } + if len(m.PluginOptions) > 0 { + i -= len(m.PluginOptions) + copy(dAtA[i:], m.PluginOptions) + i = encodeVarint(dAtA, i, uint64(len(m.PluginOptions))) + i-- + dAtA[i] = 0x2a + } if len(m.SqlcVersion) > 0 { i -= len(m.SqlcVersion) copy(dAtA[i:], m.SqlcVersion) @@ -2417,6 +2424,10 @@ func (m *CodeGenRequest) SizeVT() (n int) { if l > 0 { n += 1 + l + sov(uint64(l)) } + l = len(m.PluginOptions) + if l > 0 { + n += 1 + l + sov(uint64(l)) + } if m.unknownFields != nil { n += len(m.unknownFields) } @@ -6987,6 +6998,40 @@ func (m *CodeGenRequest) UnmarshalVT(dAtA []byte) error { } m.SqlcVersion = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex + case 5: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field PluginOptions", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflow + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLength + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLength + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.PluginOptions = append(m.PluginOptions[:0], dAtA[iNdEx:postIndex]...) + if m.PluginOptions == nil { + m.PluginOptions = []byte{} + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skip(dAtA[iNdEx:]) diff --git a/protos/plugin/codegen.proto b/protos/plugin/codegen.proto index 458a724eba..65244126ae 100644 --- a/protos/plugin/codegen.proto +++ b/protos/plugin/codegen.proto @@ -208,6 +208,7 @@ message CodeGenRequest Catalog catalog = 2 [json_name="catalog"]; repeated Query queries = 3 [json_name="queries"]; string sqlc_version = 4 [json_name="sqlc_version"]; + bytes plugin_options = 5 [json_name="plugin_options"]; } message CodeGenResponse