From 0d9819266ec74c1f8437112621cc5ddd58051b62 Mon Sep 17 00:00:00 2001 From: nametake Date: Sat, 20 Nov 2021 13:06:52 +0900 Subject: [PATCH 01/12] Added known types GoImportPath --- gengohttp.go | 20 +++++++++++++++----- go.mod | 4 +--- go.sum | 9 --------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/gengohttp.go b/gengohttp.go index 61aa146..ae35ec3 100644 --- a/gengohttp.go +++ b/gengohttp.go @@ -23,11 +23,21 @@ var ( ) var ( - protoPackage = protogen.GoImportPath("google.golang.org/protobuf/proto") - protojsonPackage = protogen.GoImportPath("google.golang.org/protobuf/encoding/protojson") - grpcPackage = protogen.GoImportPath("google.golang.org/grpc") - codesPackage = protogen.GoImportPath("google.golang.org/grpc/codes") - statusPackage = protogen.GoImportPath("google.golang.org/grpc/status") + protoPackage = protogen.GoImportPath("google.golang.org/protobuf/proto") + protojsonPackage = protogen.GoImportPath("google.golang.org/protobuf/encoding/protojson") + grpcPackage = protogen.GoImportPath("google.golang.org/grpc") + codesPackage = protogen.GoImportPath("google.golang.org/grpc/codes") + statusPackage = protogen.GoImportPath("google.golang.org/grpc/status") + anypbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known") + apipbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known") + durationpbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known") + emptypbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known") + fieldmaskpbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known") + sourcecontextpbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known") + structpbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known") + timestamppbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known") + typepbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known") + wrapperspbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known") ) func GenerateFile(gen *protogen.Plugin, file *protogen.File) (*protogen.GeneratedFile, error) { diff --git a/go.mod b/go.mod index d173e27..6f0e9d1 100644 --- a/go.mod +++ b/go.mod @@ -2,10 +2,8 @@ module github.com/nametake/protoc-gen-gohttp require ( github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b - github.com/golang/protobuf v1.4.2 - github.com/google/go-cmp v0.5.0 + github.com/golang/protobuf v1.4.2 // indirect google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 - google.golang.org/grpc v1.27.0 google.golang.org/protobuf v1.25.0 ) diff --git a/go.sum b/go.sum index 539731a..b3818c5 100644 --- a/go.sum +++ b/go.sum @@ -7,7 +7,6 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7 github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= -github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= @@ -19,10 +18,8 @@ github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QD github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0= github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= -github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= @@ -35,16 +32,13 @@ golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHl golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= -golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= @@ -55,13 +49,11 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= -google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 h1:gSJIx1SDwno+2ElGhA4+qG2zF97qiUzTM+rQ0klBOcE= google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 h1:+kGHl1aib/qcwaRi1CbqBZ1rk19r85MNUf8HaBghugY= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= -google.golang.org/grpc v1.27.0 h1:rRYRFMVgRv6E0D70Skyfsr28tDXIuuPZyWGMPdMcnXg= google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= @@ -69,7 +61,6 @@ google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQ google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c= From 9b1bf205043f5ec86d68ba342c2e83a71e2a0ebf Mon Sep 17 00:00:00 2001 From: nametake Date: Sat, 20 Nov 2021 13:20:26 +0900 Subject: [PATCH 02/12] Added any.proto --- Makefile | 4 +- _examples/google/api/annotations.proto | 2 +- _examples/google/api/http.proto | 3 +- _examples/google/protobuf/any.proto | 158 +++++++++++++++++++++++++ testdata/google/api/annotations.proto | 2 +- testdata/google/api/http.proto | 3 +- testdata/google/protobuf/any.proto | 158 +++++++++++++++++++++++++ 7 files changed, 323 insertions(+), 7 deletions(-) create mode 100644 _examples/google/protobuf/any.proto create mode 100644 testdata/google/protobuf/any.proto diff --git a/Makefile b/Makefile index 30dada6..b0e77a2 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,8 @@ run_examples: curl_google_api: @curl https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/annotations.proto > _examples/google/api/annotations.proto - @curl https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/http.proto > _examples/google/api/http.proto @curl https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/annotations.proto > testdata/google/api/annotations.proto + @curl https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/http.proto > _examples/google/api/http.proto @curl https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/http.proto > testdata/google/api/http.proto + @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/any.proto> _examples/google/protobuf/any.proto + @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/any.proto> testdata/google/protobuf/any.proto diff --git a/_examples/google/api/annotations.proto b/_examples/google/api/annotations.proto index 85c361b..efdab3d 100644 --- a/_examples/google/api/annotations.proto +++ b/_examples/google/api/annotations.proto @@ -1,4 +1,4 @@ -// Copyright (c) 2015, Google Inc. +// Copyright 2015 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/_examples/google/api/http.proto b/_examples/google/api/http.proto index b2977f5..113fa93 100644 --- a/_examples/google/api/http.proto +++ b/_examples/google/api/http.proto @@ -1,4 +1,4 @@ -// Copyright 2019 Google LLC. +// Copyright 2015 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -11,7 +11,6 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -// syntax = "proto3"; diff --git a/_examples/google/protobuf/any.proto b/_examples/google/protobuf/any.proto new file mode 100644 index 0000000..e2c2042 --- /dev/null +++ b/_examples/google/protobuf/any.proto @@ -0,0 +1,158 @@ +// 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/anypb"; +option java_package = "com.google.protobuf"; +option java_outer_classname = "AnyProto"; +option java_multiple_files = true; +option objc_class_prefix = "GPB"; + +// `Any` contains an arbitrary serialized protocol buffer message along with a +// URL that describes the type of the serialized message. +// +// Protobuf library provides support to pack/unpack Any values in the form +// of utility functions or additional generated methods of the Any type. +// +// Example 1: Pack and unpack a message in C++. +// +// Foo foo = ...; +// Any any; +// any.PackFrom(foo); +// ... +// if (any.UnpackTo(&foo)) { +// ... +// } +// +// Example 2: Pack and unpack a message in Java. +// +// Foo foo = ...; +// Any any = Any.pack(foo); +// ... +// if (any.is(Foo.class)) { +// foo = any.unpack(Foo.class); +// } +// +// Example 3: Pack and unpack a message in Python. +// +// foo = Foo(...) +// any = Any() +// any.Pack(foo) +// ... +// if any.Is(Foo.DESCRIPTOR): +// any.Unpack(foo) +// ... +// +// Example 4: Pack and unpack a message in Go +// +// foo := &pb.Foo{...} +// any, err := anypb.New(foo) +// if err != nil { +// ... +// } +// ... +// foo := &pb.Foo{} +// if err := any.UnmarshalTo(foo); err != nil { +// ... +// } +// +// The pack methods provided by protobuf library will by default use +// 'type.googleapis.com/full.type.name' as the type URL and the unpack +// methods only use the fully qualified type name after the last '/' +// in the type URL, for example "foo.bar.com/x/y.z" will yield type +// name "y.z". +// +// +// JSON +// +// The JSON representation of an `Any` value uses the regular +// representation of the deserialized, embedded message, with an +// additional field `@type` which contains the type URL. Example: +// +// package google.profile; +// message Person { +// string first_name = 1; +// string last_name = 2; +// } +// +// { +// "@type": "type.googleapis.com/google.profile.Person", +// "firstName": , +// "lastName": +// } +// +// If the embedded message type is well-known and has a custom JSON +// representation, that representation will be embedded adding a field +// `value` which holds the custom JSON in addition to the `@type` +// field. Example (for message [google.protobuf.Duration][]): +// +// { +// "@type": "type.googleapis.com/google.protobuf.Duration", +// "value": "1.212s" +// } +// +message Any { + // A URL/resource name that uniquely identifies the type of the serialized + // protocol buffer message. This string must contain at least + // one "/" character. The last segment of the URL's path must represent + // the fully qualified name of the type (as in + // `path/google.protobuf.Duration`). The name should be in a canonical form + // (e.g., leading "." is not accepted). + // + // In practice, teams usually precompile into the binary all types that they + // expect it to use in the context of Any. However, for URLs which use the + // scheme `http`, `https`, or no scheme, one can optionally set up a type + // server that maps type URLs to message definitions as follows: + // + // * If no scheme is provided, `https` is assumed. + // * An HTTP GET on the URL must yield a [google.protobuf.Type][] + // value in binary format, or produce an error. + // * Applications are allowed to cache lookup results based on the + // URL, or have them precompiled into a binary to avoid any + // lookup. Therefore, binary compatibility needs to be preserved + // on changes to types. (Use versioned type names to manage + // breaking changes.) + // + // Note: this functionality is not currently available in the official + // protobuf release, and it is not used for type URLs beginning with + // type.googleapis.com. + // + // Schemes other than `http`, `https` (or the empty scheme) might be + // used with implementation specific semantics. + // + string type_url = 1; + + // Must be a valid serialized protocol buffer of the above specified type. + bytes value = 2; +} diff --git a/testdata/google/api/annotations.proto b/testdata/google/api/annotations.proto index 85c361b..efdab3d 100644 --- a/testdata/google/api/annotations.proto +++ b/testdata/google/api/annotations.proto @@ -1,4 +1,4 @@ -// Copyright (c) 2015, Google Inc. +// Copyright 2015 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/testdata/google/api/http.proto b/testdata/google/api/http.proto index b2977f5..113fa93 100644 --- a/testdata/google/api/http.proto +++ b/testdata/google/api/http.proto @@ -1,4 +1,4 @@ -// Copyright 2019 Google LLC. +// Copyright 2015 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -11,7 +11,6 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -// syntax = "proto3"; diff --git a/testdata/google/protobuf/any.proto b/testdata/google/protobuf/any.proto new file mode 100644 index 0000000..e2c2042 --- /dev/null +++ b/testdata/google/protobuf/any.proto @@ -0,0 +1,158 @@ +// 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/anypb"; +option java_package = "com.google.protobuf"; +option java_outer_classname = "AnyProto"; +option java_multiple_files = true; +option objc_class_prefix = "GPB"; + +// `Any` contains an arbitrary serialized protocol buffer message along with a +// URL that describes the type of the serialized message. +// +// Protobuf library provides support to pack/unpack Any values in the form +// of utility functions or additional generated methods of the Any type. +// +// Example 1: Pack and unpack a message in C++. +// +// Foo foo = ...; +// Any any; +// any.PackFrom(foo); +// ... +// if (any.UnpackTo(&foo)) { +// ... +// } +// +// Example 2: Pack and unpack a message in Java. +// +// Foo foo = ...; +// Any any = Any.pack(foo); +// ... +// if (any.is(Foo.class)) { +// foo = any.unpack(Foo.class); +// } +// +// Example 3: Pack and unpack a message in Python. +// +// foo = Foo(...) +// any = Any() +// any.Pack(foo) +// ... +// if any.Is(Foo.DESCRIPTOR): +// any.Unpack(foo) +// ... +// +// Example 4: Pack and unpack a message in Go +// +// foo := &pb.Foo{...} +// any, err := anypb.New(foo) +// if err != nil { +// ... +// } +// ... +// foo := &pb.Foo{} +// if err := any.UnmarshalTo(foo); err != nil { +// ... +// } +// +// The pack methods provided by protobuf library will by default use +// 'type.googleapis.com/full.type.name' as the type URL and the unpack +// methods only use the fully qualified type name after the last '/' +// in the type URL, for example "foo.bar.com/x/y.z" will yield type +// name "y.z". +// +// +// JSON +// +// The JSON representation of an `Any` value uses the regular +// representation of the deserialized, embedded message, with an +// additional field `@type` which contains the type URL. Example: +// +// package google.profile; +// message Person { +// string first_name = 1; +// string last_name = 2; +// } +// +// { +// "@type": "type.googleapis.com/google.profile.Person", +// "firstName": , +// "lastName": +// } +// +// If the embedded message type is well-known and has a custom JSON +// representation, that representation will be embedded adding a field +// `value` which holds the custom JSON in addition to the `@type` +// field. Example (for message [google.protobuf.Duration][]): +// +// { +// "@type": "type.googleapis.com/google.protobuf.Duration", +// "value": "1.212s" +// } +// +message Any { + // A URL/resource name that uniquely identifies the type of the serialized + // protocol buffer message. This string must contain at least + // one "/" character. The last segment of the URL's path must represent + // the fully qualified name of the type (as in + // `path/google.protobuf.Duration`). The name should be in a canonical form + // (e.g., leading "." is not accepted). + // + // In practice, teams usually precompile into the binary all types that they + // expect it to use in the context of Any. However, for URLs which use the + // scheme `http`, `https`, or no scheme, one can optionally set up a type + // server that maps type URLs to message definitions as follows: + // + // * If no scheme is provided, `https` is assumed. + // * An HTTP GET on the URL must yield a [google.protobuf.Type][] + // value in binary format, or produce an error. + // * Applications are allowed to cache lookup results based on the + // URL, or have them precompiled into a binary to avoid any + // lookup. Therefore, binary compatibility needs to be preserved + // on changes to types. (Use versioned type names to manage + // breaking changes.) + // + // Note: this functionality is not currently available in the official + // protobuf release, and it is not used for type URLs beginning with + // type.googleapis.com. + // + // Schemes other than `http`, `https` (or the empty scheme) might be + // used with implementation specific semantics. + // + string type_url = 1; + + // Must be a valid serialized protocol buffer of the above specified type. + bytes value = 2; +} From 6f324987547bc5fe19182a92b52b923bce060ed2 Mon Sep 17 00:00:00 2001 From: nametake Date: Sat, 20 Nov 2021 13:44:32 +0900 Subject: [PATCH 03/12] Added go mod to _examples --- Makefile | 9 ++-- _examples/go.mod | 18 +++++++ _examples/go.sum | 137 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 158 insertions(+), 6 deletions(-) create mode 100644 _examples/go.mod create mode 100644 _examples/go.sum diff --git a/Makefile b/Makefile index b0e77a2..075efc2 100644 --- a/Makefile +++ b/Makefile @@ -9,17 +9,14 @@ gen_examples: install gen_pb: @protoc --go_out=./testdata/ --gohttp_out=./testdata/ --go_opt=paths=source_relative -I testdata ./testdata/**/*.proto -test: - @go test ./... ./_examples/ - -test_gen: +test: test_examples @go test ./... test_examples: - @go test ./_examples/ + @cd _examples && go test run_examples: - @go run _examples/main.go _examples/greeter.pb.go _examples/greeter.http.go + @cd _examples && go run main.go greeter.pb.go greeter.http.go curl_google_api: @curl https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/annotations.proto > _examples/google/api/annotations.proto diff --git a/_examples/go.mod b/_examples/go.mod new file mode 100644 index 0000000..779e3fe --- /dev/null +++ b/_examples/go.mod @@ -0,0 +1,18 @@ +module github.com/nametake/protoc-gen-gohttp/_examples + +go 1.17 + +require ( + github.com/google/go-cmp v0.5.6 + google.golang.org/genproto v0.0.0-20211118181313-81c1377c94b1 + google.golang.org/grpc v1.42.0 + google.golang.org/protobuf v1.27.1 +) + +require ( + github.com/golang/protobuf v1.5.2 // indirect + golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect + golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect + golang.org/x/text v0.3.5 // indirect + golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect +) diff --git a/_examples/go.sum b/_examples/go.sum new file mode 100644 index 0000000..afdc220 --- /dev/null +++ b/_examples/go.sum @@ -0,0 +1,137 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= +github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= +github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= +github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= +github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4/go.mod h1:6pvJx4me5XPnfI9Z40ddWsdw2W/uZgQLFXToKeRcDiI= +github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= +github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= +github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= +github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= +github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= +github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.mod h1:hliV/p42l8fGbc6Y9bQ70uLwIvmJyVE5k4iMKlh8wCQ= +github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= +github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ= +golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= +google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/genproto v0.0.0-20211118181313-81c1377c94b1 h1:b9mVrqYfq3P4bCdaLg1qtBnPzUYgglsIdjZkL/fQVOE= +google.golang.org/genproto v0.0.0-20211118181313-81c1377c94b1/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0= +google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= +google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34= +google.golang.org/grpc v1.42.0 h1:XT2/MFpuPFsEX2fWh3YQtHkZ+WYZFQRfaUgLZYj/p6A= +google.golang.org/grpc v1.42.0/go.mod h1:k+4IHHFw41K8+bbowsex27ge2rCb65oeWqe4jJ590SU= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= +google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= From d762909796a73368811d9c4b46a4fe4494f53f43 Mon Sep 17 00:00:00 2001 From: nametake Date: Sun, 21 Nov 2021 09:50:22 +0900 Subject: [PATCH 04/12] Added known types testdata --- testdata/knowntypes/knowntypes.http.go | 183 +++++++++++++++++++++++++ testdata/knowntypes/knowntypes.proto | 12 ++ 2 files changed, 195 insertions(+) create mode 100644 testdata/knowntypes/knowntypes.http.go create mode 100644 testdata/knowntypes/knowntypes.proto diff --git a/testdata/knowntypes/knowntypes.http.go b/testdata/knowntypes/knowntypes.http.go new file mode 100644 index 0000000..3151487 --- /dev/null +++ b/testdata/knowntypes/knowntypes.http.go @@ -0,0 +1,183 @@ +// Code generated by protoc-gen-gohttp. DO NOT EDIT. +// source: knowntypes/knowntypes.proto + +package knowntypespb + +import ( + bytes "bytes" + context "context" + fmt "fmt" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" + protojson "google.golang.org/protobuf/encoding/protojson" + proto "google.golang.org/protobuf/proto" + io "io" + ioutil "io/ioutil" + mime "mime" + http "net/http" + strings "strings" + anypb "google.golang.org/protobuf/types/known/anypb" +) + +// KnownTypesServiceHTTPService is the server API for KnownTypesService service. +type KnownTypesServiceHTTPService interface { + Any(context.Context, *anypb.Any) (*anypb.Any, error) +} + +// KnownTypesServiceHTTPConverter has a function to convert KnownTypesServiceHTTPService interface to http.HandlerFunc. +type KnownTypesServiceHTTPConverter struct { + srv KnownTypesServiceHTTPService +} + +// NewKnownTypesServiceHTTPConverter returns KnownTypesServiceHTTPConverter. +func NewKnownTypesServiceHTTPConverter(srv KnownTypesServiceHTTPService) *KnownTypesServiceHTTPConverter { + return &KnownTypesServiceHTTPConverter{ + srv: srv, + } +} + +// Any returns KnownTypesServiceHTTPService interface's Any converted to http.HandlerFunc. +func (h *KnownTypesServiceHTTPConverter) Any(cb func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error), interceptors ...grpc.UnaryServerInterceptor) http.HandlerFunc { + if cb == nil { + cb = func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error) { + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + p := status.New(codes.Unknown, err.Error()).Proto() + switch contentType, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")); contentType { + case "application/protobuf", "application/x-protobuf": + buf, err := proto.Marshal(p) + if err != nil { + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + return + } + case "application/json": + buf, err := protojson.Marshal(p) + if err != nil { + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + return + } + default: + } + } + } + } + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + contentType, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")) + + accepts := strings.Split(r.Header.Get("Accept"), ",") + accept := accepts[0] + if accept == "*/*" || accept == "" { + if contentType != "" { + accept = contentType + } else { + accept = "application/json" + } + } + + w.Header().Set("Content-Type", accept) + + arg := &anypb.Any{} + if r.Method != http.MethodGet { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + + switch contentType { + case "application/protobuf", "application/x-protobuf": + if err := proto.Unmarshal(body, arg); err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + case "application/json": + if err := protojson.Unmarshal(body, arg); err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + default: + w.WriteHeader(http.StatusUnsupportedMediaType) + _, err := fmt.Fprintf(w, "Unsupported Content-Type: %s", contentType) + cb(ctx, w, r, nil, nil, err) + return + } + } + + n := len(interceptors) + chained := func(ctx context.Context, arg interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { + chainer := func(currentInter grpc.UnaryServerInterceptor, currentHandler grpc.UnaryHandler) grpc.UnaryHandler { + return func(currentCtx context.Context, currentReq interface{}) (interface{}, error) { + return currentInter(currentCtx, currentReq, info, currentHandler) + } + } + + chainedHandler := handler + for i := n - 1; i >= 0; i-- { + chainedHandler = chainer(interceptors[i], chainedHandler) + } + return chainedHandler(ctx, arg) + } + + info := &grpc.UnaryServerInfo{ + Server: h.srv, + FullMethod: "/knowntypes.KnownTypesService/Any", + } + + handler := func(c context.Context, req interface{}) (interface{}, error) { + return h.srv.Any(c, req.(*anypb.Any)) + } + + iret, err := chained(ctx, arg, info, handler) + if err != nil { + cb(ctx, w, r, arg, nil, err) + return + } + + ret, ok := iret.(*anypb.Any) + if !ok { + cb(ctx, w, r, arg, nil, fmt.Errorf("/knowntypes.KnownTypesService/Any: interceptors have not return anypb.Any")) + return + } + + switch accept { + case "application/protobuf", "application/x-protobuf": + buf, err := proto.Marshal(ret) + if err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + case "application/json": + buf, err := protojson.Marshal(ret) + if err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + default: + w.WriteHeader(http.StatusUnsupportedMediaType) + _, err := fmt.Fprintf(w, "Unsupported Accept: %s", accept) + cb(ctx, w, r, arg, ret, err) + return + } + cb(ctx, w, r, arg, ret, nil) + }) +} + +// AnyWithName returns Service name, Method name and KnownTypesServiceHTTPService interface's Any converted to http.HandlerFunc. +func (h *KnownTypesServiceHTTPConverter) AnyWithName(cb func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error), interceptors ...grpc.UnaryServerInterceptor) (string, string, http.HandlerFunc) { + return "KnownTypesService", "Any", h.Any(cb, interceptors...) +} diff --git a/testdata/knowntypes/knowntypes.proto b/testdata/knowntypes/knowntypes.proto new file mode 100644 index 0000000..89997b7 --- /dev/null +++ b/testdata/knowntypes/knowntypes.proto @@ -0,0 +1,12 @@ +syntax = "proto3"; + +package knowntypes; + +option go_package = "./knowntypes/;knowntypespb"; + +import "google/protobuf/any.proto"; + +service KnownTypesService { + rpc Any(google.protobuf.Any) returns (google.protobuf.Any); +} + From 593b6ceef769618413abc898f2d0c7576b7312d3 Mon Sep 17 00:00:00 2001 From: nametake Date: Sun, 21 Nov 2021 11:29:15 +0900 Subject: [PATCH 05/12] Supported anypb --- gengohttp.go | 48 +++++++++++++------------- testdata/knowntypes/knowntypes.http.go | 2 +- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/gengohttp.go b/gengohttp.go index ae35ec3..f0da80b 100644 --- a/gengohttp.go +++ b/gengohttp.go @@ -23,21 +23,12 @@ var ( ) var ( - protoPackage = protogen.GoImportPath("google.golang.org/protobuf/proto") - protojsonPackage = protogen.GoImportPath("google.golang.org/protobuf/encoding/protojson") - grpcPackage = protogen.GoImportPath("google.golang.org/grpc") - codesPackage = protogen.GoImportPath("google.golang.org/grpc/codes") - statusPackage = protogen.GoImportPath("google.golang.org/grpc/status") - anypbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known") - apipbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known") - durationpbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known") - emptypbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known") - fieldmaskpbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known") - sourcecontextpbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known") - structpbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known") - timestamppbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known") - typepbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known") - wrapperspbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known") + protoPackage = protogen.GoImportPath("google.golang.org/protobuf/proto") + protojsonPackage = protogen.GoImportPath("google.golang.org/protobuf/encoding/protojson") + grpcPackage = protogen.GoImportPath("google.golang.org/grpc") + codesPackage = protogen.GoImportPath("google.golang.org/grpc/codes") + statusPackage = protogen.GoImportPath("google.golang.org/grpc/status") + anypbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known/anypb") ) func GenerateFile(gen *protogen.Plugin, file *protogen.File) (*protogen.GeneratedFile, error) { @@ -148,7 +139,7 @@ func genServiceInterface(g *protogen.GeneratedFile, srv *protogen.Service) { if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() { continue } - g.P(method.Comments.Leading, method.GoName, "(", contextPackage.Ident("Context"), ", *", method.Input.GoIdent.GoName, ") (*", method.Output.GoIdent.GoName, ", error)") + g.P(method.Comments.Leading, method.GoName, "(", contextPackage.Ident("Context"), ", *", genMessageName(method.Input), ") (*", genMessageName(method.Output), ", error)") } g.P("}") } @@ -193,7 +184,7 @@ func genMethod(g *protogen.GeneratedFile, method *protogen.Method) { g.P("") g.P(" w.Header().Set(\"Content-Type\", accept)") g.P("") - g.P(" arg := &", method.Input.GoIdent.GoName, "{}") + g.P(" arg := &", genMessageName(method.Input), "{}") g.P(" if r.Method != ", httpPackage.Ident("MethodGet"), " {") g.P(" body, err := ", ioutilPackage.Ident("ReadAll"), "(r.Body)") g.P(" if err != nil {") @@ -241,7 +232,7 @@ func genMethod(g *protogen.GeneratedFile, method *protogen.Method) { g.P(" }") g.P("") g.P(" handler := func(c ", contextPackage.Ident("Context"), ", req interface{}) (interface{}, error) {") - g.P(" return h.srv.", method.GoName, "(c, req.(*", method.Input.GoIdent.GoName, "))") + g.P(" return h.srv.", method.GoName, "(c, req.(*", genMessageName(method.Input), "))") g.P(" }") g.P("") g.P(" iret, err := chained(ctx, arg, info, handler)") @@ -250,9 +241,9 @@ func genMethod(g *protogen.GeneratedFile, method *protogen.Method) { g.P(" return") g.P(" }") g.P("") - g.P(" ret, ok := iret.(*", method.Output.GoIdent.GoName, ")") + g.P(" ret, ok := iret.(*", genMessageName(method.Output), ")") g.P(" if !ok {") - g.P(" cb(ctx, w, r, arg, nil, fmt.Errorf(\"/", method.Desc.ParentFile().Package(), ".", method.Parent.GoName, "/", method.GoName, ": interceptors have not return ", method.Output.GoIdent.GoName, "\"))") + g.P(" cb(ctx, w, r, arg, nil, fmt.Errorf(\"/", method.Desc.ParentFile().Package(), ".", method.Parent.GoName, "/", method.GoName, ": interceptors have not return ", genMessageName(method.Output), "\"))") g.P(" return") g.P(" }") g.P("") @@ -364,7 +355,7 @@ func genMethodHTTPRule(g *protogen.GeneratedFile, method *protogen.Method) error g.P("") g.P(" w.Header().Set(\"Content-Type\", accept)") g.P("") - g.P(" arg := &", method.Input.GoIdent.GoName, "{}") + g.P(" arg := &", genMessageName(method.Input), "{}") if _, ok := httpRule.GetPattern().(*annotations.HttpRule_Get); ok { g.P("if r.Method == http.MethodGet {") for _, p := range queryParams { @@ -440,7 +431,7 @@ func genMethodHTTPRule(g *protogen.GeneratedFile, method *protogen.Method) error g.P(" }") g.P("") g.P(" handler := func(c ", contextPackage.Ident("Context"), ", req interface{}) (interface{}, error) {") - g.P(" return h.srv.", method.GoName, "(c, req.(*", method.Input.GoIdent.GoName, "))") + g.P(" return h.srv.", method.GoName, "(c, req.(*", genMessageName(method.Input), "))") g.P(" }") g.P("") g.P(" iret, err := chained(ctx, arg, info, handler)") @@ -449,9 +440,9 @@ func genMethodHTTPRule(g *protogen.GeneratedFile, method *protogen.Method) error g.P(" return") g.P(" }") g.P("") - g.P(" ret, ok := iret.(*", method.Output.GoIdent.GoName, ")") + g.P(" ret, ok := iret.(*", genMessageName(method.Output), ")") g.P(" if !ok {") - g.P(" cb(ctx, w, r, arg, nil, fmt.Errorf(\"/", method.Desc.ParentFile().Package(), ".", method.Parent.GoName, "/", method.GoName, ": interceptors have not return ", method.Output.GoIdent.GoName, "\"))") + g.P(" cb(ctx, w, r, arg, nil, fmt.Errorf(\"/", method.Desc.ParentFile().Package(), ".", method.Parent.GoName, "/", method.GoName, ": interceptors have not return ", genMessageName(method.Output), "\"))") g.P(" return") g.P(" }") g.P("") @@ -699,3 +690,12 @@ func genQueryString(g *protogen.GeneratedFile, queryParam *queryParam) { } } } + +func genMessageName(msg *protogen.Message) protogen.GoIdent { + switch msg.Location.SourceFile { + case "google/protobuf/any.proto": + return anypbPackage.Ident(msg.GoIdent.GoName) + default: + return msg.GoIdent + } +} diff --git a/testdata/knowntypes/knowntypes.http.go b/testdata/knowntypes/knowntypes.http.go index 3151487..9cf25b6 100644 --- a/testdata/knowntypes/knowntypes.http.go +++ b/testdata/knowntypes/knowntypes.http.go @@ -12,12 +12,12 @@ import ( status "google.golang.org/grpc/status" protojson "google.golang.org/protobuf/encoding/protojson" proto "google.golang.org/protobuf/proto" + anypb "google.golang.org/protobuf/types/known/anypb" io "io" ioutil "io/ioutil" mime "mime" http "net/http" strings "strings" - anypb "google.golang.org/protobuf/types/known/anypb" ) // KnownTypesServiceHTTPService is the server API for KnownTypesService service. From 82f28bbbd6b56cd487795584cc49494799994f18 Mon Sep 17 00:00:00 2001 From: nametake Date: Mon, 22 Nov 2021 00:59:56 +0900 Subject: [PATCH 06/12] Added others known types packages --- gengohttp.go | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/gengohttp.go b/gengohttp.go index f0da80b..26e88e1 100644 --- a/gengohttp.go +++ b/gengohttp.go @@ -23,12 +23,21 @@ var ( ) var ( - protoPackage = protogen.GoImportPath("google.golang.org/protobuf/proto") - protojsonPackage = protogen.GoImportPath("google.golang.org/protobuf/encoding/protojson") - grpcPackage = protogen.GoImportPath("google.golang.org/grpc") - codesPackage = protogen.GoImportPath("google.golang.org/grpc/codes") - statusPackage = protogen.GoImportPath("google.golang.org/grpc/status") - anypbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known/anypb") + protoPackage = protogen.GoImportPath("google.golang.org/protobuf/proto") + protojsonPackage = protogen.GoImportPath("google.golang.org/protobuf/encoding/protojson") + grpcPackage = protogen.GoImportPath("google.golang.org/grpc") + codesPackage = protogen.GoImportPath("google.golang.org/grpc/codes") + statusPackage = protogen.GoImportPath("google.golang.org/grpc/status") + anypbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known/anypb") + apipbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known/apipb") + durationpbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known/durationpb") + emptypbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known/emptypb") + fieldmaskpbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known/fieldmaskpb") + sourcecontextpbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known/sourcecontextpb") + structpbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known/structpb") + timestamppbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known/timestamppb") + typepbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known/typepb") + wrapperspbPackage = protogen.GoImportPath("google.golang.org/protobuf/types/known/wrapperspb") ) func GenerateFile(gen *protogen.Plugin, file *protogen.File) (*protogen.GeneratedFile, error) { @@ -695,6 +704,24 @@ func genMessageName(msg *protogen.Message) protogen.GoIdent { switch msg.Location.SourceFile { case "google/protobuf/any.proto": return anypbPackage.Ident(msg.GoIdent.GoName) + case "google/protobuf/api.proto": + return apipbPackage.Ident(msg.GoIdent.GoName) + case "google/protobuf/duration.proto": + return durationpbPackage.Ident(msg.GoIdent.GoName) + case "google/protobuf/empty.proto": + return emptypbPackage.Ident(msg.GoIdent.GoName) + case "google/protobuf/fieldmask.proto": + return fieldmaskpbPackage.Ident(msg.GoIdent.GoName) + case "google/protobuf/sourcecontext.proto": + return sourcecontextpbPackage.Ident(msg.GoIdent.GoName) + case "google/protobuf/struct.proto": + return statusPackage.Ident(msg.GoIdent.GoName) + case "google/protobuf/timestamp.proto": + return timestamppbPackage.Ident(msg.GoIdent.GoName) + case "google/protobuf/type.proto": + return typepbPackage.Ident(msg.GoIdent.GoName) + case "google/protobuf/wrappers.proto": + return wrapperspbPackage.Ident(msg.GoIdent.GoName) default: return msg.GoIdent } From 3aafd0cd9d7f82f0a08ceba2d2fcef066219ab77 Mon Sep 17 00:00:00 2001 From: nametake Date: Tue, 23 Nov 2021 12:33:04 +0900 Subject: [PATCH 07/12] Added google/protobuf/*.proto --- Makefile | 20 +- _examples/google/protobuf/api.proto | 208 +++++++++++++++ _examples/google/protobuf/duration.proto | 116 +++++++++ _examples/google/protobuf/empty.proto | 52 ++++ _examples/google/protobuf/field_mask.proto | 245 ++++++++++++++++++ _examples/google/protobuf/sourcecontext.proto | 48 ++++ _examples/google/protobuf/struct.proto | 95 +++++++ _examples/google/protobuf/timestamp.proto | 147 +++++++++++ _examples/google/protobuf/type.proto | 187 +++++++++++++ _examples/google/protobuf/wrappers.proto | 123 +++++++++ testdata/google/protobuf/api.proto | 208 +++++++++++++++ testdata/google/protobuf/duration.proto | 116 +++++++++ testdata/google/protobuf/empty.proto | 52 ++++ testdata/google/protobuf/field_mask.proto | 245 ++++++++++++++++++ testdata/google/protobuf/sourcecontext.proto | 48 ++++ testdata/google/protobuf/struct.proto | 95 +++++++ testdata/google/protobuf/timestamp.proto | 147 +++++++++++ testdata/google/protobuf/type.proto | 187 +++++++++++++ testdata/google/protobuf/wrappers.proto | 123 +++++++++ 19 files changed, 2461 insertions(+), 1 deletion(-) create mode 100644 _examples/google/protobuf/api.proto create mode 100644 _examples/google/protobuf/duration.proto create mode 100644 _examples/google/protobuf/empty.proto create mode 100644 _examples/google/protobuf/field_mask.proto create mode 100644 _examples/google/protobuf/sourcecontext.proto create mode 100644 _examples/google/protobuf/struct.proto create mode 100644 _examples/google/protobuf/timestamp.proto create mode 100644 _examples/google/protobuf/type.proto create mode 100644 _examples/google/protobuf/wrappers.proto create mode 100644 testdata/google/protobuf/api.proto create mode 100644 testdata/google/protobuf/duration.proto create mode 100644 testdata/google/protobuf/empty.proto create mode 100644 testdata/google/protobuf/field_mask.proto create mode 100644 testdata/google/protobuf/sourcecontext.proto create mode 100644 testdata/google/protobuf/struct.proto create mode 100644 testdata/google/protobuf/timestamp.proto create mode 100644 testdata/google/protobuf/type.proto create mode 100644 testdata/google/protobuf/wrappers.proto diff --git a/Makefile b/Makefile index 075efc2..3523bef 100644 --- a/Makefile +++ b/Makefile @@ -18,10 +18,28 @@ test_examples: run_examples: @cd _examples && go run main.go greeter.pb.go greeter.http.go -curl_google_api: +curl_google_option_proto: @curl https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/annotations.proto > _examples/google/api/annotations.proto @curl https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/annotations.proto > testdata/google/api/annotations.proto @curl https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/http.proto > _examples/google/api/http.proto @curl https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/http.proto > testdata/google/api/http.proto @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/any.proto> _examples/google/protobuf/any.proto @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/any.proto> testdata/google/protobuf/any.proto + @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/api.proto> _examples/google/protobuf/api.proto + @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/api.proto> testdata/google/protobuf/api.proto + @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/duration.proto> _examples/google/protobuf/duration.proto + @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/duration.proto> testdata/google/protobuf/duration.proto + @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/empty.proto> _examples/google/protobuf/empty.proto + @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/empty.proto> testdata/google/protobuf/empty.proto + @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/field_mask.proto> _examples/google/protobuf/field_mask.proto + @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/field_mask.proto> testdata/google/protobuf/field_mask.proto + @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/source_context.proto> _examples/google/protobuf/sourcecontext.proto + @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/source_context.proto> testdata/google/protobuf/sourcecontext.proto + @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/struct.proto> _examples/google/protobuf/struct.proto + @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/struct.proto> testdata/google/protobuf/struct.proto + @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/timestamp.proto> _examples/google/protobuf/timestamp.proto + @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/timestamp.proto> testdata/google/protobuf/timestamp.proto + @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/type.proto> _examples/google/protobuf/type.proto + @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/type.proto> testdata/google/protobuf/type.proto + @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/wrappers.proto> _examples/google/protobuf/wrappers.proto + @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/wrappers.proto> testdata/google/protobuf/wrappers.proto diff --git a/_examples/google/protobuf/api.proto b/_examples/google/protobuf/api.proto new file mode 100644 index 0000000..3d598fc --- /dev/null +++ b/_examples/google/protobuf/api.proto @@ -0,0 +1,208 @@ +// 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; + +import "google/protobuf/source_context.proto"; +import "google/protobuf/type.proto"; + +option csharp_namespace = "Google.Protobuf.WellKnownTypes"; +option java_package = "com.google.protobuf"; +option java_outer_classname = "ApiProto"; +option java_multiple_files = true; +option objc_class_prefix = "GPB"; +option go_package = "google.golang.org/protobuf/types/known/apipb"; + +// Api is a light-weight descriptor for an API Interface. +// +// Interfaces are also described as "protocol buffer services" in some contexts, +// such as by the "service" keyword in a .proto file, but they are different +// from API Services, which represent a concrete implementation of an interface +// as opposed to simply a description of methods and bindings. They are also +// sometimes simply referred to as "APIs" in other contexts, such as the name of +// this message itself. See https://cloud.google.com/apis/design/glossary for +// detailed terminology. +message Api { + // The fully qualified name of this interface, including package name + // followed by the interface's simple name. + string name = 1; + + // The methods of this interface, in unspecified order. + repeated Method methods = 2; + + // Any metadata attached to the interface. + repeated Option options = 3; + + // A version string for this interface. If specified, must have the form + // `major-version.minor-version`, as in `1.10`. If the minor version is + // omitted, it defaults to zero. If the entire version field is empty, the + // major version is derived from the package name, as outlined below. If the + // field is not empty, the version in the package name will be verified to be + // consistent with what is provided here. + // + // The versioning schema uses [semantic + // versioning](http://semver.org) where the major version number + // indicates a breaking change and the minor version an additive, + // non-breaking change. Both version numbers are signals to users + // what to expect from different versions, and should be carefully + // chosen based on the product plan. + // + // The major version is also reflected in the package name of the + // interface, which must end in `v`, as in + // `google.feature.v1`. For major versions 0 and 1, the suffix can + // be omitted. Zero major versions must only be used for + // experimental, non-GA interfaces. + // + // + string version = 4; + + // Source context for the protocol buffer service represented by this + // message. + SourceContext source_context = 5; + + // Included interfaces. See [Mixin][]. + repeated Mixin mixins = 6; + + // The source syntax of the service. + Syntax syntax = 7; +} + +// Method represents a method of an API interface. +message Method { + // The simple name of this method. + string name = 1; + + // A URL of the input message type. + string request_type_url = 2; + + // If true, the request is streamed. + bool request_streaming = 3; + + // The URL of the output message type. + string response_type_url = 4; + + // If true, the response is streamed. + bool response_streaming = 5; + + // Any metadata attached to the method. + repeated Option options = 6; + + // The source syntax of this method. + Syntax syntax = 7; +} + +// Declares an API Interface to be included in this interface. The including +// interface must redeclare all the methods from the included interface, but +// documentation and options are inherited as follows: +// +// - If after comment and whitespace stripping, the documentation +// string of the redeclared method is empty, it will be inherited +// from the original method. +// +// - Each annotation belonging to the service config (http, +// visibility) which is not set in the redeclared method will be +// inherited. +// +// - If an http annotation is inherited, the path pattern will be +// modified as follows. Any version prefix will be replaced by the +// version of the including interface plus the [root][] path if +// specified. +// +// Example of a simple mixin: +// +// package google.acl.v1; +// service AccessControl { +// // Get the underlying ACL object. +// rpc GetAcl(GetAclRequest) returns (Acl) { +// option (google.api.http).get = "/v1/{resource=**}:getAcl"; +// } +// } +// +// package google.storage.v2; +// service Storage { +// rpc GetAcl(GetAclRequest) returns (Acl); +// +// // Get a data record. +// rpc GetData(GetDataRequest) returns (Data) { +// option (google.api.http).get = "/v2/{resource=**}"; +// } +// } +// +// Example of a mixin configuration: +// +// apis: +// - name: google.storage.v2.Storage +// mixins: +// - name: google.acl.v1.AccessControl +// +// The mixin construct implies that all methods in `AccessControl` are +// also declared with same name and request/response types in +// `Storage`. A documentation generator or annotation processor will +// see the effective `Storage.GetAcl` method after inheriting +// documentation and annotations as follows: +// +// service Storage { +// // Get the underlying ACL object. +// rpc GetAcl(GetAclRequest) returns (Acl) { +// option (google.api.http).get = "/v2/{resource=**}:getAcl"; +// } +// ... +// } +// +// Note how the version in the path pattern changed from `v1` to `v2`. +// +// If the `root` field in the mixin is specified, it should be a +// relative path under which inherited HTTP paths are placed. Example: +// +// apis: +// - name: google.storage.v2.Storage +// mixins: +// - name: google.acl.v1.AccessControl +// root: acls +// +// This implies the following inherited HTTP annotation: +// +// service Storage { +// // Get the underlying ACL object. +// rpc GetAcl(GetAclRequest) returns (Acl) { +// option (google.api.http).get = "/v2/acls/{resource=**}:getAcl"; +// } +// ... +// } +message Mixin { + // The fully qualified name of the interface which is included. + string name = 1; + + // If non-empty specifies a path under which inherited HTTP paths + // are rooted. + string root = 2; +} diff --git a/_examples/google/protobuf/duration.proto b/_examples/google/protobuf/duration.proto new file mode 100644 index 0000000..81c3e36 --- /dev/null +++ b/_examples/google/protobuf/duration.proto @@ -0,0 +1,116 @@ +// 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 cc_enable_arenas = true; +option go_package = "google.golang.org/protobuf/types/known/durationpb"; +option java_package = "com.google.protobuf"; +option java_outer_classname = "DurationProto"; +option java_multiple_files = true; +option objc_class_prefix = "GPB"; + +// A Duration represents a signed, fixed-length span of time represented +// as a count of seconds and fractions of seconds at nanosecond +// resolution. It is independent of any calendar and concepts like "day" +// or "month". It is related to Timestamp in that the difference between +// two Timestamp values is a Duration and it can be added or subtracted +// from a Timestamp. Range is approximately +-10,000 years. +// +// # Examples +// +// Example 1: Compute Duration from two Timestamps in pseudo code. +// +// Timestamp start = ...; +// Timestamp end = ...; +// Duration duration = ...; +// +// duration.seconds = end.seconds - start.seconds; +// duration.nanos = end.nanos - start.nanos; +// +// if (duration.seconds < 0 && duration.nanos > 0) { +// duration.seconds += 1; +// duration.nanos -= 1000000000; +// } else if (duration.seconds > 0 && duration.nanos < 0) { +// duration.seconds -= 1; +// duration.nanos += 1000000000; +// } +// +// Example 2: Compute Timestamp from Timestamp + Duration in pseudo code. +// +// Timestamp start = ...; +// Duration duration = ...; +// Timestamp end = ...; +// +// end.seconds = start.seconds + duration.seconds; +// end.nanos = start.nanos + duration.nanos; +// +// if (end.nanos < 0) { +// end.seconds -= 1; +// end.nanos += 1000000000; +// } else if (end.nanos >= 1000000000) { +// end.seconds += 1; +// end.nanos -= 1000000000; +// } +// +// Example 3: Compute Duration from datetime.timedelta in Python. +// +// td = datetime.timedelta(days=3, minutes=10) +// duration = Duration() +// duration.FromTimedelta(td) +// +// # JSON Mapping +// +// In JSON format, the Duration type is encoded as a string rather than an +// object, where the string ends in the suffix "s" (indicating seconds) and +// is preceded by the number of seconds, with nanoseconds expressed as +// fractional seconds. For example, 3 seconds with 0 nanoseconds should be +// encoded in JSON format as "3s", while 3 seconds and 1 nanosecond should +// be expressed in JSON format as "3.000000001s", and 3 seconds and 1 +// microsecond should be expressed in JSON format as "3.000001s". +// +// +message Duration { + // Signed seconds of the span of time. Must be from -315,576,000,000 + // to +315,576,000,000 inclusive. Note: these bounds are computed from: + // 60 sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years + int64 seconds = 1; + + // Signed fractions of a second at nanosecond resolution of the span + // of time. Durations less than one second are represented with a 0 + // `seconds` field and a positive or negative `nanos` field. For durations + // of one second or more, a non-zero value for the `nanos` field must be + // of the same sign as the `seconds` field. Must be from -999,999,999 + // to +999,999,999 inclusive. + int32 nanos = 2; +} diff --git a/_examples/google/protobuf/empty.proto b/_examples/google/protobuf/empty.proto new file mode 100644 index 0000000..5f992de --- /dev/null +++ b/_examples/google/protobuf/empty.proto @@ -0,0 +1,52 @@ +// 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 {} diff --git a/_examples/google/protobuf/field_mask.proto b/_examples/google/protobuf/field_mask.proto new file mode 100644 index 0000000..6b5104f --- /dev/null +++ b/_examples/google/protobuf/field_mask.proto @@ -0,0 +1,245 @@ +// 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 java_package = "com.google.protobuf"; +option java_outer_classname = "FieldMaskProto"; +option java_multiple_files = true; +option objc_class_prefix = "GPB"; +option go_package = "google.golang.org/protobuf/types/known/fieldmaskpb"; +option cc_enable_arenas = true; + +// `FieldMask` represents a set of symbolic field paths, for example: +// +// paths: "f.a" +// paths: "f.b.d" +// +// Here `f` represents a field in some root message, `a` and `b` +// fields in the message found in `f`, and `d` a field found in the +// message in `f.b`. +// +// Field masks are used to specify a subset of fields that should be +// returned by a get operation or modified by an update operation. +// Field masks also have a custom JSON encoding (see below). +// +// # Field Masks in Projections +// +// When used in the context of a projection, a response message or +// sub-message is filtered by the API to only contain those fields as +// specified in the mask. For example, if the mask in the previous +// example is applied to a response message as follows: +// +// f { +// a : 22 +// b { +// d : 1 +// x : 2 +// } +// y : 13 +// } +// z: 8 +// +// The result will not contain specific values for fields x,y and z +// (their value will be set to the default, and omitted in proto text +// output): +// +// +// f { +// a : 22 +// b { +// d : 1 +// } +// } +// +// A repeated field is not allowed except at the last position of a +// paths string. +// +// If a FieldMask object is not present in a get operation, the +// operation applies to all fields (as if a FieldMask of all fields +// had been specified). +// +// Note that a field mask does not necessarily apply to the +// top-level response message. In case of a REST get operation, the +// field mask applies directly to the response, but in case of a REST +// list operation, the mask instead applies to each individual message +// in the returned resource list. In case of a REST custom method, +// other definitions may be used. Where the mask applies will be +// clearly documented together with its declaration in the API. In +// any case, the effect on the returned resource/resources is required +// behavior for APIs. +// +// # Field Masks in Update Operations +// +// A field mask in update operations specifies which fields of the +// targeted resource are going to be updated. The API is required +// to only change the values of the fields as specified in the mask +// and leave the others untouched. If a resource is passed in to +// describe the updated values, the API ignores the values of all +// fields not covered by the mask. +// +// If a repeated field is specified for an update operation, new values will +// be appended to the existing repeated field in the target resource. Note that +// a repeated field is only allowed in the last position of a `paths` string. +// +// If a sub-message is specified in the last position of the field mask for an +// update operation, then new value will be merged into the existing sub-message +// in the target resource. +// +// For example, given the target message: +// +// f { +// b { +// d: 1 +// x: 2 +// } +// c: [1] +// } +// +// And an update message: +// +// f { +// b { +// d: 10 +// } +// c: [2] +// } +// +// then if the field mask is: +// +// paths: ["f.b", "f.c"] +// +// then the result will be: +// +// f { +// b { +// d: 10 +// x: 2 +// } +// c: [1, 2] +// } +// +// An implementation may provide options to override this default behavior for +// repeated and message fields. +// +// In order to reset a field's value to the default, the field must +// be in the mask and set to the default value in the provided resource. +// Hence, in order to reset all fields of a resource, provide a default +// instance of the resource and set all fields in the mask, or do +// not provide a mask as described below. +// +// If a field mask is not present on update, the operation applies to +// all fields (as if a field mask of all fields has been specified). +// Note that in the presence of schema evolution, this may mean that +// fields the client does not know and has therefore not filled into +// the request will be reset to their default. If this is unwanted +// behavior, a specific service may require a client to always specify +// a field mask, producing an error if not. +// +// As with get operations, the location of the resource which +// describes the updated values in the request message depends on the +// operation kind. In any case, the effect of the field mask is +// required to be honored by the API. +// +// ## Considerations for HTTP REST +// +// The HTTP kind of an update operation which uses a field mask must +// be set to PATCH instead of PUT in order to satisfy HTTP semantics +// (PUT must only be used for full updates). +// +// # JSON Encoding of Field Masks +// +// In JSON, a field mask is encoded as a single string where paths are +// separated by a comma. Fields name in each path are converted +// to/from lower-camel naming conventions. +// +// As an example, consider the following message declarations: +// +// message Profile { +// User user = 1; +// Photo photo = 2; +// } +// message User { +// string display_name = 1; +// string address = 2; +// } +// +// In proto a field mask for `Profile` may look as such: +// +// mask { +// paths: "user.display_name" +// paths: "photo" +// } +// +// In JSON, the same mask is represented as below: +// +// { +// mask: "user.displayName,photo" +// } +// +// # Field Masks and Oneof Fields +// +// Field masks treat fields in oneofs just as regular fields. Consider the +// following message: +// +// message SampleMessage { +// oneof test_oneof { +// string name = 4; +// SubMessage sub_message = 9; +// } +// } +// +// The field mask can be: +// +// mask { +// paths: "name" +// } +// +// Or: +// +// mask { +// paths: "sub_message" +// } +// +// Note that oneof type names ("test_oneof" in this case) cannot be used in +// paths. +// +// ## Field Mask Verification +// +// The implementation of any API method which has a FieldMask type field in the +// request should verify the included field paths, and return an +// `INVALID_ARGUMENT` error if any path is unmappable. +message FieldMask { + // The set of field mask paths. + repeated string paths = 1; +} diff --git a/_examples/google/protobuf/sourcecontext.proto b/_examples/google/protobuf/sourcecontext.proto new file mode 100644 index 0000000..06bfc43 --- /dev/null +++ b/_examples/google/protobuf/sourcecontext.proto @@ -0,0 +1,48 @@ +// 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 java_package = "com.google.protobuf"; +option java_outer_classname = "SourceContextProto"; +option java_multiple_files = true; +option objc_class_prefix = "GPB"; +option go_package = "google.golang.org/protobuf/types/known/sourcecontextpb"; + +// `SourceContext` represents information about the source of a +// protobuf element, like the file in which it is defined. +message SourceContext { + // The path-qualified name of the .proto file that contained the associated + // protobuf element. For example: `"google/protobuf/source_context.proto"`. + string file_name = 1; +} diff --git a/_examples/google/protobuf/struct.proto b/_examples/google/protobuf/struct.proto new file mode 100644 index 0000000..0ac843c --- /dev/null +++ b/_examples/google/protobuf/struct.proto @@ -0,0 +1,95 @@ +// 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 cc_enable_arenas = true; +option go_package = "google.golang.org/protobuf/types/known/structpb"; +option java_package = "com.google.protobuf"; +option java_outer_classname = "StructProto"; +option java_multiple_files = true; +option objc_class_prefix = "GPB"; + +// `Struct` represents a structured data value, consisting of fields +// which map to dynamically typed values. In some languages, `Struct` +// might be supported by a native representation. For example, in +// scripting languages like JS a struct is represented as an +// object. The details of that representation are described together +// with the proto support for the language. +// +// The JSON representation for `Struct` is JSON object. +message Struct { + // Unordered map of dynamically typed values. + map fields = 1; +} + +// `Value` represents a dynamically typed value which can be either +// null, a number, a string, a boolean, a recursive struct value, or a +// list of values. A producer of value is expected to set one of these +// variants. Absence of any variant indicates an error. +// +// The JSON representation for `Value` is JSON value. +message Value { + // The kind of value. + oneof kind { + // Represents a null value. + NullValue null_value = 1; + // Represents a double value. + double number_value = 2; + // Represents a string value. + string string_value = 3; + // Represents a boolean value. + bool bool_value = 4; + // Represents a structured value. + Struct struct_value = 5; + // Represents a repeated `Value`. + ListValue list_value = 6; + } +} + +// `NullValue` is a singleton enumeration to represent the null value for the +// `Value` type union. +// +// The JSON representation for `NullValue` is JSON `null`. +enum NullValue { + // Null value. + NULL_VALUE = 0; +} + +// `ListValue` is a wrapper around a repeated field of values. +// +// The JSON representation for `ListValue` is JSON array. +message ListValue { + // Repeated field of dynamically typed values. + repeated Value values = 1; +} diff --git a/_examples/google/protobuf/timestamp.proto b/_examples/google/protobuf/timestamp.proto new file mode 100644 index 0000000..3b2df6d --- /dev/null +++ b/_examples/google/protobuf/timestamp.proto @@ -0,0 +1,147 @@ +// 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 cc_enable_arenas = true; +option go_package = "google.golang.org/protobuf/types/known/timestamppb"; +option java_package = "com.google.protobuf"; +option java_outer_classname = "TimestampProto"; +option java_multiple_files = true; +option objc_class_prefix = "GPB"; + +// A Timestamp represents a point in time independent of any time zone or local +// calendar, encoded as a count of seconds and fractions of seconds at +// nanosecond resolution. The count is relative to an epoch at UTC midnight on +// January 1, 1970, in the proleptic Gregorian calendar which extends the +// Gregorian calendar backwards to year one. +// +// All minutes are 60 seconds long. Leap seconds are "smeared" so that no leap +// second table is needed for interpretation, using a [24-hour linear +// smear](https://developers.google.com/time/smear). +// +// The range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. By +// restricting to that range, we ensure that we can convert to and from [RFC +// 3339](https://www.ietf.org/rfc/rfc3339.txt) date strings. +// +// # Examples +// +// Example 1: Compute Timestamp from POSIX `time()`. +// +// Timestamp timestamp; +// timestamp.set_seconds(time(NULL)); +// timestamp.set_nanos(0); +// +// Example 2: Compute Timestamp from POSIX `gettimeofday()`. +// +// struct timeval tv; +// gettimeofday(&tv, NULL); +// +// Timestamp timestamp; +// timestamp.set_seconds(tv.tv_sec); +// timestamp.set_nanos(tv.tv_usec * 1000); +// +// Example 3: Compute Timestamp from Win32 `GetSystemTimeAsFileTime()`. +// +// FILETIME ft; +// GetSystemTimeAsFileTime(&ft); +// UINT64 ticks = (((UINT64)ft.dwHighDateTime) << 32) | ft.dwLowDateTime; +// +// // A Windows tick is 100 nanoseconds. Windows epoch 1601-01-01T00:00:00Z +// // is 11644473600 seconds before Unix epoch 1970-01-01T00:00:00Z. +// Timestamp timestamp; +// timestamp.set_seconds((INT64) ((ticks / 10000000) - 11644473600LL)); +// timestamp.set_nanos((INT32) ((ticks % 10000000) * 100)); +// +// Example 4: Compute Timestamp from Java `System.currentTimeMillis()`. +// +// long millis = System.currentTimeMillis(); +// +// Timestamp timestamp = Timestamp.newBuilder().setSeconds(millis / 1000) +// .setNanos((int) ((millis % 1000) * 1000000)).build(); +// +// +// Example 5: Compute Timestamp from Java `Instant.now()`. +// +// Instant now = Instant.now(); +// +// Timestamp timestamp = +// Timestamp.newBuilder().setSeconds(now.getEpochSecond()) +// .setNanos(now.getNano()).build(); +// +// +// Example 6: Compute Timestamp from current time in Python. +// +// timestamp = Timestamp() +// timestamp.GetCurrentTime() +// +// # JSON Mapping +// +// In JSON format, the Timestamp type is encoded as a string in the +// [RFC 3339](https://www.ietf.org/rfc/rfc3339.txt) format. That is, the +// format is "{year}-{month}-{day}T{hour}:{min}:{sec}[.{frac_sec}]Z" +// where {year} is always expressed using four digits while {month}, {day}, +// {hour}, {min}, and {sec} are zero-padded to two digits each. The fractional +// seconds, which can go up to 9 digits (i.e. up to 1 nanosecond resolution), +// are optional. The "Z" suffix indicates the timezone ("UTC"); the timezone +// is required. A proto3 JSON serializer should always use UTC (as indicated by +// "Z") when printing the Timestamp type and a proto3 JSON parser should be +// able to accept both UTC and other timezones (as indicated by an offset). +// +// For example, "2017-01-15T01:30:15.01Z" encodes 15.01 seconds past +// 01:30 UTC on January 15, 2017. +// +// In JavaScript, one can convert a Date object to this format using the +// standard +// [toISOString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) +// method. In Python, a standard `datetime.datetime` object can be converted +// to this format using +// [`strftime`](https://docs.python.org/2/library/time.html#time.strftime) with +// the time format spec '%Y-%m-%dT%H:%M:%S.%fZ'. Likewise, in Java, one can use +// the Joda Time's [`ISODateTimeFormat.dateTime()`]( +// http://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTime%2D%2D +// ) to obtain a formatter capable of generating timestamps in this format. +// +// +message Timestamp { + // Represents seconds of UTC time since Unix epoch + // 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to + // 9999-12-31T23:59:59Z inclusive. + int64 seconds = 1; + + // Non-negative fractions of a second at nanosecond resolution. Negative + // second values with fractions must still have non-negative nanos values + // that count forward in time. Must be from 0 to 999,999,999 + // inclusive. + int32 nanos = 2; +} diff --git a/_examples/google/protobuf/type.proto b/_examples/google/protobuf/type.proto new file mode 100644 index 0000000..d3f6a68 --- /dev/null +++ b/_examples/google/protobuf/type.proto @@ -0,0 +1,187 @@ +// 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; + +import "google/protobuf/any.proto"; +import "google/protobuf/source_context.proto"; + +option csharp_namespace = "Google.Protobuf.WellKnownTypes"; +option cc_enable_arenas = true; +option java_package = "com.google.protobuf"; +option java_outer_classname = "TypeProto"; +option java_multiple_files = true; +option objc_class_prefix = "GPB"; +option go_package = "google.golang.org/protobuf/types/known/typepb"; + +// A protocol buffer message type. +message Type { + // The fully qualified message name. + string name = 1; + // The list of fields. + repeated Field fields = 2; + // The list of types appearing in `oneof` definitions in this type. + repeated string oneofs = 3; + // The protocol buffer options. + repeated Option options = 4; + // The source context. + SourceContext source_context = 5; + // The source syntax. + Syntax syntax = 6; +} + +// A single field of a message type. +message Field { + // Basic field types. + enum Kind { + // Field type unknown. + TYPE_UNKNOWN = 0; + // Field type double. + TYPE_DOUBLE = 1; + // Field type float. + TYPE_FLOAT = 2; + // Field type int64. + TYPE_INT64 = 3; + // Field type uint64. + TYPE_UINT64 = 4; + // Field type int32. + TYPE_INT32 = 5; + // Field type fixed64. + TYPE_FIXED64 = 6; + // Field type fixed32. + TYPE_FIXED32 = 7; + // Field type bool. + TYPE_BOOL = 8; + // Field type string. + TYPE_STRING = 9; + // Field type group. Proto2 syntax only, and deprecated. + TYPE_GROUP = 10; + // Field type message. + TYPE_MESSAGE = 11; + // Field type bytes. + TYPE_BYTES = 12; + // Field type uint32. + TYPE_UINT32 = 13; + // Field type enum. + TYPE_ENUM = 14; + // Field type sfixed32. + TYPE_SFIXED32 = 15; + // Field type sfixed64. + TYPE_SFIXED64 = 16; + // Field type sint32. + TYPE_SINT32 = 17; + // Field type sint64. + TYPE_SINT64 = 18; + } + + // Whether a field is optional, required, or repeated. + enum Cardinality { + // For fields with unknown cardinality. + CARDINALITY_UNKNOWN = 0; + // For optional fields. + CARDINALITY_OPTIONAL = 1; + // For required fields. Proto2 syntax only. + CARDINALITY_REQUIRED = 2; + // For repeated fields. + CARDINALITY_REPEATED = 3; + } + + // The field type. + Kind kind = 1; + // The field cardinality. + Cardinality cardinality = 2; + // The field number. + int32 number = 3; + // The field name. + string name = 4; + // The field type URL, without the scheme, for message or enumeration + // types. Example: `"type.googleapis.com/google.protobuf.Timestamp"`. + string type_url = 6; + // The index of the field type in `Type.oneofs`, for message or enumeration + // types. The first type has index 1; zero means the type is not in the list. + int32 oneof_index = 7; + // Whether to use alternative packed wire representation. + bool packed = 8; + // The protocol buffer options. + repeated Option options = 9; + // The field JSON name. + string json_name = 10; + // The string value of the default value of this field. Proto2 syntax only. + string default_value = 11; +} + +// Enum type definition. +message Enum { + // Enum type name. + string name = 1; + // Enum value definitions. + repeated EnumValue enumvalue = 2; + // Protocol buffer options. + repeated Option options = 3; + // The source context. + SourceContext source_context = 4; + // The source syntax. + Syntax syntax = 5; +} + +// Enum value definition. +message EnumValue { + // Enum value name. + string name = 1; + // Enum value number. + int32 number = 2; + // Protocol buffer options. + repeated Option options = 3; +} + +// A protocol buffer option, which can be attached to a message, field, +// enumeration, etc. +message Option { + // The option's name. For protobuf built-in options (options defined in + // descriptor.proto), this is the short name. For example, `"map_entry"`. + // For custom options, it should be the fully-qualified name. For example, + // `"google.api.http"`. + string name = 1; + // The option's value packed in an Any message. If the value is a primitive, + // the corresponding wrapper type defined in google/protobuf/wrappers.proto + // should be used. If the value is an enum, it should be stored as an int32 + // value using the google.protobuf.Int32Value type. + Any value = 2; +} + +// The syntax in which a protocol buffer element is defined. +enum Syntax { + // Syntax `proto2`. + SYNTAX_PROTO2 = 0; + // Syntax `proto3`. + SYNTAX_PROTO3 = 1; +} diff --git a/_examples/google/protobuf/wrappers.proto b/_examples/google/protobuf/wrappers.proto new file mode 100644 index 0000000..d49dd53 --- /dev/null +++ b/_examples/google/protobuf/wrappers.proto @@ -0,0 +1,123 @@ +// 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. + +// Wrappers for primitive (non-message) types. These types are useful +// for embedding primitives in the `google.protobuf.Any` type and for places +// where we need to distinguish between the absence of a primitive +// typed field and its default value. +// +// These wrappers have no meaningful use within repeated fields as they lack +// the ability to detect presence on individual elements. +// These wrappers have no meaningful use within a map or a oneof since +// individual entries of a map or fields of a oneof can already detect presence. + +syntax = "proto3"; + +package google.protobuf; + +option csharp_namespace = "Google.Protobuf.WellKnownTypes"; +option cc_enable_arenas = true; +option go_package = "google.golang.org/protobuf/types/known/wrapperspb"; +option java_package = "com.google.protobuf"; +option java_outer_classname = "WrappersProto"; +option java_multiple_files = true; +option objc_class_prefix = "GPB"; + +// Wrapper message for `double`. +// +// The JSON representation for `DoubleValue` is JSON number. +message DoubleValue { + // The double value. + double value = 1; +} + +// Wrapper message for `float`. +// +// The JSON representation for `FloatValue` is JSON number. +message FloatValue { + // The float value. + float value = 1; +} + +// Wrapper message for `int64`. +// +// The JSON representation for `Int64Value` is JSON string. +message Int64Value { + // The int64 value. + int64 value = 1; +} + +// Wrapper message for `uint64`. +// +// The JSON representation for `UInt64Value` is JSON string. +message UInt64Value { + // The uint64 value. + uint64 value = 1; +} + +// Wrapper message for `int32`. +// +// The JSON representation for `Int32Value` is JSON number. +message Int32Value { + // The int32 value. + int32 value = 1; +} + +// Wrapper message for `uint32`. +// +// The JSON representation for `UInt32Value` is JSON number. +message UInt32Value { + // The uint32 value. + uint32 value = 1; +} + +// Wrapper message for `bool`. +// +// The JSON representation for `BoolValue` is JSON `true` and `false`. +message BoolValue { + // The bool value. + bool value = 1; +} + +// Wrapper message for `string`. +// +// The JSON representation for `StringValue` is JSON string. +message StringValue { + // The string value. + string value = 1; +} + +// Wrapper message for `bytes`. +// +// The JSON representation for `BytesValue` is JSON string. +message BytesValue { + // The bytes value. + bytes value = 1; +} diff --git a/testdata/google/protobuf/api.proto b/testdata/google/protobuf/api.proto new file mode 100644 index 0000000..3d598fc --- /dev/null +++ b/testdata/google/protobuf/api.proto @@ -0,0 +1,208 @@ +// 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; + +import "google/protobuf/source_context.proto"; +import "google/protobuf/type.proto"; + +option csharp_namespace = "Google.Protobuf.WellKnownTypes"; +option java_package = "com.google.protobuf"; +option java_outer_classname = "ApiProto"; +option java_multiple_files = true; +option objc_class_prefix = "GPB"; +option go_package = "google.golang.org/protobuf/types/known/apipb"; + +// Api is a light-weight descriptor for an API Interface. +// +// Interfaces are also described as "protocol buffer services" in some contexts, +// such as by the "service" keyword in a .proto file, but they are different +// from API Services, which represent a concrete implementation of an interface +// as opposed to simply a description of methods and bindings. They are also +// sometimes simply referred to as "APIs" in other contexts, such as the name of +// this message itself. See https://cloud.google.com/apis/design/glossary for +// detailed terminology. +message Api { + // The fully qualified name of this interface, including package name + // followed by the interface's simple name. + string name = 1; + + // The methods of this interface, in unspecified order. + repeated Method methods = 2; + + // Any metadata attached to the interface. + repeated Option options = 3; + + // A version string for this interface. If specified, must have the form + // `major-version.minor-version`, as in `1.10`. If the minor version is + // omitted, it defaults to zero. If the entire version field is empty, the + // major version is derived from the package name, as outlined below. If the + // field is not empty, the version in the package name will be verified to be + // consistent with what is provided here. + // + // The versioning schema uses [semantic + // versioning](http://semver.org) where the major version number + // indicates a breaking change and the minor version an additive, + // non-breaking change. Both version numbers are signals to users + // what to expect from different versions, and should be carefully + // chosen based on the product plan. + // + // The major version is also reflected in the package name of the + // interface, which must end in `v`, as in + // `google.feature.v1`. For major versions 0 and 1, the suffix can + // be omitted. Zero major versions must only be used for + // experimental, non-GA interfaces. + // + // + string version = 4; + + // Source context for the protocol buffer service represented by this + // message. + SourceContext source_context = 5; + + // Included interfaces. See [Mixin][]. + repeated Mixin mixins = 6; + + // The source syntax of the service. + Syntax syntax = 7; +} + +// Method represents a method of an API interface. +message Method { + // The simple name of this method. + string name = 1; + + // A URL of the input message type. + string request_type_url = 2; + + // If true, the request is streamed. + bool request_streaming = 3; + + // The URL of the output message type. + string response_type_url = 4; + + // If true, the response is streamed. + bool response_streaming = 5; + + // Any metadata attached to the method. + repeated Option options = 6; + + // The source syntax of this method. + Syntax syntax = 7; +} + +// Declares an API Interface to be included in this interface. The including +// interface must redeclare all the methods from the included interface, but +// documentation and options are inherited as follows: +// +// - If after comment and whitespace stripping, the documentation +// string of the redeclared method is empty, it will be inherited +// from the original method. +// +// - Each annotation belonging to the service config (http, +// visibility) which is not set in the redeclared method will be +// inherited. +// +// - If an http annotation is inherited, the path pattern will be +// modified as follows. Any version prefix will be replaced by the +// version of the including interface plus the [root][] path if +// specified. +// +// Example of a simple mixin: +// +// package google.acl.v1; +// service AccessControl { +// // Get the underlying ACL object. +// rpc GetAcl(GetAclRequest) returns (Acl) { +// option (google.api.http).get = "/v1/{resource=**}:getAcl"; +// } +// } +// +// package google.storage.v2; +// service Storage { +// rpc GetAcl(GetAclRequest) returns (Acl); +// +// // Get a data record. +// rpc GetData(GetDataRequest) returns (Data) { +// option (google.api.http).get = "/v2/{resource=**}"; +// } +// } +// +// Example of a mixin configuration: +// +// apis: +// - name: google.storage.v2.Storage +// mixins: +// - name: google.acl.v1.AccessControl +// +// The mixin construct implies that all methods in `AccessControl` are +// also declared with same name and request/response types in +// `Storage`. A documentation generator or annotation processor will +// see the effective `Storage.GetAcl` method after inheriting +// documentation and annotations as follows: +// +// service Storage { +// // Get the underlying ACL object. +// rpc GetAcl(GetAclRequest) returns (Acl) { +// option (google.api.http).get = "/v2/{resource=**}:getAcl"; +// } +// ... +// } +// +// Note how the version in the path pattern changed from `v1` to `v2`. +// +// If the `root` field in the mixin is specified, it should be a +// relative path under which inherited HTTP paths are placed. Example: +// +// apis: +// - name: google.storage.v2.Storage +// mixins: +// - name: google.acl.v1.AccessControl +// root: acls +// +// This implies the following inherited HTTP annotation: +// +// service Storage { +// // Get the underlying ACL object. +// rpc GetAcl(GetAclRequest) returns (Acl) { +// option (google.api.http).get = "/v2/acls/{resource=**}:getAcl"; +// } +// ... +// } +message Mixin { + // The fully qualified name of the interface which is included. + string name = 1; + + // If non-empty specifies a path under which inherited HTTP paths + // are rooted. + string root = 2; +} diff --git a/testdata/google/protobuf/duration.proto b/testdata/google/protobuf/duration.proto new file mode 100644 index 0000000..81c3e36 --- /dev/null +++ b/testdata/google/protobuf/duration.proto @@ -0,0 +1,116 @@ +// 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 cc_enable_arenas = true; +option go_package = "google.golang.org/protobuf/types/known/durationpb"; +option java_package = "com.google.protobuf"; +option java_outer_classname = "DurationProto"; +option java_multiple_files = true; +option objc_class_prefix = "GPB"; + +// A Duration represents a signed, fixed-length span of time represented +// as a count of seconds and fractions of seconds at nanosecond +// resolution. It is independent of any calendar and concepts like "day" +// or "month". It is related to Timestamp in that the difference between +// two Timestamp values is a Duration and it can be added or subtracted +// from a Timestamp. Range is approximately +-10,000 years. +// +// # Examples +// +// Example 1: Compute Duration from two Timestamps in pseudo code. +// +// Timestamp start = ...; +// Timestamp end = ...; +// Duration duration = ...; +// +// duration.seconds = end.seconds - start.seconds; +// duration.nanos = end.nanos - start.nanos; +// +// if (duration.seconds < 0 && duration.nanos > 0) { +// duration.seconds += 1; +// duration.nanos -= 1000000000; +// } else if (duration.seconds > 0 && duration.nanos < 0) { +// duration.seconds -= 1; +// duration.nanos += 1000000000; +// } +// +// Example 2: Compute Timestamp from Timestamp + Duration in pseudo code. +// +// Timestamp start = ...; +// Duration duration = ...; +// Timestamp end = ...; +// +// end.seconds = start.seconds + duration.seconds; +// end.nanos = start.nanos + duration.nanos; +// +// if (end.nanos < 0) { +// end.seconds -= 1; +// end.nanos += 1000000000; +// } else if (end.nanos >= 1000000000) { +// end.seconds += 1; +// end.nanos -= 1000000000; +// } +// +// Example 3: Compute Duration from datetime.timedelta in Python. +// +// td = datetime.timedelta(days=3, minutes=10) +// duration = Duration() +// duration.FromTimedelta(td) +// +// # JSON Mapping +// +// In JSON format, the Duration type is encoded as a string rather than an +// object, where the string ends in the suffix "s" (indicating seconds) and +// is preceded by the number of seconds, with nanoseconds expressed as +// fractional seconds. For example, 3 seconds with 0 nanoseconds should be +// encoded in JSON format as "3s", while 3 seconds and 1 nanosecond should +// be expressed in JSON format as "3.000000001s", and 3 seconds and 1 +// microsecond should be expressed in JSON format as "3.000001s". +// +// +message Duration { + // Signed seconds of the span of time. Must be from -315,576,000,000 + // to +315,576,000,000 inclusive. Note: these bounds are computed from: + // 60 sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years + int64 seconds = 1; + + // Signed fractions of a second at nanosecond resolution of the span + // of time. Durations less than one second are represented with a 0 + // `seconds` field and a positive or negative `nanos` field. For durations + // of one second or more, a non-zero value for the `nanos` field must be + // of the same sign as the `seconds` field. Must be from -999,999,999 + // to +999,999,999 inclusive. + int32 nanos = 2; +} diff --git a/testdata/google/protobuf/empty.proto b/testdata/google/protobuf/empty.proto new file mode 100644 index 0000000..5f992de --- /dev/null +++ b/testdata/google/protobuf/empty.proto @@ -0,0 +1,52 @@ +// 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 {} diff --git a/testdata/google/protobuf/field_mask.proto b/testdata/google/protobuf/field_mask.proto new file mode 100644 index 0000000..6b5104f --- /dev/null +++ b/testdata/google/protobuf/field_mask.proto @@ -0,0 +1,245 @@ +// 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 java_package = "com.google.protobuf"; +option java_outer_classname = "FieldMaskProto"; +option java_multiple_files = true; +option objc_class_prefix = "GPB"; +option go_package = "google.golang.org/protobuf/types/known/fieldmaskpb"; +option cc_enable_arenas = true; + +// `FieldMask` represents a set of symbolic field paths, for example: +// +// paths: "f.a" +// paths: "f.b.d" +// +// Here `f` represents a field in some root message, `a` and `b` +// fields in the message found in `f`, and `d` a field found in the +// message in `f.b`. +// +// Field masks are used to specify a subset of fields that should be +// returned by a get operation or modified by an update operation. +// Field masks also have a custom JSON encoding (see below). +// +// # Field Masks in Projections +// +// When used in the context of a projection, a response message or +// sub-message is filtered by the API to only contain those fields as +// specified in the mask. For example, if the mask in the previous +// example is applied to a response message as follows: +// +// f { +// a : 22 +// b { +// d : 1 +// x : 2 +// } +// y : 13 +// } +// z: 8 +// +// The result will not contain specific values for fields x,y and z +// (their value will be set to the default, and omitted in proto text +// output): +// +// +// f { +// a : 22 +// b { +// d : 1 +// } +// } +// +// A repeated field is not allowed except at the last position of a +// paths string. +// +// If a FieldMask object is not present in a get operation, the +// operation applies to all fields (as if a FieldMask of all fields +// had been specified). +// +// Note that a field mask does not necessarily apply to the +// top-level response message. In case of a REST get operation, the +// field mask applies directly to the response, but in case of a REST +// list operation, the mask instead applies to each individual message +// in the returned resource list. In case of a REST custom method, +// other definitions may be used. Where the mask applies will be +// clearly documented together with its declaration in the API. In +// any case, the effect on the returned resource/resources is required +// behavior for APIs. +// +// # Field Masks in Update Operations +// +// A field mask in update operations specifies which fields of the +// targeted resource are going to be updated. The API is required +// to only change the values of the fields as specified in the mask +// and leave the others untouched. If a resource is passed in to +// describe the updated values, the API ignores the values of all +// fields not covered by the mask. +// +// If a repeated field is specified for an update operation, new values will +// be appended to the existing repeated field in the target resource. Note that +// a repeated field is only allowed in the last position of a `paths` string. +// +// If a sub-message is specified in the last position of the field mask for an +// update operation, then new value will be merged into the existing sub-message +// in the target resource. +// +// For example, given the target message: +// +// f { +// b { +// d: 1 +// x: 2 +// } +// c: [1] +// } +// +// And an update message: +// +// f { +// b { +// d: 10 +// } +// c: [2] +// } +// +// then if the field mask is: +// +// paths: ["f.b", "f.c"] +// +// then the result will be: +// +// f { +// b { +// d: 10 +// x: 2 +// } +// c: [1, 2] +// } +// +// An implementation may provide options to override this default behavior for +// repeated and message fields. +// +// In order to reset a field's value to the default, the field must +// be in the mask and set to the default value in the provided resource. +// Hence, in order to reset all fields of a resource, provide a default +// instance of the resource and set all fields in the mask, or do +// not provide a mask as described below. +// +// If a field mask is not present on update, the operation applies to +// all fields (as if a field mask of all fields has been specified). +// Note that in the presence of schema evolution, this may mean that +// fields the client does not know and has therefore not filled into +// the request will be reset to their default. If this is unwanted +// behavior, a specific service may require a client to always specify +// a field mask, producing an error if not. +// +// As with get operations, the location of the resource which +// describes the updated values in the request message depends on the +// operation kind. In any case, the effect of the field mask is +// required to be honored by the API. +// +// ## Considerations for HTTP REST +// +// The HTTP kind of an update operation which uses a field mask must +// be set to PATCH instead of PUT in order to satisfy HTTP semantics +// (PUT must only be used for full updates). +// +// # JSON Encoding of Field Masks +// +// In JSON, a field mask is encoded as a single string where paths are +// separated by a comma. Fields name in each path are converted +// to/from lower-camel naming conventions. +// +// As an example, consider the following message declarations: +// +// message Profile { +// User user = 1; +// Photo photo = 2; +// } +// message User { +// string display_name = 1; +// string address = 2; +// } +// +// In proto a field mask for `Profile` may look as such: +// +// mask { +// paths: "user.display_name" +// paths: "photo" +// } +// +// In JSON, the same mask is represented as below: +// +// { +// mask: "user.displayName,photo" +// } +// +// # Field Masks and Oneof Fields +// +// Field masks treat fields in oneofs just as regular fields. Consider the +// following message: +// +// message SampleMessage { +// oneof test_oneof { +// string name = 4; +// SubMessage sub_message = 9; +// } +// } +// +// The field mask can be: +// +// mask { +// paths: "name" +// } +// +// Or: +// +// mask { +// paths: "sub_message" +// } +// +// Note that oneof type names ("test_oneof" in this case) cannot be used in +// paths. +// +// ## Field Mask Verification +// +// The implementation of any API method which has a FieldMask type field in the +// request should verify the included field paths, and return an +// `INVALID_ARGUMENT` error if any path is unmappable. +message FieldMask { + // The set of field mask paths. + repeated string paths = 1; +} diff --git a/testdata/google/protobuf/sourcecontext.proto b/testdata/google/protobuf/sourcecontext.proto new file mode 100644 index 0000000..06bfc43 --- /dev/null +++ b/testdata/google/protobuf/sourcecontext.proto @@ -0,0 +1,48 @@ +// 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 java_package = "com.google.protobuf"; +option java_outer_classname = "SourceContextProto"; +option java_multiple_files = true; +option objc_class_prefix = "GPB"; +option go_package = "google.golang.org/protobuf/types/known/sourcecontextpb"; + +// `SourceContext` represents information about the source of a +// protobuf element, like the file in which it is defined. +message SourceContext { + // The path-qualified name of the .proto file that contained the associated + // protobuf element. For example: `"google/protobuf/source_context.proto"`. + string file_name = 1; +} diff --git a/testdata/google/protobuf/struct.proto b/testdata/google/protobuf/struct.proto new file mode 100644 index 0000000..0ac843c --- /dev/null +++ b/testdata/google/protobuf/struct.proto @@ -0,0 +1,95 @@ +// 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 cc_enable_arenas = true; +option go_package = "google.golang.org/protobuf/types/known/structpb"; +option java_package = "com.google.protobuf"; +option java_outer_classname = "StructProto"; +option java_multiple_files = true; +option objc_class_prefix = "GPB"; + +// `Struct` represents a structured data value, consisting of fields +// which map to dynamically typed values. In some languages, `Struct` +// might be supported by a native representation. For example, in +// scripting languages like JS a struct is represented as an +// object. The details of that representation are described together +// with the proto support for the language. +// +// The JSON representation for `Struct` is JSON object. +message Struct { + // Unordered map of dynamically typed values. + map fields = 1; +} + +// `Value` represents a dynamically typed value which can be either +// null, a number, a string, a boolean, a recursive struct value, or a +// list of values. A producer of value is expected to set one of these +// variants. Absence of any variant indicates an error. +// +// The JSON representation for `Value` is JSON value. +message Value { + // The kind of value. + oneof kind { + // Represents a null value. + NullValue null_value = 1; + // Represents a double value. + double number_value = 2; + // Represents a string value. + string string_value = 3; + // Represents a boolean value. + bool bool_value = 4; + // Represents a structured value. + Struct struct_value = 5; + // Represents a repeated `Value`. + ListValue list_value = 6; + } +} + +// `NullValue` is a singleton enumeration to represent the null value for the +// `Value` type union. +// +// The JSON representation for `NullValue` is JSON `null`. +enum NullValue { + // Null value. + NULL_VALUE = 0; +} + +// `ListValue` is a wrapper around a repeated field of values. +// +// The JSON representation for `ListValue` is JSON array. +message ListValue { + // Repeated field of dynamically typed values. + repeated Value values = 1; +} diff --git a/testdata/google/protobuf/timestamp.proto b/testdata/google/protobuf/timestamp.proto new file mode 100644 index 0000000..3b2df6d --- /dev/null +++ b/testdata/google/protobuf/timestamp.proto @@ -0,0 +1,147 @@ +// 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 cc_enable_arenas = true; +option go_package = "google.golang.org/protobuf/types/known/timestamppb"; +option java_package = "com.google.protobuf"; +option java_outer_classname = "TimestampProto"; +option java_multiple_files = true; +option objc_class_prefix = "GPB"; + +// A Timestamp represents a point in time independent of any time zone or local +// calendar, encoded as a count of seconds and fractions of seconds at +// nanosecond resolution. The count is relative to an epoch at UTC midnight on +// January 1, 1970, in the proleptic Gregorian calendar which extends the +// Gregorian calendar backwards to year one. +// +// All minutes are 60 seconds long. Leap seconds are "smeared" so that no leap +// second table is needed for interpretation, using a [24-hour linear +// smear](https://developers.google.com/time/smear). +// +// The range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. By +// restricting to that range, we ensure that we can convert to and from [RFC +// 3339](https://www.ietf.org/rfc/rfc3339.txt) date strings. +// +// # Examples +// +// Example 1: Compute Timestamp from POSIX `time()`. +// +// Timestamp timestamp; +// timestamp.set_seconds(time(NULL)); +// timestamp.set_nanos(0); +// +// Example 2: Compute Timestamp from POSIX `gettimeofday()`. +// +// struct timeval tv; +// gettimeofday(&tv, NULL); +// +// Timestamp timestamp; +// timestamp.set_seconds(tv.tv_sec); +// timestamp.set_nanos(tv.tv_usec * 1000); +// +// Example 3: Compute Timestamp from Win32 `GetSystemTimeAsFileTime()`. +// +// FILETIME ft; +// GetSystemTimeAsFileTime(&ft); +// UINT64 ticks = (((UINT64)ft.dwHighDateTime) << 32) | ft.dwLowDateTime; +// +// // A Windows tick is 100 nanoseconds. Windows epoch 1601-01-01T00:00:00Z +// // is 11644473600 seconds before Unix epoch 1970-01-01T00:00:00Z. +// Timestamp timestamp; +// timestamp.set_seconds((INT64) ((ticks / 10000000) - 11644473600LL)); +// timestamp.set_nanos((INT32) ((ticks % 10000000) * 100)); +// +// Example 4: Compute Timestamp from Java `System.currentTimeMillis()`. +// +// long millis = System.currentTimeMillis(); +// +// Timestamp timestamp = Timestamp.newBuilder().setSeconds(millis / 1000) +// .setNanos((int) ((millis % 1000) * 1000000)).build(); +// +// +// Example 5: Compute Timestamp from Java `Instant.now()`. +// +// Instant now = Instant.now(); +// +// Timestamp timestamp = +// Timestamp.newBuilder().setSeconds(now.getEpochSecond()) +// .setNanos(now.getNano()).build(); +// +// +// Example 6: Compute Timestamp from current time in Python. +// +// timestamp = Timestamp() +// timestamp.GetCurrentTime() +// +// # JSON Mapping +// +// In JSON format, the Timestamp type is encoded as a string in the +// [RFC 3339](https://www.ietf.org/rfc/rfc3339.txt) format. That is, the +// format is "{year}-{month}-{day}T{hour}:{min}:{sec}[.{frac_sec}]Z" +// where {year} is always expressed using four digits while {month}, {day}, +// {hour}, {min}, and {sec} are zero-padded to two digits each. The fractional +// seconds, which can go up to 9 digits (i.e. up to 1 nanosecond resolution), +// are optional. The "Z" suffix indicates the timezone ("UTC"); the timezone +// is required. A proto3 JSON serializer should always use UTC (as indicated by +// "Z") when printing the Timestamp type and a proto3 JSON parser should be +// able to accept both UTC and other timezones (as indicated by an offset). +// +// For example, "2017-01-15T01:30:15.01Z" encodes 15.01 seconds past +// 01:30 UTC on January 15, 2017. +// +// In JavaScript, one can convert a Date object to this format using the +// standard +// [toISOString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) +// method. In Python, a standard `datetime.datetime` object can be converted +// to this format using +// [`strftime`](https://docs.python.org/2/library/time.html#time.strftime) with +// the time format spec '%Y-%m-%dT%H:%M:%S.%fZ'. Likewise, in Java, one can use +// the Joda Time's [`ISODateTimeFormat.dateTime()`]( +// http://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTime%2D%2D +// ) to obtain a formatter capable of generating timestamps in this format. +// +// +message Timestamp { + // Represents seconds of UTC time since Unix epoch + // 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to + // 9999-12-31T23:59:59Z inclusive. + int64 seconds = 1; + + // Non-negative fractions of a second at nanosecond resolution. Negative + // second values with fractions must still have non-negative nanos values + // that count forward in time. Must be from 0 to 999,999,999 + // inclusive. + int32 nanos = 2; +} diff --git a/testdata/google/protobuf/type.proto b/testdata/google/protobuf/type.proto new file mode 100644 index 0000000..d3f6a68 --- /dev/null +++ b/testdata/google/protobuf/type.proto @@ -0,0 +1,187 @@ +// 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; + +import "google/protobuf/any.proto"; +import "google/protobuf/source_context.proto"; + +option csharp_namespace = "Google.Protobuf.WellKnownTypes"; +option cc_enable_arenas = true; +option java_package = "com.google.protobuf"; +option java_outer_classname = "TypeProto"; +option java_multiple_files = true; +option objc_class_prefix = "GPB"; +option go_package = "google.golang.org/protobuf/types/known/typepb"; + +// A protocol buffer message type. +message Type { + // The fully qualified message name. + string name = 1; + // The list of fields. + repeated Field fields = 2; + // The list of types appearing in `oneof` definitions in this type. + repeated string oneofs = 3; + // The protocol buffer options. + repeated Option options = 4; + // The source context. + SourceContext source_context = 5; + // The source syntax. + Syntax syntax = 6; +} + +// A single field of a message type. +message Field { + // Basic field types. + enum Kind { + // Field type unknown. + TYPE_UNKNOWN = 0; + // Field type double. + TYPE_DOUBLE = 1; + // Field type float. + TYPE_FLOAT = 2; + // Field type int64. + TYPE_INT64 = 3; + // Field type uint64. + TYPE_UINT64 = 4; + // Field type int32. + TYPE_INT32 = 5; + // Field type fixed64. + TYPE_FIXED64 = 6; + // Field type fixed32. + TYPE_FIXED32 = 7; + // Field type bool. + TYPE_BOOL = 8; + // Field type string. + TYPE_STRING = 9; + // Field type group. Proto2 syntax only, and deprecated. + TYPE_GROUP = 10; + // Field type message. + TYPE_MESSAGE = 11; + // Field type bytes. + TYPE_BYTES = 12; + // Field type uint32. + TYPE_UINT32 = 13; + // Field type enum. + TYPE_ENUM = 14; + // Field type sfixed32. + TYPE_SFIXED32 = 15; + // Field type sfixed64. + TYPE_SFIXED64 = 16; + // Field type sint32. + TYPE_SINT32 = 17; + // Field type sint64. + TYPE_SINT64 = 18; + } + + // Whether a field is optional, required, or repeated. + enum Cardinality { + // For fields with unknown cardinality. + CARDINALITY_UNKNOWN = 0; + // For optional fields. + CARDINALITY_OPTIONAL = 1; + // For required fields. Proto2 syntax only. + CARDINALITY_REQUIRED = 2; + // For repeated fields. + CARDINALITY_REPEATED = 3; + } + + // The field type. + Kind kind = 1; + // The field cardinality. + Cardinality cardinality = 2; + // The field number. + int32 number = 3; + // The field name. + string name = 4; + // The field type URL, without the scheme, for message or enumeration + // types. Example: `"type.googleapis.com/google.protobuf.Timestamp"`. + string type_url = 6; + // The index of the field type in `Type.oneofs`, for message or enumeration + // types. The first type has index 1; zero means the type is not in the list. + int32 oneof_index = 7; + // Whether to use alternative packed wire representation. + bool packed = 8; + // The protocol buffer options. + repeated Option options = 9; + // The field JSON name. + string json_name = 10; + // The string value of the default value of this field. Proto2 syntax only. + string default_value = 11; +} + +// Enum type definition. +message Enum { + // Enum type name. + string name = 1; + // Enum value definitions. + repeated EnumValue enumvalue = 2; + // Protocol buffer options. + repeated Option options = 3; + // The source context. + SourceContext source_context = 4; + // The source syntax. + Syntax syntax = 5; +} + +// Enum value definition. +message EnumValue { + // Enum value name. + string name = 1; + // Enum value number. + int32 number = 2; + // Protocol buffer options. + repeated Option options = 3; +} + +// A protocol buffer option, which can be attached to a message, field, +// enumeration, etc. +message Option { + // The option's name. For protobuf built-in options (options defined in + // descriptor.proto), this is the short name. For example, `"map_entry"`. + // For custom options, it should be the fully-qualified name. For example, + // `"google.api.http"`. + string name = 1; + // The option's value packed in an Any message. If the value is a primitive, + // the corresponding wrapper type defined in google/protobuf/wrappers.proto + // should be used. If the value is an enum, it should be stored as an int32 + // value using the google.protobuf.Int32Value type. + Any value = 2; +} + +// The syntax in which a protocol buffer element is defined. +enum Syntax { + // Syntax `proto2`. + SYNTAX_PROTO2 = 0; + // Syntax `proto3`. + SYNTAX_PROTO3 = 1; +} diff --git a/testdata/google/protobuf/wrappers.proto b/testdata/google/protobuf/wrappers.proto new file mode 100644 index 0000000..d49dd53 --- /dev/null +++ b/testdata/google/protobuf/wrappers.proto @@ -0,0 +1,123 @@ +// 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. + +// Wrappers for primitive (non-message) types. These types are useful +// for embedding primitives in the `google.protobuf.Any` type and for places +// where we need to distinguish between the absence of a primitive +// typed field and its default value. +// +// These wrappers have no meaningful use within repeated fields as they lack +// the ability to detect presence on individual elements. +// These wrappers have no meaningful use within a map or a oneof since +// individual entries of a map or fields of a oneof can already detect presence. + +syntax = "proto3"; + +package google.protobuf; + +option csharp_namespace = "Google.Protobuf.WellKnownTypes"; +option cc_enable_arenas = true; +option go_package = "google.golang.org/protobuf/types/known/wrapperspb"; +option java_package = "com.google.protobuf"; +option java_outer_classname = "WrappersProto"; +option java_multiple_files = true; +option objc_class_prefix = "GPB"; + +// Wrapper message for `double`. +// +// The JSON representation for `DoubleValue` is JSON number. +message DoubleValue { + // The double value. + double value = 1; +} + +// Wrapper message for `float`. +// +// The JSON representation for `FloatValue` is JSON number. +message FloatValue { + // The float value. + float value = 1; +} + +// Wrapper message for `int64`. +// +// The JSON representation for `Int64Value` is JSON string. +message Int64Value { + // The int64 value. + int64 value = 1; +} + +// Wrapper message for `uint64`. +// +// The JSON representation for `UInt64Value` is JSON string. +message UInt64Value { + // The uint64 value. + uint64 value = 1; +} + +// Wrapper message for `int32`. +// +// The JSON representation for `Int32Value` is JSON number. +message Int32Value { + // The int32 value. + int32 value = 1; +} + +// Wrapper message for `uint32`. +// +// The JSON representation for `UInt32Value` is JSON number. +message UInt32Value { + // The uint32 value. + uint32 value = 1; +} + +// Wrapper message for `bool`. +// +// The JSON representation for `BoolValue` is JSON `true` and `false`. +message BoolValue { + // The bool value. + bool value = 1; +} + +// Wrapper message for `string`. +// +// The JSON representation for `StringValue` is JSON string. +message StringValue { + // The string value. + string value = 1; +} + +// Wrapper message for `bytes`. +// +// The JSON representation for `BytesValue` is JSON string. +message BytesValue { + // The bytes value. + bytes value = 1; +} From 5afb6792c59b7e782ec86b1038f24aca43fb23b7 Mon Sep 17 00:00:00 2001 From: nametake Date: Tue, 23 Nov 2021 12:33:29 +0900 Subject: [PATCH 08/12] Fixed case source file string --- gengohttp.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gengohttp.go b/gengohttp.go index 26e88e1..12bd923 100644 --- a/gengohttp.go +++ b/gengohttp.go @@ -710,9 +710,9 @@ func genMessageName(msg *protogen.Message) protogen.GoIdent { return durationpbPackage.Ident(msg.GoIdent.GoName) case "google/protobuf/empty.proto": return emptypbPackage.Ident(msg.GoIdent.GoName) - case "google/protobuf/fieldmask.proto": + case "google/protobuf/field_mask.proto": return fieldmaskpbPackage.Ident(msg.GoIdent.GoName) - case "google/protobuf/sourcecontext.proto": + case "google/protobuf/source_context.proto": return sourcecontextpbPackage.Ident(msg.GoIdent.GoName) case "google/protobuf/struct.proto": return statusPackage.Ident(msg.GoIdent.GoName) From ae15cc0f489a03ac54236361fca7936097be7167 Mon Sep 17 00:00:00 2001 From: nametake Date: Tue, 23 Nov 2021 13:52:47 +0900 Subject: [PATCH 09/12] Fixed source_context.proto name --- Makefile | 4 ++-- .../protobuf/{sourcecontext.proto => source_context.proto} | 0 .../protobuf/{sourcecontext.proto => source_context.proto} | 0 testdata/knowntypes/knowntypes.proto | 2 ++ 4 files changed, 4 insertions(+), 2 deletions(-) rename _examples/google/protobuf/{sourcecontext.proto => source_context.proto} (100%) rename testdata/google/protobuf/{sourcecontext.proto => source_context.proto} (100%) diff --git a/Makefile b/Makefile index 3523bef..157c649 100644 --- a/Makefile +++ b/Makefile @@ -33,8 +33,8 @@ curl_google_option_proto: @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/empty.proto> testdata/google/protobuf/empty.proto @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/field_mask.proto> _examples/google/protobuf/field_mask.proto @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/field_mask.proto> testdata/google/protobuf/field_mask.proto - @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/source_context.proto> _examples/google/protobuf/sourcecontext.proto - @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/source_context.proto> testdata/google/protobuf/sourcecontext.proto + @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/source_context.proto> _examples/google/protobuf/source_context.proto + @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/source_context.proto> testdata/google/protobuf/source_context.proto @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/struct.proto> _examples/google/protobuf/struct.proto @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/struct.proto> testdata/google/protobuf/struct.proto @curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/timestamp.proto> _examples/google/protobuf/timestamp.proto diff --git a/_examples/google/protobuf/sourcecontext.proto b/_examples/google/protobuf/source_context.proto similarity index 100% rename from _examples/google/protobuf/sourcecontext.proto rename to _examples/google/protobuf/source_context.proto diff --git a/testdata/google/protobuf/sourcecontext.proto b/testdata/google/protobuf/source_context.proto similarity index 100% rename from testdata/google/protobuf/sourcecontext.proto rename to testdata/google/protobuf/source_context.proto diff --git a/testdata/knowntypes/knowntypes.proto b/testdata/knowntypes/knowntypes.proto index 89997b7..e86cfba 100644 --- a/testdata/knowntypes/knowntypes.proto +++ b/testdata/knowntypes/knowntypes.proto @@ -5,8 +5,10 @@ package knowntypes; option go_package = "./knowntypes/;knowntypespb"; import "google/protobuf/any.proto"; +import "google/protobuf/api.proto"; service KnownTypesService { rpc Any(google.protobuf.Any) returns (google.protobuf.Any); + rpc Api(google.protobuf.Api) returns (google.protobuf.Api); } From 9668a9e0ad6754456d80ae0c0a3900a4261ca390 Mon Sep 17 00:00:00 2001 From: nametake Date: Tue, 23 Nov 2021 14:09:47 +0900 Subject: [PATCH 10/12] Added rpc --- testdata/knowntypes/knowntypes.proto | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/testdata/knowntypes/knowntypes.proto b/testdata/knowntypes/knowntypes.proto index e86cfba..cd36531 100644 --- a/testdata/knowntypes/knowntypes.proto +++ b/testdata/knowntypes/knowntypes.proto @@ -6,9 +6,25 @@ option go_package = "./knowntypes/;knowntypespb"; import "google/protobuf/any.proto"; import "google/protobuf/api.proto"; +import "google/protobuf/duration.proto"; +import "google/protobuf/empty.proto"; +import "google/protobuf/field_mask.proto"; +import "google/protobuf/source_context.proto"; +import "google/protobuf/struct.proto"; +import "google/protobuf/timestamp.proto"; +import "google/protobuf/type.proto"; +import "google/protobuf/wrappers.proto"; service KnownTypesService { rpc Any(google.protobuf.Any) returns (google.protobuf.Any); rpc Api(google.protobuf.Api) returns (google.protobuf.Api); + rpc Duration(google.protobuf.Duration) returns (google.protobuf.Duration); + rpc Empty(google.protobuf.Empty) returns (google.protobuf.Empty); + rpc FieldMask(google.protobuf.FieldMask) returns (google.protobuf.FieldMask); + rpc SourceContext(google.protobuf.SourceContext) returns (google.protobuf.SourceContext); + rpc Struct(google.protobuf.Struct) returns (google.protobuf.Struct); + rpc Timestamp(google.protobuf.Timestamp) returns (google.protobuf.Timestamp); + rpc Type(google.protobuf.Type) returns (google.protobuf.Type); + rpc Wrappers(google.protobuf.BoolValue) returns (google.protobuf.BoolValue); } From 0850744c6247fd022b8a8f8ef2f9bf72d2004961 Mon Sep 17 00:00:00 2001 From: nametake Date: Tue, 23 Nov 2021 14:17:08 +0900 Subject: [PATCH 11/12] Fixed golden file --- testdata/knowntypes/knowntypes.http.go | 1322 ++++++++++++++++++++++++ 1 file changed, 1322 insertions(+) diff --git a/testdata/knowntypes/knowntypes.http.go b/testdata/knowntypes/knowntypes.http.go index 9cf25b6..1d50b8d 100644 --- a/testdata/knowntypes/knowntypes.http.go +++ b/testdata/knowntypes/knowntypes.http.go @@ -13,6 +13,14 @@ import ( protojson "google.golang.org/protobuf/encoding/protojson" proto "google.golang.org/protobuf/proto" anypb "google.golang.org/protobuf/types/known/anypb" + apipb "google.golang.org/protobuf/types/known/apipb" + durationpb "google.golang.org/protobuf/types/known/durationpb" + emptypb "google.golang.org/protobuf/types/known/emptypb" + fieldmaskpb "google.golang.org/protobuf/types/known/fieldmaskpb" + sourcecontextpb "google.golang.org/protobuf/types/known/sourcecontextpb" + timestamppb "google.golang.org/protobuf/types/known/timestamppb" + typepb "google.golang.org/protobuf/types/known/typepb" + wrapperspb "google.golang.org/protobuf/types/known/wrapperspb" io "io" ioutil "io/ioutil" mime "mime" @@ -23,6 +31,15 @@ import ( // KnownTypesServiceHTTPService is the server API for KnownTypesService service. type KnownTypesServiceHTTPService interface { Any(context.Context, *anypb.Any) (*anypb.Any, error) + Api(context.Context, *apipb.Api) (*apipb.Api, error) + Duration(context.Context, *durationpb.Duration) (*durationpb.Duration, error) + Empty(context.Context, *emptypb.Empty) (*emptypb.Empty, error) + FieldMask(context.Context, *fieldmaskpb.FieldMask) (*fieldmaskpb.FieldMask, error) + SourceContext(context.Context, *sourcecontextpb.SourceContext) (*sourcecontextpb.SourceContext, error) + Struct(context.Context, *status.Struct) (*status.Struct, error) + Timestamp(context.Context, *timestamppb.Timestamp) (*timestamppb.Timestamp, error) + Type(context.Context, *typepb.Type) (*typepb.Type, error) + Wrappers(context.Context, *wrapperspb.BoolValue) (*wrapperspb.BoolValue, error) } // KnownTypesServiceHTTPConverter has a function to convert KnownTypesServiceHTTPService interface to http.HandlerFunc. @@ -181,3 +198,1308 @@ func (h *KnownTypesServiceHTTPConverter) Any(cb func(ctx context.Context, w http func (h *KnownTypesServiceHTTPConverter) AnyWithName(cb func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error), interceptors ...grpc.UnaryServerInterceptor) (string, string, http.HandlerFunc) { return "KnownTypesService", "Any", h.Any(cb, interceptors...) } + +// Api returns KnownTypesServiceHTTPService interface's Api converted to http.HandlerFunc. +func (h *KnownTypesServiceHTTPConverter) Api(cb func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error), interceptors ...grpc.UnaryServerInterceptor) http.HandlerFunc { + if cb == nil { + cb = func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error) { + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + p := status.New(codes.Unknown, err.Error()).Proto() + switch contentType, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")); contentType { + case "application/protobuf", "application/x-protobuf": + buf, err := proto.Marshal(p) + if err != nil { + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + return + } + case "application/json": + buf, err := protojson.Marshal(p) + if err != nil { + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + return + } + default: + } + } + } + } + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + contentType, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")) + + accepts := strings.Split(r.Header.Get("Accept"), ",") + accept := accepts[0] + if accept == "*/*" || accept == "" { + if contentType != "" { + accept = contentType + } else { + accept = "application/json" + } + } + + w.Header().Set("Content-Type", accept) + + arg := &apipb.Api{} + if r.Method != http.MethodGet { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + + switch contentType { + case "application/protobuf", "application/x-protobuf": + if err := proto.Unmarshal(body, arg); err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + case "application/json": + if err := protojson.Unmarshal(body, arg); err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + default: + w.WriteHeader(http.StatusUnsupportedMediaType) + _, err := fmt.Fprintf(w, "Unsupported Content-Type: %s", contentType) + cb(ctx, w, r, nil, nil, err) + return + } + } + + n := len(interceptors) + chained := func(ctx context.Context, arg interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { + chainer := func(currentInter grpc.UnaryServerInterceptor, currentHandler grpc.UnaryHandler) grpc.UnaryHandler { + return func(currentCtx context.Context, currentReq interface{}) (interface{}, error) { + return currentInter(currentCtx, currentReq, info, currentHandler) + } + } + + chainedHandler := handler + for i := n - 1; i >= 0; i-- { + chainedHandler = chainer(interceptors[i], chainedHandler) + } + return chainedHandler(ctx, arg) + } + + info := &grpc.UnaryServerInfo{ + Server: h.srv, + FullMethod: "/knowntypes.KnownTypesService/Api", + } + + handler := func(c context.Context, req interface{}) (interface{}, error) { + return h.srv.Api(c, req.(*apipb.Api)) + } + + iret, err := chained(ctx, arg, info, handler) + if err != nil { + cb(ctx, w, r, arg, nil, err) + return + } + + ret, ok := iret.(*apipb.Api) + if !ok { + cb(ctx, w, r, arg, nil, fmt.Errorf("/knowntypes.KnownTypesService/Api: interceptors have not return apipb.Api")) + return + } + + switch accept { + case "application/protobuf", "application/x-protobuf": + buf, err := proto.Marshal(ret) + if err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + case "application/json": + buf, err := protojson.Marshal(ret) + if err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + default: + w.WriteHeader(http.StatusUnsupportedMediaType) + _, err := fmt.Fprintf(w, "Unsupported Accept: %s", accept) + cb(ctx, w, r, arg, ret, err) + return + } + cb(ctx, w, r, arg, ret, nil) + }) +} + +// ApiWithName returns Service name, Method name and KnownTypesServiceHTTPService interface's Api converted to http.HandlerFunc. +func (h *KnownTypesServiceHTTPConverter) ApiWithName(cb func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error), interceptors ...grpc.UnaryServerInterceptor) (string, string, http.HandlerFunc) { + return "KnownTypesService", "Api", h.Api(cb, interceptors...) +} + +// Duration returns KnownTypesServiceHTTPService interface's Duration converted to http.HandlerFunc. +func (h *KnownTypesServiceHTTPConverter) Duration(cb func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error), interceptors ...grpc.UnaryServerInterceptor) http.HandlerFunc { + if cb == nil { + cb = func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error) { + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + p := status.New(codes.Unknown, err.Error()).Proto() + switch contentType, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")); contentType { + case "application/protobuf", "application/x-protobuf": + buf, err := proto.Marshal(p) + if err != nil { + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + return + } + case "application/json": + buf, err := protojson.Marshal(p) + if err != nil { + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + return + } + default: + } + } + } + } + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + contentType, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")) + + accepts := strings.Split(r.Header.Get("Accept"), ",") + accept := accepts[0] + if accept == "*/*" || accept == "" { + if contentType != "" { + accept = contentType + } else { + accept = "application/json" + } + } + + w.Header().Set("Content-Type", accept) + + arg := &durationpb.Duration{} + if r.Method != http.MethodGet { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + + switch contentType { + case "application/protobuf", "application/x-protobuf": + if err := proto.Unmarshal(body, arg); err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + case "application/json": + if err := protojson.Unmarshal(body, arg); err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + default: + w.WriteHeader(http.StatusUnsupportedMediaType) + _, err := fmt.Fprintf(w, "Unsupported Content-Type: %s", contentType) + cb(ctx, w, r, nil, nil, err) + return + } + } + + n := len(interceptors) + chained := func(ctx context.Context, arg interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { + chainer := func(currentInter grpc.UnaryServerInterceptor, currentHandler grpc.UnaryHandler) grpc.UnaryHandler { + return func(currentCtx context.Context, currentReq interface{}) (interface{}, error) { + return currentInter(currentCtx, currentReq, info, currentHandler) + } + } + + chainedHandler := handler + for i := n - 1; i >= 0; i-- { + chainedHandler = chainer(interceptors[i], chainedHandler) + } + return chainedHandler(ctx, arg) + } + + info := &grpc.UnaryServerInfo{ + Server: h.srv, + FullMethod: "/knowntypes.KnownTypesService/Duration", + } + + handler := func(c context.Context, req interface{}) (interface{}, error) { + return h.srv.Duration(c, req.(*durationpb.Duration)) + } + + iret, err := chained(ctx, arg, info, handler) + if err != nil { + cb(ctx, w, r, arg, nil, err) + return + } + + ret, ok := iret.(*durationpb.Duration) + if !ok { + cb(ctx, w, r, arg, nil, fmt.Errorf("/knowntypes.KnownTypesService/Duration: interceptors have not return durationpb.Duration")) + return + } + + switch accept { + case "application/protobuf", "application/x-protobuf": + buf, err := proto.Marshal(ret) + if err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + case "application/json": + buf, err := protojson.Marshal(ret) + if err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + default: + w.WriteHeader(http.StatusUnsupportedMediaType) + _, err := fmt.Fprintf(w, "Unsupported Accept: %s", accept) + cb(ctx, w, r, arg, ret, err) + return + } + cb(ctx, w, r, arg, ret, nil) + }) +} + +// DurationWithName returns Service name, Method name and KnownTypesServiceHTTPService interface's Duration converted to http.HandlerFunc. +func (h *KnownTypesServiceHTTPConverter) DurationWithName(cb func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error), interceptors ...grpc.UnaryServerInterceptor) (string, string, http.HandlerFunc) { + return "KnownTypesService", "Duration", h.Duration(cb, interceptors...) +} + +// Empty returns KnownTypesServiceHTTPService interface's Empty converted to http.HandlerFunc. +func (h *KnownTypesServiceHTTPConverter) Empty(cb func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error), interceptors ...grpc.UnaryServerInterceptor) http.HandlerFunc { + if cb == nil { + cb = func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error) { + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + p := status.New(codes.Unknown, err.Error()).Proto() + switch contentType, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")); contentType { + case "application/protobuf", "application/x-protobuf": + buf, err := proto.Marshal(p) + if err != nil { + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + return + } + case "application/json": + buf, err := protojson.Marshal(p) + if err != nil { + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + return + } + default: + } + } + } + } + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + contentType, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")) + + accepts := strings.Split(r.Header.Get("Accept"), ",") + accept := accepts[0] + if accept == "*/*" || accept == "" { + if contentType != "" { + accept = contentType + } else { + accept = "application/json" + } + } + + w.Header().Set("Content-Type", accept) + + arg := &emptypb.Empty{} + if r.Method != http.MethodGet { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + + switch contentType { + case "application/protobuf", "application/x-protobuf": + if err := proto.Unmarshal(body, arg); err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + case "application/json": + if err := protojson.Unmarshal(body, arg); err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + default: + w.WriteHeader(http.StatusUnsupportedMediaType) + _, err := fmt.Fprintf(w, "Unsupported Content-Type: %s", contentType) + cb(ctx, w, r, nil, nil, err) + return + } + } + + n := len(interceptors) + chained := func(ctx context.Context, arg interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { + chainer := func(currentInter grpc.UnaryServerInterceptor, currentHandler grpc.UnaryHandler) grpc.UnaryHandler { + return func(currentCtx context.Context, currentReq interface{}) (interface{}, error) { + return currentInter(currentCtx, currentReq, info, currentHandler) + } + } + + chainedHandler := handler + for i := n - 1; i >= 0; i-- { + chainedHandler = chainer(interceptors[i], chainedHandler) + } + return chainedHandler(ctx, arg) + } + + info := &grpc.UnaryServerInfo{ + Server: h.srv, + FullMethod: "/knowntypes.KnownTypesService/Empty", + } + + handler := func(c context.Context, req interface{}) (interface{}, error) { + return h.srv.Empty(c, req.(*emptypb.Empty)) + } + + iret, err := chained(ctx, arg, info, handler) + if err != nil { + cb(ctx, w, r, arg, nil, err) + return + } + + ret, ok := iret.(*emptypb.Empty) + if !ok { + cb(ctx, w, r, arg, nil, fmt.Errorf("/knowntypes.KnownTypesService/Empty: interceptors have not return emptypb.Empty")) + return + } + + switch accept { + case "application/protobuf", "application/x-protobuf": + buf, err := proto.Marshal(ret) + if err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + case "application/json": + buf, err := protojson.Marshal(ret) + if err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + default: + w.WriteHeader(http.StatusUnsupportedMediaType) + _, err := fmt.Fprintf(w, "Unsupported Accept: %s", accept) + cb(ctx, w, r, arg, ret, err) + return + } + cb(ctx, w, r, arg, ret, nil) + }) +} + +// EmptyWithName returns Service name, Method name and KnownTypesServiceHTTPService interface's Empty converted to http.HandlerFunc. +func (h *KnownTypesServiceHTTPConverter) EmptyWithName(cb func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error), interceptors ...grpc.UnaryServerInterceptor) (string, string, http.HandlerFunc) { + return "KnownTypesService", "Empty", h.Empty(cb, interceptors...) +} + +// FieldMask returns KnownTypesServiceHTTPService interface's FieldMask converted to http.HandlerFunc. +func (h *KnownTypesServiceHTTPConverter) FieldMask(cb func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error), interceptors ...grpc.UnaryServerInterceptor) http.HandlerFunc { + if cb == nil { + cb = func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error) { + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + p := status.New(codes.Unknown, err.Error()).Proto() + switch contentType, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")); contentType { + case "application/protobuf", "application/x-protobuf": + buf, err := proto.Marshal(p) + if err != nil { + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + return + } + case "application/json": + buf, err := protojson.Marshal(p) + if err != nil { + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + return + } + default: + } + } + } + } + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + contentType, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")) + + accepts := strings.Split(r.Header.Get("Accept"), ",") + accept := accepts[0] + if accept == "*/*" || accept == "" { + if contentType != "" { + accept = contentType + } else { + accept = "application/json" + } + } + + w.Header().Set("Content-Type", accept) + + arg := &fieldmaskpb.FieldMask{} + if r.Method != http.MethodGet { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + + switch contentType { + case "application/protobuf", "application/x-protobuf": + if err := proto.Unmarshal(body, arg); err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + case "application/json": + if err := protojson.Unmarshal(body, arg); err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + default: + w.WriteHeader(http.StatusUnsupportedMediaType) + _, err := fmt.Fprintf(w, "Unsupported Content-Type: %s", contentType) + cb(ctx, w, r, nil, nil, err) + return + } + } + + n := len(interceptors) + chained := func(ctx context.Context, arg interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { + chainer := func(currentInter grpc.UnaryServerInterceptor, currentHandler grpc.UnaryHandler) grpc.UnaryHandler { + return func(currentCtx context.Context, currentReq interface{}) (interface{}, error) { + return currentInter(currentCtx, currentReq, info, currentHandler) + } + } + + chainedHandler := handler + for i := n - 1; i >= 0; i-- { + chainedHandler = chainer(interceptors[i], chainedHandler) + } + return chainedHandler(ctx, arg) + } + + info := &grpc.UnaryServerInfo{ + Server: h.srv, + FullMethod: "/knowntypes.KnownTypesService/FieldMask", + } + + handler := func(c context.Context, req interface{}) (interface{}, error) { + return h.srv.FieldMask(c, req.(*fieldmaskpb.FieldMask)) + } + + iret, err := chained(ctx, arg, info, handler) + if err != nil { + cb(ctx, w, r, arg, nil, err) + return + } + + ret, ok := iret.(*fieldmaskpb.FieldMask) + if !ok { + cb(ctx, w, r, arg, nil, fmt.Errorf("/knowntypes.KnownTypesService/FieldMask: interceptors have not return fieldmaskpb.FieldMask")) + return + } + + switch accept { + case "application/protobuf", "application/x-protobuf": + buf, err := proto.Marshal(ret) + if err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + case "application/json": + buf, err := protojson.Marshal(ret) + if err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + default: + w.WriteHeader(http.StatusUnsupportedMediaType) + _, err := fmt.Fprintf(w, "Unsupported Accept: %s", accept) + cb(ctx, w, r, arg, ret, err) + return + } + cb(ctx, w, r, arg, ret, nil) + }) +} + +// FieldMaskWithName returns Service name, Method name and KnownTypesServiceHTTPService interface's FieldMask converted to http.HandlerFunc. +func (h *KnownTypesServiceHTTPConverter) FieldMaskWithName(cb func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error), interceptors ...grpc.UnaryServerInterceptor) (string, string, http.HandlerFunc) { + return "KnownTypesService", "FieldMask", h.FieldMask(cb, interceptors...) +} + +// SourceContext returns KnownTypesServiceHTTPService interface's SourceContext converted to http.HandlerFunc. +func (h *KnownTypesServiceHTTPConverter) SourceContext(cb func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error), interceptors ...grpc.UnaryServerInterceptor) http.HandlerFunc { + if cb == nil { + cb = func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error) { + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + p := status.New(codes.Unknown, err.Error()).Proto() + switch contentType, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")); contentType { + case "application/protobuf", "application/x-protobuf": + buf, err := proto.Marshal(p) + if err != nil { + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + return + } + case "application/json": + buf, err := protojson.Marshal(p) + if err != nil { + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + return + } + default: + } + } + } + } + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + contentType, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")) + + accepts := strings.Split(r.Header.Get("Accept"), ",") + accept := accepts[0] + if accept == "*/*" || accept == "" { + if contentType != "" { + accept = contentType + } else { + accept = "application/json" + } + } + + w.Header().Set("Content-Type", accept) + + arg := &sourcecontextpb.SourceContext{} + if r.Method != http.MethodGet { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + + switch contentType { + case "application/protobuf", "application/x-protobuf": + if err := proto.Unmarshal(body, arg); err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + case "application/json": + if err := protojson.Unmarshal(body, arg); err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + default: + w.WriteHeader(http.StatusUnsupportedMediaType) + _, err := fmt.Fprintf(w, "Unsupported Content-Type: %s", contentType) + cb(ctx, w, r, nil, nil, err) + return + } + } + + n := len(interceptors) + chained := func(ctx context.Context, arg interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { + chainer := func(currentInter grpc.UnaryServerInterceptor, currentHandler grpc.UnaryHandler) grpc.UnaryHandler { + return func(currentCtx context.Context, currentReq interface{}) (interface{}, error) { + return currentInter(currentCtx, currentReq, info, currentHandler) + } + } + + chainedHandler := handler + for i := n - 1; i >= 0; i-- { + chainedHandler = chainer(interceptors[i], chainedHandler) + } + return chainedHandler(ctx, arg) + } + + info := &grpc.UnaryServerInfo{ + Server: h.srv, + FullMethod: "/knowntypes.KnownTypesService/SourceContext", + } + + handler := func(c context.Context, req interface{}) (interface{}, error) { + return h.srv.SourceContext(c, req.(*sourcecontextpb.SourceContext)) + } + + iret, err := chained(ctx, arg, info, handler) + if err != nil { + cb(ctx, w, r, arg, nil, err) + return + } + + ret, ok := iret.(*sourcecontextpb.SourceContext) + if !ok { + cb(ctx, w, r, arg, nil, fmt.Errorf("/knowntypes.KnownTypesService/SourceContext: interceptors have not return sourcecontextpb.SourceContext")) + return + } + + switch accept { + case "application/protobuf", "application/x-protobuf": + buf, err := proto.Marshal(ret) + if err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + case "application/json": + buf, err := protojson.Marshal(ret) + if err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + default: + w.WriteHeader(http.StatusUnsupportedMediaType) + _, err := fmt.Fprintf(w, "Unsupported Accept: %s", accept) + cb(ctx, w, r, arg, ret, err) + return + } + cb(ctx, w, r, arg, ret, nil) + }) +} + +// SourceContextWithName returns Service name, Method name and KnownTypesServiceHTTPService interface's SourceContext converted to http.HandlerFunc. +func (h *KnownTypesServiceHTTPConverter) SourceContextWithName(cb func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error), interceptors ...grpc.UnaryServerInterceptor) (string, string, http.HandlerFunc) { + return "KnownTypesService", "SourceContext", h.SourceContext(cb, interceptors...) +} + +// Struct returns KnownTypesServiceHTTPService interface's Struct converted to http.HandlerFunc. +func (h *KnownTypesServiceHTTPConverter) Struct(cb func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error), interceptors ...grpc.UnaryServerInterceptor) http.HandlerFunc { + if cb == nil { + cb = func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error) { + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + p := status.New(codes.Unknown, err.Error()).Proto() + switch contentType, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")); contentType { + case "application/protobuf", "application/x-protobuf": + buf, err := proto.Marshal(p) + if err != nil { + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + return + } + case "application/json": + buf, err := protojson.Marshal(p) + if err != nil { + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + return + } + default: + } + } + } + } + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + contentType, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")) + + accepts := strings.Split(r.Header.Get("Accept"), ",") + accept := accepts[0] + if accept == "*/*" || accept == "" { + if contentType != "" { + accept = contentType + } else { + accept = "application/json" + } + } + + w.Header().Set("Content-Type", accept) + + arg := &status.Struct{} + if r.Method != http.MethodGet { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + + switch contentType { + case "application/protobuf", "application/x-protobuf": + if err := proto.Unmarshal(body, arg); err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + case "application/json": + if err := protojson.Unmarshal(body, arg); err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + default: + w.WriteHeader(http.StatusUnsupportedMediaType) + _, err := fmt.Fprintf(w, "Unsupported Content-Type: %s", contentType) + cb(ctx, w, r, nil, nil, err) + return + } + } + + n := len(interceptors) + chained := func(ctx context.Context, arg interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { + chainer := func(currentInter grpc.UnaryServerInterceptor, currentHandler grpc.UnaryHandler) grpc.UnaryHandler { + return func(currentCtx context.Context, currentReq interface{}) (interface{}, error) { + return currentInter(currentCtx, currentReq, info, currentHandler) + } + } + + chainedHandler := handler + for i := n - 1; i >= 0; i-- { + chainedHandler = chainer(interceptors[i], chainedHandler) + } + return chainedHandler(ctx, arg) + } + + info := &grpc.UnaryServerInfo{ + Server: h.srv, + FullMethod: "/knowntypes.KnownTypesService/Struct", + } + + handler := func(c context.Context, req interface{}) (interface{}, error) { + return h.srv.Struct(c, req.(*status.Struct)) + } + + iret, err := chained(ctx, arg, info, handler) + if err != nil { + cb(ctx, w, r, arg, nil, err) + return + } + + ret, ok := iret.(*status.Struct) + if !ok { + cb(ctx, w, r, arg, nil, fmt.Errorf("/knowntypes.KnownTypesService/Struct: interceptors have not return status.Struct")) + return + } + + switch accept { + case "application/protobuf", "application/x-protobuf": + buf, err := proto.Marshal(ret) + if err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + case "application/json": + buf, err := protojson.Marshal(ret) + if err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + default: + w.WriteHeader(http.StatusUnsupportedMediaType) + _, err := fmt.Fprintf(w, "Unsupported Accept: %s", accept) + cb(ctx, w, r, arg, ret, err) + return + } + cb(ctx, w, r, arg, ret, nil) + }) +} + +// StructWithName returns Service name, Method name and KnownTypesServiceHTTPService interface's Struct converted to http.HandlerFunc. +func (h *KnownTypesServiceHTTPConverter) StructWithName(cb func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error), interceptors ...grpc.UnaryServerInterceptor) (string, string, http.HandlerFunc) { + return "KnownTypesService", "Struct", h.Struct(cb, interceptors...) +} + +// Timestamp returns KnownTypesServiceHTTPService interface's Timestamp converted to http.HandlerFunc. +func (h *KnownTypesServiceHTTPConverter) Timestamp(cb func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error), interceptors ...grpc.UnaryServerInterceptor) http.HandlerFunc { + if cb == nil { + cb = func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error) { + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + p := status.New(codes.Unknown, err.Error()).Proto() + switch contentType, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")); contentType { + case "application/protobuf", "application/x-protobuf": + buf, err := proto.Marshal(p) + if err != nil { + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + return + } + case "application/json": + buf, err := protojson.Marshal(p) + if err != nil { + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + return + } + default: + } + } + } + } + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + contentType, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")) + + accepts := strings.Split(r.Header.Get("Accept"), ",") + accept := accepts[0] + if accept == "*/*" || accept == "" { + if contentType != "" { + accept = contentType + } else { + accept = "application/json" + } + } + + w.Header().Set("Content-Type", accept) + + arg := ×tamppb.Timestamp{} + if r.Method != http.MethodGet { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + + switch contentType { + case "application/protobuf", "application/x-protobuf": + if err := proto.Unmarshal(body, arg); err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + case "application/json": + if err := protojson.Unmarshal(body, arg); err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + default: + w.WriteHeader(http.StatusUnsupportedMediaType) + _, err := fmt.Fprintf(w, "Unsupported Content-Type: %s", contentType) + cb(ctx, w, r, nil, nil, err) + return + } + } + + n := len(interceptors) + chained := func(ctx context.Context, arg interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { + chainer := func(currentInter grpc.UnaryServerInterceptor, currentHandler grpc.UnaryHandler) grpc.UnaryHandler { + return func(currentCtx context.Context, currentReq interface{}) (interface{}, error) { + return currentInter(currentCtx, currentReq, info, currentHandler) + } + } + + chainedHandler := handler + for i := n - 1; i >= 0; i-- { + chainedHandler = chainer(interceptors[i], chainedHandler) + } + return chainedHandler(ctx, arg) + } + + info := &grpc.UnaryServerInfo{ + Server: h.srv, + FullMethod: "/knowntypes.KnownTypesService/Timestamp", + } + + handler := func(c context.Context, req interface{}) (interface{}, error) { + return h.srv.Timestamp(c, req.(*timestamppb.Timestamp)) + } + + iret, err := chained(ctx, arg, info, handler) + if err != nil { + cb(ctx, w, r, arg, nil, err) + return + } + + ret, ok := iret.(*timestamppb.Timestamp) + if !ok { + cb(ctx, w, r, arg, nil, fmt.Errorf("/knowntypes.KnownTypesService/Timestamp: interceptors have not return timestamppb.Timestamp")) + return + } + + switch accept { + case "application/protobuf", "application/x-protobuf": + buf, err := proto.Marshal(ret) + if err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + case "application/json": + buf, err := protojson.Marshal(ret) + if err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + default: + w.WriteHeader(http.StatusUnsupportedMediaType) + _, err := fmt.Fprintf(w, "Unsupported Accept: %s", accept) + cb(ctx, w, r, arg, ret, err) + return + } + cb(ctx, w, r, arg, ret, nil) + }) +} + +// TimestampWithName returns Service name, Method name and KnownTypesServiceHTTPService interface's Timestamp converted to http.HandlerFunc. +func (h *KnownTypesServiceHTTPConverter) TimestampWithName(cb func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error), interceptors ...grpc.UnaryServerInterceptor) (string, string, http.HandlerFunc) { + return "KnownTypesService", "Timestamp", h.Timestamp(cb, interceptors...) +} + +// Type returns KnownTypesServiceHTTPService interface's Type converted to http.HandlerFunc. +func (h *KnownTypesServiceHTTPConverter) Type(cb func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error), interceptors ...grpc.UnaryServerInterceptor) http.HandlerFunc { + if cb == nil { + cb = func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error) { + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + p := status.New(codes.Unknown, err.Error()).Proto() + switch contentType, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")); contentType { + case "application/protobuf", "application/x-protobuf": + buf, err := proto.Marshal(p) + if err != nil { + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + return + } + case "application/json": + buf, err := protojson.Marshal(p) + if err != nil { + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + return + } + default: + } + } + } + } + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + contentType, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")) + + accepts := strings.Split(r.Header.Get("Accept"), ",") + accept := accepts[0] + if accept == "*/*" || accept == "" { + if contentType != "" { + accept = contentType + } else { + accept = "application/json" + } + } + + w.Header().Set("Content-Type", accept) + + arg := &typepb.Type{} + if r.Method != http.MethodGet { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + + switch contentType { + case "application/protobuf", "application/x-protobuf": + if err := proto.Unmarshal(body, arg); err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + case "application/json": + if err := protojson.Unmarshal(body, arg); err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + default: + w.WriteHeader(http.StatusUnsupportedMediaType) + _, err := fmt.Fprintf(w, "Unsupported Content-Type: %s", contentType) + cb(ctx, w, r, nil, nil, err) + return + } + } + + n := len(interceptors) + chained := func(ctx context.Context, arg interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { + chainer := func(currentInter grpc.UnaryServerInterceptor, currentHandler grpc.UnaryHandler) grpc.UnaryHandler { + return func(currentCtx context.Context, currentReq interface{}) (interface{}, error) { + return currentInter(currentCtx, currentReq, info, currentHandler) + } + } + + chainedHandler := handler + for i := n - 1; i >= 0; i-- { + chainedHandler = chainer(interceptors[i], chainedHandler) + } + return chainedHandler(ctx, arg) + } + + info := &grpc.UnaryServerInfo{ + Server: h.srv, + FullMethod: "/knowntypes.KnownTypesService/Type", + } + + handler := func(c context.Context, req interface{}) (interface{}, error) { + return h.srv.Type(c, req.(*typepb.Type)) + } + + iret, err := chained(ctx, arg, info, handler) + if err != nil { + cb(ctx, w, r, arg, nil, err) + return + } + + ret, ok := iret.(*typepb.Type) + if !ok { + cb(ctx, w, r, arg, nil, fmt.Errorf("/knowntypes.KnownTypesService/Type: interceptors have not return typepb.Type")) + return + } + + switch accept { + case "application/protobuf", "application/x-protobuf": + buf, err := proto.Marshal(ret) + if err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + case "application/json": + buf, err := protojson.Marshal(ret) + if err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + default: + w.WriteHeader(http.StatusUnsupportedMediaType) + _, err := fmt.Fprintf(w, "Unsupported Accept: %s", accept) + cb(ctx, w, r, arg, ret, err) + return + } + cb(ctx, w, r, arg, ret, nil) + }) +} + +// TypeWithName returns Service name, Method name and KnownTypesServiceHTTPService interface's Type converted to http.HandlerFunc. +func (h *KnownTypesServiceHTTPConverter) TypeWithName(cb func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error), interceptors ...grpc.UnaryServerInterceptor) (string, string, http.HandlerFunc) { + return "KnownTypesService", "Type", h.Type(cb, interceptors...) +} + +// Wrappers returns KnownTypesServiceHTTPService interface's Wrappers converted to http.HandlerFunc. +func (h *KnownTypesServiceHTTPConverter) Wrappers(cb func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error), interceptors ...grpc.UnaryServerInterceptor) http.HandlerFunc { + if cb == nil { + cb = func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error) { + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + p := status.New(codes.Unknown, err.Error()).Proto() + switch contentType, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")); contentType { + case "application/protobuf", "application/x-protobuf": + buf, err := proto.Marshal(p) + if err != nil { + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + return + } + case "application/json": + buf, err := protojson.Marshal(p) + if err != nil { + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + return + } + default: + } + } + } + } + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + contentType, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")) + + accepts := strings.Split(r.Header.Get("Accept"), ",") + accept := accepts[0] + if accept == "*/*" || accept == "" { + if contentType != "" { + accept = contentType + } else { + accept = "application/json" + } + } + + w.Header().Set("Content-Type", accept) + + arg := &wrapperspb.BoolValue{} + if r.Method != http.MethodGet { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + + switch contentType { + case "application/protobuf", "application/x-protobuf": + if err := proto.Unmarshal(body, arg); err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + case "application/json": + if err := protojson.Unmarshal(body, arg); err != nil { + cb(ctx, w, r, nil, nil, err) + return + } + default: + w.WriteHeader(http.StatusUnsupportedMediaType) + _, err := fmt.Fprintf(w, "Unsupported Content-Type: %s", contentType) + cb(ctx, w, r, nil, nil, err) + return + } + } + + n := len(interceptors) + chained := func(ctx context.Context, arg interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { + chainer := func(currentInter grpc.UnaryServerInterceptor, currentHandler grpc.UnaryHandler) grpc.UnaryHandler { + return func(currentCtx context.Context, currentReq interface{}) (interface{}, error) { + return currentInter(currentCtx, currentReq, info, currentHandler) + } + } + + chainedHandler := handler + for i := n - 1; i >= 0; i-- { + chainedHandler = chainer(interceptors[i], chainedHandler) + } + return chainedHandler(ctx, arg) + } + + info := &grpc.UnaryServerInfo{ + Server: h.srv, + FullMethod: "/knowntypes.KnownTypesService/Wrappers", + } + + handler := func(c context.Context, req interface{}) (interface{}, error) { + return h.srv.Wrappers(c, req.(*wrapperspb.BoolValue)) + } + + iret, err := chained(ctx, arg, info, handler) + if err != nil { + cb(ctx, w, r, arg, nil, err) + return + } + + ret, ok := iret.(*wrapperspb.BoolValue) + if !ok { + cb(ctx, w, r, arg, nil, fmt.Errorf("/knowntypes.KnownTypesService/Wrappers: interceptors have not return wrapperspb.BoolValue")) + return + } + + switch accept { + case "application/protobuf", "application/x-protobuf": + buf, err := proto.Marshal(ret) + if err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + case "application/json": + buf, err := protojson.Marshal(ret) + if err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil { + cb(ctx, w, r, arg, ret, err) + return + } + default: + w.WriteHeader(http.StatusUnsupportedMediaType) + _, err := fmt.Fprintf(w, "Unsupported Accept: %s", accept) + cb(ctx, w, r, arg, ret, err) + return + } + cb(ctx, w, r, arg, ret, nil) + }) +} + +// WrappersWithName returns Service name, Method name and KnownTypesServiceHTTPService interface's Wrappers converted to http.HandlerFunc. +func (h *KnownTypesServiceHTTPConverter) WrappersWithName(cb func(ctx context.Context, w http.ResponseWriter, r *http.Request, arg, ret proto.Message, err error), interceptors ...grpc.UnaryServerInterceptor) (string, string, http.HandlerFunc) { + return "KnownTypesService", "Wrappers", h.Wrappers(cb, interceptors...) +} From 5778b3577592323605943e967497b75b7a082d07 Mon Sep 17 00:00:00 2001 From: nametake Date: Tue, 23 Nov 2021 16:12:44 +0900 Subject: [PATCH 12/12] Updated go version --- .circleci/config.yml | 2 +- .github/workflows/go.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 7372ff1..d745325 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,7 +3,7 @@ version: 2 jobs: build: docker: - - image: circleci/golang:1.12 + - image: circleci/golang:1.17 environment: GO111MODULE: 'on' PROTOC_VERSION: '3.5.1' diff --git a/.github/workflows/go.yaml b/.github/workflows/go.yaml index f94cfd3..e24a146 100644 --- a/.github/workflows/go.yaml +++ b/.github/workflows/go.yaml @@ -10,7 +10,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v1 with: - version: 1.13 + version: 1.17 - name: Checkout uses: actions/checkout@master - name: Setup protoc