Skip to content

Commit

Permalink
Enum support (#23)
Browse files Browse the repository at this point in the history
* Moving images to github repo, because they are not always displaying when visiting the README

* Adds enum support.

* Replacing deprecated code.

* Updating go.mod to latest commit on gnostic master instead of latest tag.
  • Loading branch information
LorenzHW committed May 27, 2020
1 parent b47c208 commit a3a34ce
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ OpenAPI descriptions are read and processed with
gnostic plugin.

## High level overview:
![High Level Overview](https://raw.githubusercontent.com/googleapis/gnostic-grpc/master/high_level_overview.png "High Level Overview")
![High Level Overview](https://raw.githubusercontent.com/googleapis/gnostic-grpc/master/examples/images/high-level-overview.png "High Level Overview")

Under the hood the plugin first creates a FileDescriptorSet (`bookststore.descr`) from the input
data. Then [protoreflect](https://github.com/jhump/protoreflect/) is used to print the output file.
Expand Down
4 changes: 2 additions & 2 deletions examples/bookstore/bookstore.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ package bookstore;

import "google/api/annotations.proto";

import "google/protobuf/empty.proto";

import "google/protobuf/descriptor.proto";

import "google/protobuf/empty.proto";

message Book {
string author = 1;

Expand Down
2 changes: 1 addition & 1 deletion examples/end-to-end-grpc-gateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ OpenAPI description.

#### What we will build:

![alt text](https://drive.google.com/uc?export=view&id=118eI8Tb88gJF47nclHbLxqOS1N_ygt4o "gRPC with Transcoding")
![alt text](https://raw.githubusercontent.com/googleapis/gnostic-grpc/master/examples/images/end-to-end-grpc-gateway.png "gRPC with Transcoding")

This tutorial has six steps:

Expand Down
2 changes: 1 addition & 1 deletion examples/end-to-end/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ OpenAPI description.


#### What we will build:
![alt text](https://drive.google.com/uc?export=view&id=1hXqL-KYaa-38p_nyfC2Moycxv0eV2Ya6 "gRPC with Transcoding")
![alt text](https://raw.githubusercontent.com/googleapis/gnostic-grpc/master/examples/images/end-to-end-envoy.png "gRPC with Transcoding")

#### Prerequisite
Install [gnostic](https://github.com/googleapis/gnostic), [gnostic-grpc](https://github.com/googleapis/gnostic-grpc),
Expand Down
Binary file added examples/images/end-to-end-envoy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/images/end-to-end-grpc-gateway.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
38 changes: 32 additions & 6 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func buildDependencies(fdSet *dpb.FileDescriptorSet) {
// 2. Problem: The name is set wrong.
// 3. Problem: google/api/annotations.proto has a dependency to google/protobuf/descriptor.proto.
http := annotations.Http{}
fd, _ := descriptor.ForMessage(&http)
fd, _ := descriptor.MessageDescriptorProto(&http)

extensionName := "http"
n := "google/api/annotations.proto"
Expand All @@ -200,8 +200,8 @@ func buildDependencies(fdSet *dpb.FileDescriptorSet) {
// Build other required dependencies
e := empty.Empty{}
fdp := dpb.DescriptorProto{}
fd2, _ := descriptor.ForMessage(&e)
fd3, _ := descriptor.ForMessage(&fdp)
fd2, _ := descriptor.MessageDescriptorProto(&e)
fd3, _ := descriptor.MessageDescriptorProto(&fdp)
dependencies := []*dpb.FileDescriptorProto{fd, fd2, fd3}

// According to the documentation of protoReflect.CreateFileDescriptorFromSet the file I want to print
Expand All @@ -226,10 +226,14 @@ func buildMessagesFromTypes(descr *dpb.FileDescriptorProto, renderer *Renderer)
validateQueryParameter(f)
}
}
if f.EnumValues != nil {
message.EnumType = append(message.EnumType, buildEnumDescriptorProto(f))
}

ctr := int32(i + 1)
fieldDescriptor := &dpb.FieldDescriptorProto{Number: &ctr}
fieldDescriptor.Name = &f.FieldName
fieldDescriptor.Type = getFieldDescriptorType(f.NativeType)
fieldDescriptor.Type = getFieldDescriptorType(f.NativeType, f.EnumValues)
setFieldDescriptorLabel(fieldDescriptor, f)
setFieldDescriptorTypeName(fieldDescriptor, f, renderer.Package)

Expand Down Expand Up @@ -291,6 +295,21 @@ func buildServiceFromMethods(descr *dpb.FileDescriptorProto, renderer *Renderer)
return nil
}

// buildEnumDescriptorProto builds the necessary descriptor to render a enum. (https://developers.google.com/protocol-buffers/docs/proto3#enum)
func buildEnumDescriptorProto(f *surface_v1.Field) *dpb.EnumDescriptorProto {
enumDescriptor := &dpb.EnumDescriptorProto{Name: &f.NativeType}
for enumCtr, value := range f.EnumValues {
num := int32(enumCtr)
name := strings.ToUpper(value)
valueDescriptor := &dpb.EnumValueDescriptorProto{
Name: &name,
Number: &num,
}
enumDescriptor.Value = append(enumDescriptor.Value, valueDescriptor)
}
return enumDescriptor
}

// buildMapDescriptorProto builds the necessary descriptor to render a map. (https://developers.google.com/protocol-buffers/docs/proto3#maps)
// A map is represented as nested message with two fields: 'key', 'value' and the Options set accordingly.
func buildMapDescriptorProto(field *surface_v1.Field) *dpb.DescriptorProto {
Expand Down Expand Up @@ -323,7 +342,7 @@ func buildKeyValueFields(field *surface_v1.Field) []*dpb.FieldDescriptorProto {
Name: &v,
Number: &n2,
Label: &l,
Type: getFieldDescriptorType(valueType),
Type: getFieldDescriptorType(valueType, field.EnumValues),
TypeName: getTypeNameForMapValueType(valueType),
}
return []*dpb.FieldDescriptorProto{keyField, valueField}
Expand Down Expand Up @@ -386,6 +405,9 @@ func setFieldDescriptorTypeName(fd *dpb.FieldDescriptorProto, f *surface_v1.Fiel
}
fd.TypeName = &typeName
}
if *fd.Type == dpb.FieldDescriptorProto_TYPE_ENUM {
fd.TypeName = &f.NativeType
}
}

// getRequestBodyForRequestParameters finds the corresponding surface model type for 'name' and returns the name of the
Expand Down Expand Up @@ -463,11 +485,15 @@ func getTypeNameForMapValueType(valueType string) *string {

// getFieldDescriptorType returns a field descriptor type for the given 'nativeType'. If it is not a scalar type
// then we have a reference to another type which will get rendered as a message.
func getFieldDescriptorType(nativeType string) *dpb.FieldDescriptorProto_Type {
func getFieldDescriptorType(nativeType string, enumValues []string) *dpb.FieldDescriptorProto_Type {
protoType := dpb.FieldDescriptorProto_TYPE_MESSAGE
if protoType, ok := protoBufScalarTypes[nativeType]; ok {
return &protoType
}
if enumValues != nil {
protoType := dpb.FieldDescriptorProto_TYPE_ENUM
return &protoType
}
return &protoType
}

Expand Down
4 changes: 4 additions & 0 deletions generator/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (language *ProtoLanguageModel) Prepare(model *surface_v1.Model, inputDocume
for _, f := range t.Fields {
f.FieldName = protoFieldName(f.Name, f.Type)
f.NativeType = findNativeType(f.Type, f.Format)

if f.EnumValues != nil {
f.NativeType = strings.Title(f.Name)
}
}
}

Expand Down
18 changes: 16 additions & 2 deletions generator/testfiles/goldstandard/parameters.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,29 @@ message TestParameterQueryParameters {
}

message TestParameterQueryEnumParameters {
repeated int64 param2 = 1;
repeated Param2 param2 = 1;

enum Param2 {
DINGO = 0;

HUSKY = 1;

RETRIEVER = 2;
}
}

message TestParameterPathParameters {
string param3 = 1;
}

message TestParameterPathEnumParameters {
int64 param4 = 1;
Param4 param4 = 1;

enum Param4 {
DINGO = 0;

HUSKY = 1;
}
}

message TestParameterMultiplePathParameters {
Expand Down
24 changes: 11 additions & 13 deletions generator/testfiles/parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ paths:
responses:
200:
description: success
/testParameterQueryEnum: #TODO: Does not work
/testParameterQueryEnum: #TODO: Generates invalid proto for integer enums
get:
operationId: testParameterQueryEnum
parameters:
Expand All @@ -35,13 +35,12 @@ paths:
schema:
type: array
items:
type: integer
format: int64
type: string
enum:
- 1337
- 1338
- 1339
default: 1338
- Dingo
- Husky
- Retriever
default: Husky
responses:
200:
description: success
Expand All @@ -56,19 +55,18 @@ paths:
responses:
200:
description: success
/testParameterPathEnum/{param1}: #TODO: Does not work
/testParameterPathEnum/{param1}: #TODO: Generates invalid proto for integer enums
get:
operationId: testParameterPathEnum
parameters:
- name: param4
in: path
schema:
type: integer
format: int64
type: string
enum:
- 1337
- 1338
default: 1338
- Dingo
- Husky
default: Dingo
responses:
200:
description: success
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.13

require (
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/golang/protobuf v1.3.4
github.com/googleapis/gnostic v0.4.1
github.com/golang/protobuf v1.4.2
github.com/googleapis/gnostic v0.4.2-0.20200520192801-89741bb7d0e0
github.com/grpc-ecosystem/grpc-gateway v1.12.2
github.com/jhump/protoreflect v1.6.0
golang.org/x/net v0.0.0-20191002035440-2ec189313ef0
Expand Down
34 changes: 30 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,33 @@ github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/protobuf v1.3.4 h1:87PNWwrRvUSnqS4dlcBU/ftvOIBep4sYuBLlh6rX2wk=
github.com/golang/protobuf v1.3.4/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 h1:oOuy+ugB+P/kBdUnG5QaMXSIyJ1q38wWSojYCb3z5VQ=
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
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/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/googleapis/gnostic v0.4.1 h1:DLJCy1n/vrD4HPjOvYcT8aYQXpPIzoRZONaYwyycI+I=
github.com/googleapis/gnostic v0.4.1/go.mod h1:LRhVm6pbyptWbWbuZ38d1eyptfvIytN3ir6b65WBswg=
github.com/googleapis/gnostic v0.4.2-0.20200519195951-57a06a8ff3b3 h1:lH+GwKj0oEtF26nCnebhO7oS6LdSISSI9mQKOelDG3o=
github.com/googleapis/gnostic v0.4.2-0.20200519195951-57a06a8ff3b3/go.mod h1:66LN4oFEjss7jBuyFHfz9jIXKCPM+TQnz5GybKvhVQI=
github.com/googleapis/gnostic v0.4.2-0.20200520192801-89741bb7d0e0 h1:qM69YZMWsPLR3GyzAcQPjwONfVC+eqKtHOLc2hWyJvs=
github.com/googleapis/gnostic v0.4.2-0.20200520192801-89741bb7d0e0/go.mod h1:66LN4oFEjss7jBuyFHfz9jIXKCPM+TQnz5GybKvhVQI=
github.com/grpc-ecosystem/grpc-gateway v1.12.2 h1:D0EVSTwQoQOyfY35QNSuPJA4jpZRtkoGYWQMB7XNg5o=
github.com/grpc-ecosystem/grpc-gateway v1.12.2/go.mod h1:8XEsbTttt/W+VvjtQhLACqCisSPWTxCZ7sBRjU6iH9c=
github.com/jhump/protoreflect v1.6.0 h1:h5jfMVslIg6l29nsMs0D8Wj17RDVdNYti0vDN/PZZoE=
github.com/jhump/protoreflect v1.6.0/go.mod h1:eaTn3RZAmMBcV0fifFvlm6VHNz3wSkYyXYWUh7ymB74=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
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=
Expand Down Expand Up @@ -56,14 +72,14 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm
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 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/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-20170818010345-ee236bd376b0/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
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-20190927181202-20e1ac93f88c/go.mod h1:IbNlFCBrqXvoKpeg0TB2l7cyZUmoaFKYIwrEpbDKLA8=
google.golang.org/genproto v0.0.0-20200128133413-58ce757ed39b h1:c8OBoXP3kTbDWWB/oVE3FkR851p4iZ3MPadz7zXEIPU=
google.golang.org/genproto v0.0.0-20200128133413-58ce757ed39b/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc=
google.golang.org/genproto v0.0.0-20200311144346-b662892dd51b h1:IXPzGf8J51hBQirC+OIHbIlTuVYOMarft+Wvi+qDzmg=
google.golang.org/genproto v0.0.0-20200311144346-b662892dd51b/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
google.golang.org/grpc v1.8.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=
Expand All @@ -72,8 +88,18 @@ google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZi
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.24.0 h1:vb/1TCsVn3DcJlQ0Gs1yB1pKI6Do2/QNwxdKqmc/b0s=
google.golang.org/grpc v1.24.0/go.mod h1:XDChyiUovWa60DnaeDeZmSW86xtLtjtZbwvSiRnRtcA=
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=
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 h1:qdOKuR/EIArgaWNjetjgTzgVTAZ+S/WXVrq9HW9zimw=
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down

0 comments on commit a3a34ce

Please sign in to comment.