From 885a25e0ea1fb58f3c19d3812335a409fdb5d911 Mon Sep 17 00:00:00 2001 From: Yuya Kusakabe Date: Tue, 20 Apr 2021 14:45:57 +0900 Subject: [PATCH] Add ts_package extension Related to: https://github.com/protocolbuffers/protobuf/issues/6341 --- integration_tests/google/protobuf/empty.proto | 55 +++++ integration_tests/integration_test.ts | 4 + integration_tests/msg.pb.go | 9 +- integration_tests/package-lock.json | 12 + integration_tests/package.json | 2 + integration_tests/scripts/gen-protos.sh | 2 +- integration_tests/scripts/gen-server-proto.sh | 2 +- integration_tests/service.go | 6 + integration_tests/service.pb.go | 229 +++++++++++++----- integration_tests/service.pb.gw.go | 101 ++++++++ integration_tests/service.proto | 10 + options/gen.sh | 3 + options/ts_package.pb.go | 95 ++++++++ options/ts_package.proto | 11 + registry/file.go | 5 + registry/registry.go | 51 ++-- 16 files changed, 505 insertions(+), 92 deletions(-) create mode 100644 integration_tests/google/protobuf/empty.proto create mode 100755 options/gen.sh create mode 100644 options/ts_package.pb.go create mode 100644 options/ts_package.proto diff --git a/integration_tests/google/protobuf/empty.proto b/integration_tests/google/protobuf/empty.proto new file mode 100644 index 0000000..8725517 --- /dev/null +++ b/integration_tests/google/protobuf/empty.proto @@ -0,0 +1,55 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +syntax = "proto3"; + +package google.protobuf; + +option csharp_namespace = "Google.Protobuf.WellKnownTypes"; +option go_package = "google.golang.org/protobuf/types/known/emptypb"; +option java_package = "com.google.protobuf"; +option java_outer_classname = "EmptyProto"; +option java_multiple_files = true; +option objc_class_prefix = "GPB"; +option cc_enable_arenas = true; + +// A generic empty message that you can re-use to avoid defining duplicated +// empty messages in your APIs. A typical example is to use it as the request +// or the response type of an API method. For instance: +// +// service Foo { +// rpc Bar(google.protobuf.Empty) returns (google.protobuf.Empty); +// } +// +// The JSON representation for `Empty` is empty JSON object `{}`. +message Empty {} + +import "protoc-gen-grpc-gateway-ts/options/ts_package.proto"; +option (grpc.gateway.protoc_gen_grpc_gateway_ts.options.ts_package) = "google-protobuf/google/protobuf/empty_pb"; diff --git a/integration_tests/integration_test.ts b/integration_tests/integration_test.ts index 20d3ae8..a30ebc4 100644 --- a/integration_tests/integration_test.ts +++ b/integration_tests/integration_test.ts @@ -52,5 +52,9 @@ describe("test grpc-gateway-ts communication", () => { expect(getField(result, 'patch_result')).to.equal(33) }) + it('http delete check request', async () => { + const result = await CounterService.HTTPDelete({a: 10}, {pathPrefix: "http://localhost:8081"}) + expect(result).to.be.empty + }) }) diff --git a/integration_tests/msg.pb.go b/integration_tests/msg.pb.go index 4d144bd..e3abbe9 100644 --- a/integration_tests/msg.pb.go +++ b/integration_tests/msg.pb.go @@ -1,13 +1,12 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.25.0 -// protoc v3.15.6 +// protoc-gen-go v1.26.0 +// protoc v3.15.8 // source: msg.proto package main import ( - proto "github.com/golang/protobuf/proto" protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" @@ -21,10 +20,6 @@ const ( _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) -// This is a compile-time assertion that a sufficiently up-to-date version -// of the legacy proto package is being used. -const _ = proto.ProtoPackageIsVersion4 - type ExternalRequest struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache diff --git a/integration_tests/package-lock.json b/integration_tests/package-lock.json index 5c5af13..1aaea3c 100644 --- a/integration_tests/package-lock.json +++ b/integration_tests/package-lock.json @@ -281,6 +281,12 @@ "integrity": "sha512-G+ITQPXkwTrslfG5L/BksmbLUA0M1iybEsmCWPqzSxsRRhJZimBKJkoMi8fr/CPygPTj4zO5pJH7I2/cm9M7SQ==", "dev": true }, + "@types/google-protobuf": { + "version": "3.15.1", + "resolved": "https://registry.npmjs.org/@types/google-protobuf/-/google-protobuf-3.15.1.tgz", + "integrity": "sha512-GpEv+jQXPTK7U7oznibH/L6BZMANKNISVZaSM6d5O2nnuKw8N3YIoyxaKrbIR4H4HF8ksxoFzU0kpdksJK7jfw==", + "dev": true + }, "@types/lodash": { "version": "4.14.165", "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.165.tgz", @@ -1473,6 +1479,12 @@ "integrity": "sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==", "dev": true }, + "google-protobuf": { + "version": "3.15.8", + "resolved": "https://registry.npmjs.org/google-protobuf/-/google-protobuf-3.15.8.tgz", + "integrity": "sha512-2jtfdqTaSxk0cuBJBtTTWsot4WtR9RVr2rXg7x7OoqiuOKopPrwXpM1G4dXIkLcUNRh3RKzz76C8IOkksZSeOw==", + "dev": true + }, "graceful-fs": { "version": "4.2.4", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.4.tgz", diff --git a/integration_tests/package.json b/integration_tests/package.json index 915e69f..60d30e0 100644 --- a/integration_tests/package.json +++ b/integration_tests/package.json @@ -10,10 +10,12 @@ "license": "ISC", "devDependencies": { "@types/chai": "^4.2.14", + "@types/google-protobuf": "^3.15.1", "@types/lodash.camelcase": "^4.3.6", "@types/mocha": "^8.0.3", "@types/ramda": "^0.27.32", "chai": "^4.2.0", + "google-protobuf": "^3.15.8", "karma": "^5.2.3", "karma-chai": "^0.1.0", "karma-chrome-launcher": "^3.1.0", diff --git a/integration_tests/scripts/gen-protos.sh b/integration_tests/scripts/gen-protos.sh index b711a2e..165bbd5 100755 --- a/integration_tests/scripts/gen-protos.sh +++ b/integration_tests/scripts/gen-protos.sh @@ -1,6 +1,6 @@ #!/bin/bash USE_PROTO_NAMES=${1:-"false"} cd .. && go install && cd integration_tests && \ - protoc -I . \ + protoc -I . -I ../.. \ --grpc-gateway-ts_out=logtostderr=true,use_proto_names=$USE_PROTO_NAMES,loglevel=debug:./ \ service.proto msg.proto empty.proto \ No newline at end of file diff --git a/integration_tests/scripts/gen-server-proto.sh b/integration_tests/scripts/gen-server-proto.sh index c92e5e1..b6485d9 100755 --- a/integration_tests/scripts/gen-server-proto.sh +++ b/integration_tests/scripts/gen-server-proto.sh @@ -1,6 +1,6 @@ #!/bin/bash -protoc -I . --go_out ./ --go_opt plugins=grpc --go_opt paths=source_relative \ +protoc -I . -I ../.. --go_out ./ --go_opt plugins=grpc --go_opt paths=source_relative \ --grpc-gateway_out ./ --grpc-gateway_opt logtostderr=true \ --grpc-gateway_opt paths=source_relative \ --grpc-gateway_opt generate_unbound_methods=true \ diff --git a/integration_tests/service.go b/integration_tests/service.go index 687e42b..e8fd757 100644 --- a/integration_tests/service.go +++ b/integration_tests/service.go @@ -3,6 +3,8 @@ package main import ( "context" "time" + + "google.golang.org/protobuf/types/known/emptypb" ) type RealCounterService struct{} @@ -61,3 +63,7 @@ func (r *RealCounterService) HTTPPatch(ctx context.Context, in *HttpPatchRequest PatchResult: in.A + in.C, }, nil } + +func (r *RealCounterService) HTTPDelete(ctx context.Context, req *HttpDeleteRequest) (*emptypb.Empty, error) { + return &emptypb.Empty{}, nil +} diff --git a/integration_tests/service.pb.go b/integration_tests/service.pb.go index b97e7e4..9e9778f 100644 --- a/integration_tests/service.pb.go +++ b/integration_tests/service.pb.go @@ -1,20 +1,20 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.25.0 -// protoc v3.15.6 +// protoc-gen-go v1.26.0 +// protoc v3.15.8 // source: service.proto package main import ( context "context" - proto "github.com/golang/protobuf/proto" _ "google.golang.org/genproto/googleapis/api/annotations" grpc "google.golang.org/grpc" codes "google.golang.org/grpc/codes" status "google.golang.org/grpc/status" protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" + emptypb "google.golang.org/protobuf/types/known/emptypb" reflect "reflect" sync "sync" ) @@ -26,10 +26,6 @@ const ( _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) -// This is a compile-time assertion that a sufficiently up-to-date version -// of the legacy proto package is being used. -const _ = proto.ProtoPackageIsVersion4 - type UnaryRequest struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -571,48 +567,99 @@ func (x *HttpPatchResponse) GetPatchResult() int32 { return 0 } +type HttpDeleteRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + A int32 `protobuf:"varint,1,opt,name=a,proto3" json:"a,omitempty"` +} + +func (x *HttpDeleteRequest) Reset() { + *x = HttpDeleteRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_service_proto_msgTypes[11] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *HttpDeleteRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*HttpDeleteRequest) ProtoMessage() {} + +func (x *HttpDeleteRequest) ProtoReflect() protoreflect.Message { + mi := &file_service_proto_msgTypes[11] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use HttpDeleteRequest.ProtoReflect.Descriptor instead. +func (*HttpDeleteRequest) Descriptor() ([]byte, []int) { + return file_service_proto_rawDescGZIP(), []int{11} +} + +func (x *HttpDeleteRequest) GetA() int32 { + if x != nil { + return x.A + } + return 0 +} + var File_service_proto protoreflect.FileDescriptor var file_service_proto_rawDesc = []byte{ 0x0a, 0x0d, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x1a, 0x1c, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x1a, 0x09, 0x6d, 0x73, 0x67, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x28, - 0x0a, 0x0c, 0x55, 0x6e, 0x61, 0x72, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x18, - 0x0a, 0x07, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, - 0x07, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x65, 0x72, 0x22, 0x27, 0x0a, 0x0d, 0x55, 0x6e, 0x61, 0x72, - 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x72, 0x65, 0x73, - 0x75, 0x6c, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, - 0x74, 0x22, 0x2c, 0x0a, 0x10, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x69, 0x6e, 0x67, 0x52, 0x65, - 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x65, 0x72, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x65, 0x72, 0x22, - 0x2b, 0x0a, 0x11, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x73, 0x70, - 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x01, - 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x38, 0x0a, 0x0e, - 0x48, 0x74, 0x74, 0x70, 0x47, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x26, - 0x0a, 0x0f, 0x6e, 0x75, 0x6d, 0x5f, 0x74, 0x6f, 0x5f, 0x69, 0x6e, 0x63, 0x72, 0x65, 0x61, 0x73, - 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0d, 0x6e, 0x75, 0x6d, 0x54, 0x6f, 0x49, 0x6e, - 0x63, 0x72, 0x65, 0x61, 0x73, 0x65, 0x22, 0x29, 0x0a, 0x0f, 0x48, 0x74, 0x74, 0x70, 0x47, 0x65, - 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x72, 0x65, 0x73, - 0x75, 0x6c, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, - 0x74, 0x22, 0x52, 0x0a, 0x0f, 0x48, 0x74, 0x74, 0x70, 0x50, 0x6f, 0x73, 0x74, 0x52, 0x65, 0x71, - 0x75, 0x65, 0x73, 0x74, 0x12, 0x0c, 0x0a, 0x01, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, - 0x01, 0x61, 0x12, 0x23, 0x0a, 0x03, 0x72, 0x65, 0x71, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, - 0x11, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x50, 0x6f, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, - 0x73, 0x74, 0x52, 0x03, 0x72, 0x65, 0x71, 0x12, 0x0c, 0x0a, 0x01, 0x63, 0x18, 0x03, 0x20, 0x01, - 0x28, 0x05, 0x52, 0x01, 0x63, 0x22, 0x1b, 0x0a, 0x0b, 0x50, 0x6f, 0x73, 0x74, 0x52, 0x65, 0x71, - 0x75, 0x65, 0x73, 0x74, 0x12, 0x0c, 0x0a, 0x01, 0x62, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, - 0x01, 0x62, 0x22, 0x33, 0x0a, 0x10, 0x48, 0x74, 0x74, 0x70, 0x50, 0x6f, 0x73, 0x74, 0x52, 0x65, - 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1f, 0x0a, 0x0b, 0x70, 0x6f, 0x73, 0x74, 0x5f, 0x72, - 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0a, 0x70, 0x6f, 0x73, - 0x74, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x2e, 0x0a, 0x10, 0x48, 0x74, 0x74, 0x70, 0x50, - 0x61, 0x74, 0x63, 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x0c, 0x0a, 0x01, 0x61, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x01, 0x61, 0x12, 0x0c, 0x0a, 0x01, 0x63, 0x18, 0x02, - 0x20, 0x01, 0x28, 0x05, 0x52, 0x01, 0x63, 0x22, 0x36, 0x0a, 0x11, 0x48, 0x74, 0x74, 0x70, 0x50, - 0x61, 0x74, 0x63, 0x68, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x21, 0x0a, 0x0c, - 0x70, 0x61, 0x74, 0x63, 0x68, 0x5f, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x02, 0x20, 0x01, - 0x28, 0x05, 0x52, 0x0b, 0x70, 0x61, 0x74, 0x63, 0x68, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x32, - 0xbb, 0x04, 0x0a, 0x0e, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x65, 0x72, 0x53, 0x65, 0x72, 0x76, 0x69, + 0x6f, 0x74, 0x6f, 0x1a, 0x1b, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x1a, 0x09, 0x6d, 0x73, 0x67, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x28, 0x0a, 0x0c, 0x55, + 0x6e, 0x61, 0x72, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x63, + 0x6f, 0x75, 0x6e, 0x74, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x63, 0x6f, + 0x75, 0x6e, 0x74, 0x65, 0x72, 0x22, 0x27, 0x0a, 0x0d, 0x55, 0x6e, 0x61, 0x72, 0x79, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x2c, + 0x0a, 0x10, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x65, 0x72, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x05, 0x52, 0x07, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x65, 0x72, 0x22, 0x2b, 0x0a, 0x11, + 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x12, 0x16, 0x0a, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x05, 0x52, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x38, 0x0a, 0x0e, 0x48, 0x74, 0x74, + 0x70, 0x47, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x26, 0x0a, 0x0f, 0x6e, + 0x75, 0x6d, 0x5f, 0x74, 0x6f, 0x5f, 0x69, 0x6e, 0x63, 0x72, 0x65, 0x61, 0x73, 0x65, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x05, 0x52, 0x0d, 0x6e, 0x75, 0x6d, 0x54, 0x6f, 0x49, 0x6e, 0x63, 0x72, 0x65, + 0x61, 0x73, 0x65, 0x22, 0x29, 0x0a, 0x0f, 0x48, 0x74, 0x74, 0x70, 0x47, 0x65, 0x74, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x52, + 0x0a, 0x0f, 0x48, 0x74, 0x74, 0x70, 0x50, 0x6f, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x12, 0x0c, 0x0a, 0x01, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x01, 0x61, 0x12, + 0x23, 0x0a, 0x03, 0x72, 0x65, 0x71, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x6d, + 0x61, 0x69, 0x6e, 0x2e, 0x50, 0x6f, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x52, + 0x03, 0x72, 0x65, 0x71, 0x12, 0x0c, 0x0a, 0x01, 0x63, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, + 0x01, 0x63, 0x22, 0x1b, 0x0a, 0x0b, 0x50, 0x6f, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x12, 0x0c, 0x0a, 0x01, 0x62, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x01, 0x62, 0x22, + 0x33, 0x0a, 0x10, 0x48, 0x74, 0x74, 0x70, 0x50, 0x6f, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x12, 0x1f, 0x0a, 0x0b, 0x70, 0x6f, 0x73, 0x74, 0x5f, 0x72, 0x65, 0x73, 0x75, + 0x6c, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0a, 0x70, 0x6f, 0x73, 0x74, 0x52, 0x65, + 0x73, 0x75, 0x6c, 0x74, 0x22, 0x2e, 0x0a, 0x10, 0x48, 0x74, 0x74, 0x70, 0x50, 0x61, 0x74, 0x63, + 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x0c, 0x0a, 0x01, 0x61, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x05, 0x52, 0x01, 0x61, 0x12, 0x0c, 0x0a, 0x01, 0x63, 0x18, 0x02, 0x20, 0x01, 0x28, + 0x05, 0x52, 0x01, 0x63, 0x22, 0x36, 0x0a, 0x11, 0x48, 0x74, 0x74, 0x70, 0x50, 0x61, 0x74, 0x63, + 0x68, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x21, 0x0a, 0x0c, 0x70, 0x61, 0x74, + 0x63, 0x68, 0x5f, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, + 0x0b, 0x70, 0x61, 0x74, 0x63, 0x68, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x21, 0x0a, 0x11, + 0x48, 0x74, 0x74, 0x70, 0x44, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x12, 0x0c, 0x0a, 0x01, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x01, 0x61, 0x32, + 0x8f, 0x05, 0x0a, 0x0e, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x65, 0x72, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x34, 0x0a, 0x09, 0x49, 0x6e, 0x63, 0x72, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x12, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x55, 0x6e, 0x61, 0x72, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x55, 0x6e, 0x61, 0x72, 0x79, @@ -644,11 +691,17 @@ var file_service_proto_rawDesc = []byte{ 0x63, 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x74, 0x74, 0x70, 0x50, 0x61, 0x74, 0x63, 0x68, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x11, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x0b, 0x32, 0x06, 0x2f, 0x70, 0x61, 0x74, - 0x63, 0x68, 0x3a, 0x01, 0x2a, 0x12, 0x36, 0x0a, 0x0f, 0x45, 0x78, 0x74, 0x65, 0x72, 0x6e, 0x61, - 0x6c, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x10, 0x2e, 0x45, 0x78, 0x74, 0x65, 0x72, - 0x6e, 0x61, 0x6c, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x11, 0x2e, 0x45, 0x78, 0x74, - 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x09, 0x5a, - 0x07, 0x2e, 0x2f, 0x3b, 0x6d, 0x61, 0x69, 0x6e, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x63, 0x68, 0x3a, 0x01, 0x2a, 0x12, 0x52, 0x0a, 0x0a, 0x48, 0x54, 0x54, 0x50, 0x44, 0x65, 0x6c, + 0x65, 0x74, 0x65, 0x12, 0x17, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x74, 0x74, 0x70, 0x44, + 0x65, 0x6c, 0x65, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x67, + 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, + 0x6d, 0x70, 0x74, 0x79, 0x22, 0x13, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x0d, 0x2a, 0x0b, 0x2f, 0x64, + 0x65, 0x6c, 0x65, 0x74, 0x65, 0x2f, 0x7b, 0x61, 0x7d, 0x12, 0x36, 0x0a, 0x0f, 0x45, 0x78, 0x74, + 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x10, 0x2e, 0x45, + 0x78, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x11, + 0x2e, 0x45, 0x78, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x42, 0x09, 0x5a, 0x07, 0x2e, 0x2f, 0x3b, 0x6d, 0x61, 0x69, 0x6e, 0x62, 0x06, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -663,7 +716,7 @@ func file_service_proto_rawDescGZIP() []byte { return file_service_proto_rawDescData } -var file_service_proto_msgTypes = make([]protoimpl.MessageInfo, 11) +var file_service_proto_msgTypes = make([]protoimpl.MessageInfo, 12) var file_service_proto_goTypes = []interface{}{ (*UnaryRequest)(nil), // 0: main.UnaryRequest (*UnaryResponse)(nil), // 1: main.UnaryResponse @@ -676,8 +729,10 @@ var file_service_proto_goTypes = []interface{}{ (*HttpPostResponse)(nil), // 8: main.HttpPostResponse (*HttpPatchRequest)(nil), // 9: main.HttpPatchRequest (*HttpPatchResponse)(nil), // 10: main.HttpPatchResponse - (*ExternalRequest)(nil), // 11: ExternalRequest - (*ExternalResponse)(nil), // 12: ExternalResponse + (*HttpDeleteRequest)(nil), // 11: main.HttpDeleteRequest + (*ExternalRequest)(nil), // 12: ExternalRequest + (*emptypb.Empty)(nil), // 13: google.protobuf.Empty + (*ExternalResponse)(nil), // 14: ExternalResponse } var file_service_proto_depIdxs = []int32{ 7, // 0: main.HttpPostRequest.req:type_name -> main.PostRequest @@ -687,16 +742,18 @@ var file_service_proto_depIdxs = []int32{ 6, // 4: main.CounterService.HTTPPostWithNestedBodyPath:input_type -> main.HttpPostRequest 6, // 5: main.CounterService.HTTPPostWithStarBodyPath:input_type -> main.HttpPostRequest 9, // 6: main.CounterService.HTTPPatch:input_type -> main.HttpPatchRequest - 11, // 7: main.CounterService.ExternalMessage:input_type -> ExternalRequest - 1, // 8: main.CounterService.Increment:output_type -> main.UnaryResponse - 3, // 9: main.CounterService.StreamingIncrements:output_type -> main.StreamingResponse - 5, // 10: main.CounterService.HTTPGet:output_type -> main.HttpGetResponse - 8, // 11: main.CounterService.HTTPPostWithNestedBodyPath:output_type -> main.HttpPostResponse - 8, // 12: main.CounterService.HTTPPostWithStarBodyPath:output_type -> main.HttpPostResponse - 10, // 13: main.CounterService.HTTPPatch:output_type -> main.HttpPatchResponse - 12, // 14: main.CounterService.ExternalMessage:output_type -> ExternalResponse - 8, // [8:15] is the sub-list for method output_type - 1, // [1:8] is the sub-list for method input_type + 11, // 7: main.CounterService.HTTPDelete:input_type -> main.HttpDeleteRequest + 12, // 8: main.CounterService.ExternalMessage:input_type -> ExternalRequest + 1, // 9: main.CounterService.Increment:output_type -> main.UnaryResponse + 3, // 10: main.CounterService.StreamingIncrements:output_type -> main.StreamingResponse + 5, // 11: main.CounterService.HTTPGet:output_type -> main.HttpGetResponse + 8, // 12: main.CounterService.HTTPPostWithNestedBodyPath:output_type -> main.HttpPostResponse + 8, // 13: main.CounterService.HTTPPostWithStarBodyPath:output_type -> main.HttpPostResponse + 10, // 14: main.CounterService.HTTPPatch:output_type -> main.HttpPatchResponse + 13, // 15: main.CounterService.HTTPDelete:output_type -> google.protobuf.Empty + 14, // 16: main.CounterService.ExternalMessage:output_type -> ExternalResponse + 9, // [9:17] is the sub-list for method output_type + 1, // [1:9] is the sub-list for method input_type 1, // [1:1] is the sub-list for extension type_name 1, // [1:1] is the sub-list for extension extendee 0, // [0:1] is the sub-list for field type_name @@ -841,6 +898,18 @@ func file_service_proto_init() { return nil } } + file_service_proto_msgTypes[11].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*HttpDeleteRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } } type x struct{} out := protoimpl.TypeBuilder{ @@ -848,7 +917,7 @@ func file_service_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_service_proto_rawDesc, NumEnums: 0, - NumMessages: 11, + NumMessages: 12, NumExtensions: 0, NumServices: 1, }, @@ -880,6 +949,7 @@ type CounterServiceClient interface { HTTPPostWithNestedBodyPath(ctx context.Context, in *HttpPostRequest, opts ...grpc.CallOption) (*HttpPostResponse, error) HTTPPostWithStarBodyPath(ctx context.Context, in *HttpPostRequest, opts ...grpc.CallOption) (*HttpPostResponse, error) HTTPPatch(ctx context.Context, in *HttpPatchRequest, opts ...grpc.CallOption) (*HttpPatchResponse, error) + HTTPDelete(ctx context.Context, in *HttpDeleteRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) ExternalMessage(ctx context.Context, in *ExternalRequest, opts ...grpc.CallOption) (*ExternalResponse, error) } @@ -968,6 +1038,15 @@ func (c *counterServiceClient) HTTPPatch(ctx context.Context, in *HttpPatchReque return out, nil } +func (c *counterServiceClient) HTTPDelete(ctx context.Context, in *HttpDeleteRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) { + out := new(emptypb.Empty) + err := c.cc.Invoke(ctx, "/main.CounterService/HTTPDelete", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + func (c *counterServiceClient) ExternalMessage(ctx context.Context, in *ExternalRequest, opts ...grpc.CallOption) (*ExternalResponse, error) { out := new(ExternalResponse) err := c.cc.Invoke(ctx, "/main.CounterService/ExternalMessage", in, out, opts...) @@ -985,6 +1064,7 @@ type CounterServiceServer interface { HTTPPostWithNestedBodyPath(context.Context, *HttpPostRequest) (*HttpPostResponse, error) HTTPPostWithStarBodyPath(context.Context, *HttpPostRequest) (*HttpPostResponse, error) HTTPPatch(context.Context, *HttpPatchRequest) (*HttpPatchResponse, error) + HTTPDelete(context.Context, *HttpDeleteRequest) (*emptypb.Empty, error) ExternalMessage(context.Context, *ExternalRequest) (*ExternalResponse, error) } @@ -1010,6 +1090,9 @@ func (*UnimplementedCounterServiceServer) HTTPPostWithStarBodyPath(context.Conte func (*UnimplementedCounterServiceServer) HTTPPatch(context.Context, *HttpPatchRequest) (*HttpPatchResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method HTTPPatch not implemented") } +func (*UnimplementedCounterServiceServer) HTTPDelete(context.Context, *HttpDeleteRequest) (*emptypb.Empty, error) { + return nil, status.Errorf(codes.Unimplemented, "method HTTPDelete not implemented") +} func (*UnimplementedCounterServiceServer) ExternalMessage(context.Context, *ExternalRequest) (*ExternalResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method ExternalMessage not implemented") } @@ -1129,6 +1212,24 @@ func _CounterService_HTTPPatch_Handler(srv interface{}, ctx context.Context, dec return interceptor(ctx, in, info, handler) } +func _CounterService_HTTPDelete_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(HttpDeleteRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(CounterServiceServer).HTTPDelete(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/main.CounterService/HTTPDelete", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(CounterServiceServer).HTTPDelete(ctx, req.(*HttpDeleteRequest)) + } + return interceptor(ctx, in, info, handler) +} + func _CounterService_ExternalMessage_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(ExternalRequest) if err := dec(in); err != nil { @@ -1171,6 +1272,10 @@ var _CounterService_serviceDesc = grpc.ServiceDesc{ MethodName: "HTTPPatch", Handler: _CounterService_HTTPPatch_Handler, }, + { + MethodName: "HTTPDelete", + Handler: _CounterService_HTTPDelete_Handler, + }, { MethodName: "ExternalMessage", Handler: _CounterService_ExternalMessage_Handler, diff --git a/integration_tests/service.pb.gw.go b/integration_tests/service.pb.gw.go index dcbc9f7..d8ef044 100644 --- a/integration_tests/service.pb.gw.go +++ b/integration_tests/service.pb.gw.go @@ -360,6 +360,60 @@ func local_request_CounterService_HTTPPatch_0(ctx context.Context, marshaler run } +func request_CounterService_HTTPDelete_0(ctx context.Context, marshaler runtime.Marshaler, client CounterServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq HttpDeleteRequest + var metadata runtime.ServerMetadata + + var ( + val string + ok bool + err error + _ = err + ) + + val, ok = pathParams["a"] + if !ok { + return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "a") + } + + protoReq.A, err = runtime.Int32(val) + + if err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "a", err) + } + + msg, err := client.HTTPDelete(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +func local_request_CounterService_HTTPDelete_0(ctx context.Context, marshaler runtime.Marshaler, server CounterServiceServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq HttpDeleteRequest + var metadata runtime.ServerMetadata + + var ( + val string + ok bool + err error + _ = err + ) + + val, ok = pathParams["a"] + if !ok { + return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "a") + } + + protoReq.A, err = runtime.Int32(val) + + if err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "a", err) + } + + msg, err := server.HTTPDelete(ctx, &protoReq) + return msg, metadata, err + +} + func request_CounterService_ExternalMessage_0(ctx context.Context, marshaler runtime.Marshaler, client CounterServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq ExternalRequest var metadata runtime.ServerMetadata @@ -522,6 +576,29 @@ func RegisterCounterServiceHandlerServer(ctx context.Context, mux *runtime.Serve }) + mux.Handle("DELETE", pattern_CounterService_HTTPDelete_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + var stream runtime.ServerTransportStream + ctx = grpc.NewContextWithServerTransportStream(ctx, &stream) + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateIncomingContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := local_request_CounterService_HTTPDelete_0(rctx, inboundMarshaler, server, req, pathParams) + md.HeaderMD, md.TrailerMD = metadata.Join(md.HeaderMD, stream.Header()), metadata.Join(md.TrailerMD, stream.Trailer()) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_CounterService_HTTPDelete_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + mux.Handle("POST", pattern_CounterService_ExternalMessage_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { ctx, cancel := context.WithCancel(req.Context()) defer cancel() @@ -706,6 +783,26 @@ func RegisterCounterServiceHandlerClient(ctx context.Context, mux *runtime.Serve }) + mux.Handle("DELETE", pattern_CounterService_HTTPDelete_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_CounterService_HTTPDelete_0(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_CounterService_HTTPDelete_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + mux.Handle("POST", pattern_CounterService_ExternalMessage_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { ctx, cancel := context.WithCancel(req.Context()) defer cancel() @@ -742,6 +839,8 @@ var ( pattern_CounterService_HTTPPatch_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0}, []string{"patch"}, "", runtime.AssumeColonVerbOpt(true))) + pattern_CounterService_HTTPDelete_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 1, 0, 4, 1, 5, 1}, []string{"delete", "a"}, "", runtime.AssumeColonVerbOpt(true))) + pattern_CounterService_ExternalMessage_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1}, []string{"main.CounterService", "ExternalMessage"}, "", runtime.AssumeColonVerbOpt(true))) ) @@ -758,5 +857,7 @@ var ( forward_CounterService_HTTPPatch_0 = runtime.ForwardResponseMessage + forward_CounterService_HTTPDelete_0 = runtime.ForwardResponseMessage + forward_CounterService_ExternalMessage_0 = runtime.ForwardResponseMessage ) diff --git a/integration_tests/service.proto b/integration_tests/service.proto index 7797ee8..1937985 100644 --- a/integration_tests/service.proto +++ b/integration_tests/service.proto @@ -3,6 +3,7 @@ package main; option go_package = "./;main"; import "google/api/annotations.proto"; +import "google/protobuf/empty.proto"; import "msg.proto"; message UnaryRequest { @@ -52,6 +53,10 @@ message HttpPatchResponse { int32 patch_result = 2; } +message HttpDeleteRequest { + int32 a = 1; +} + service CounterService { rpc Increment(UnaryRequest) returns (UnaryResponse); rpc StreamingIncrements(StreamingRequest) returns (stream StreamingResponse); @@ -78,5 +83,10 @@ service CounterService { body: "*" }; } + rpc HTTPDelete(HttpDeleteRequest) returns (google.protobuf.Empty) { + option (google.api.http) = { + delete: "/delete/{a}" + }; + } rpc ExternalMessage(ExternalRequest) returns (ExternalResponse); } diff --git a/options/gen.sh b/options/gen.sh new file mode 100755 index 0000000..5545fb5 --- /dev/null +++ b/options/gen.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +protoc --go_out=paths=source_relative:. *.proto diff --git a/options/ts_package.pb.go b/options/ts_package.pb.go new file mode 100644 index 0000000..b8fb229 --- /dev/null +++ b/options/ts_package.pb.go @@ -0,0 +1,95 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.26.0 +// protoc v3.12.4 +// source: ts_package.proto + +package options + +import ( + descriptor "github.com/golang/protobuf/protoc-gen-go/descriptor" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +var file_ts_package_proto_extTypes = []protoimpl.ExtensionInfo{ + { + ExtendedType: (*descriptor.FileOptions)(nil), + ExtensionType: (*string)(nil), + Field: 50000, + Name: "grpc.gateway.protoc_gen_grpc_gateway_ts.options.ts_package", + Tag: "bytes,50000,opt,name=ts_package", + Filename: "ts_package.proto", + }, +} + +// Extension fields to descriptor.FileOptions. +var ( + // optional string ts_package = 50000; + E_TsPackage = &file_ts_package_proto_extTypes[0] +) + +var File_ts_package_proto protoreflect.FileDescriptor + +var file_ts_package_proto_rawDesc = []byte{ + 0x0a, 0x10, 0x74, 0x73, 0x5f, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x12, 0x2f, 0x67, 0x72, 0x70, 0x63, 0x2e, 0x67, 0x61, 0x74, 0x65, 0x77, 0x61, 0x79, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x5f, 0x67, 0x65, 0x6e, 0x5f, 0x67, 0x72, 0x70, 0x63, + 0x5f, 0x67, 0x61, 0x74, 0x65, 0x77, 0x61, 0x79, 0x5f, 0x74, 0x73, 0x2e, 0x6f, 0x70, 0x74, 0x69, + 0x6f, 0x6e, 0x73, 0x1a, 0x20, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3a, 0x40, 0x0a, 0x0a, 0x74, 0x73, 0x5f, 0x70, 0x61, 0x63, 0x6b, + 0x61, 0x67, 0x65, 0x12, 0x1c, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, + 0x73, 0x18, 0xd0, 0x86, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x74, 0x73, 0x50, 0x61, 0x63, + 0x6b, 0x61, 0x67, 0x65, 0x88, 0x01, 0x01, 0x42, 0x3e, 0x5a, 0x3c, 0x67, 0x69, 0x74, 0x68, 0x75, + 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x67, 0x72, 0x70, 0x63, 0x2d, 0x65, 0x63, 0x6f, 0x73, 0x79, + 0x73, 0x74, 0x65, 0x6d, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x2d, 0x67, 0x65, 0x6e, 0x2d, + 0x67, 0x72, 0x70, 0x63, 0x2d, 0x67, 0x61, 0x74, 0x65, 0x77, 0x61, 0x79, 0x2d, 0x74, 0x73, 0x2f, + 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var file_ts_package_proto_goTypes = []interface{}{ + (*descriptor.FileOptions)(nil), // 0: google.protobuf.FileOptions +} +var file_ts_package_proto_depIdxs = []int32{ + 0, // 0: grpc.gateway.protoc_gen_grpc_gateway_ts.options.ts_package:extendee -> google.protobuf.FileOptions + 1, // [1:1] is the sub-list for method output_type + 1, // [1:1] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 0, // [0:1] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_ts_package_proto_init() } +func file_ts_package_proto_init() { + if File_ts_package_proto != nil { + return + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_ts_package_proto_rawDesc, + NumEnums: 0, + NumMessages: 0, + NumExtensions: 1, + NumServices: 0, + }, + GoTypes: file_ts_package_proto_goTypes, + DependencyIndexes: file_ts_package_proto_depIdxs, + ExtensionInfos: file_ts_package_proto_extTypes, + }.Build() + File_ts_package_proto = out.File + file_ts_package_proto_rawDesc = nil + file_ts_package_proto_goTypes = nil + file_ts_package_proto_depIdxs = nil +} diff --git a/options/ts_package.proto b/options/ts_package.proto new file mode 100644 index 0000000..fb36dbc --- /dev/null +++ b/options/ts_package.proto @@ -0,0 +1,11 @@ +syntax = "proto3"; + +package grpc.gateway.protoc_gen_grpc_gateway_ts.options; + +option go_package = "github.com/grpc-ecosystem/protoc-gen-grpc-gateway-ts/options"; + +import "google/protobuf/descriptor.proto"; + +extend google.protobuf.FileOptions { + optional string ts_package = 50000; +} diff --git a/registry/file.go b/registry/file.go index ae0d93f..058a4fd 100644 --- a/registry/file.go +++ b/registry/file.go @@ -6,8 +6,10 @@ import ( descriptorpb "github.com/golang/protobuf/protoc-gen-go/descriptor" "github.com/grpc-ecosystem/protoc-gen-grpc-gateway-ts/data" + "github.com/grpc-ecosystem/protoc-gen-grpc-gateway-ts/options" "github.com/pkg/errors" log "github.com/sirupsen/logrus" // nolint: depguard + "google.golang.org/protobuf/proto" ) func (r *Registry) analyseFile(f *descriptorpb.FileDescriptorProto) (*data.File, error) { @@ -18,6 +20,9 @@ func (r *Registry) analyseFile(f *descriptorpb.FileDescriptorProto) (*data.File, parents := make([]string, 0) fileData.Name = fileName fileData.TSFileName = data.GetTSFileName(fileName) + if proto.HasExtension(f.Options, options.E_TsPackage) { + r.TSPackages[fileData.TSFileName] = proto.GetExtension(f.Options, options.E_TsPackage).(string) + } // analyse enums for _, enum := range f.EnumType { diff --git a/registry/registry.go b/registry/registry.go index eb279d7..fdb05a9 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -54,6 +54,9 @@ type Registry struct { // UseProtoNames will cause the generator to generate field name the same as defined in the proto UseProtoNames bool + + // TSPackages stores the package name keyed by the TS file name + TSPackages map[string]string } // NewRegistry initialise the registry and return the instance @@ -87,6 +90,7 @@ func NewRegistry(paramsMap map[string]string) (*Registry, error) { FetchModuleDirectory: fetchModuleDirectory, FetchModuleFilename: fetchModuleFilename, UseProtoNames: useProtoNames, + TSPackages: make(map[string]string), } return r, nil @@ -339,32 +343,37 @@ func (r *Registry) collectExternalDependenciesFromData(filesData map[string]*dat base := fileData.TSFileName target := data.GetTSFileName(typeInfo.File) sourceFile := "" - foundAtRoot, alias, err := r.findRootAliasForPath(func(absRoot string) (bool, error) { - completePath := filepath.Join(absRoot, typeInfo.File) - _, err := os.Stat(completePath) - if err != nil { - if os.IsNotExist(err) { - return false, nil + if pkg, ok := r.TSPackages[target]; ok { + log.Debugf("package import override %s has been found for file %s", pkg, target) + sourceFile = pkg + } else { + foundAtRoot, alias, err := r.findRootAliasForPath(func(absRoot string) (bool, error) { + completePath := filepath.Join(absRoot, typeInfo.File) + _, err := os.Stat(completePath) + if err != nil { + if os.IsNotExist(err) { + return false, nil + } + + return false, err + + } else { + return true, nil } - return false, err - - } else { - return true, nil + }) + if err != nil { + return errors.WithStack(err) } - }) - if err != nil { - return errors.WithStack(err) - } - - if foundAtRoot != "" { - target = filepath.Join(foundAtRoot, target) - } + if foundAtRoot != "" { + target = filepath.Join(foundAtRoot, target) + } - sourceFile, err = r.getSourceFileForImport(base, target, foundAtRoot, alias) - if err != nil { - return errors.Wrap(err, "error getting source file for import") + sourceFile, err = r.getSourceFileForImport(base, target, foundAtRoot, alias) + if err != nil { + return errors.Wrap(err, "error getting source file for import") + } } dependencies[identifier] = &data.Dependency{ ModuleIdentifier: data.GetModuleName(typeInfo.Package, typeInfo.File),