Merge pull request #41 from moul/feature/nested-types

Feature/nested types
This commit is contained in:
Manfred Touron 2017-01-10 14:07:17 +01:00 committed by GitHub
commit 5fa1ea5cba
4 changed files with 181 additions and 44 deletions

View File

@ -4,65 +4,166 @@
import grpc from 'grpc' import grpc from 'grpc'
import pbFile from './pbFile.js' import pbFile from './pbFile.js'
export type TestEnum =
| 'ELEMENT_A' export type TestEnum = {|
| 'ELEMENT_B'
; ELEMENT_A?: 0;
ELEMENT_B?: 1;
|};
export type TestMessage$TestNestedEnum = {|
ELEMENT_C?: 0;
ELEMENT_D?: 1;
|};
export type TestMessage$TestNestedMessage = {|
s?: string;
t?: number;
|};
export type TestMessage = {| export type TestMessage = {|
a?: string; a?: string;
b?: number; b?: number;
c?: number; c?: number;
d?: number; d?: number;
e?: number; e?: number;
n?: Array<string>; n?: Array<string>;
o?: Array<number>; o?: Array<number>;
p?: Array<number>; p?: Array<number>;
q?: Array<number>; q?: Array<number>;
r?: Array<number>; r?: Array<number>;
s?:
| 'ELEMENT_C' u?: TestEnum;
| 'ELEMENT_D'
; v?: TestMessage$TestNestedEnum;
w?: Array<TestMessage$TestNestedMessage>;
|}; |};
export type TestNoStreamRequest = {| export type TestNoStreamRequest = {|
message?: TestMessage; message?: TestMessage;
|}; |};
export type TestNoStreamReply = {| export type TestNoStreamReply = {|
message?: TestMessage; message?: TestMessage;
err_msg?: string; err_msg?: string;
|}; |};
export type TestStreamRequestRequest = {| export type TestStreamRequestRequest = {|
message?: TestMessage; message?: TestMessage;
|}; |};
export type TestStreamRequestReply = {| export type TestStreamRequestReply = {|
message?: TestMessage; message?: TestMessage;
err_msg?: string; err_msg?: string;
|}; |};
export type TestStreamReplyRequest = {| export type TestStreamReplyRequest = {|
message?: TestMessage; message?: TestMessage;
|}; |};
export type TestStreamReplyReply = {| export type TestStreamReplyReply = {|
message?: TestMessage; message?: TestMessage;
err_msg?: string; err_msg?: string;
|}; |};
export type TestStreamBothRequest = {| export type TestStreamBothRequest = {|
message?: TestMessage; message?: TestMessage;
|}; |};
export type TestStreamBothReply = {| export type TestStreamBothReply = {|
message?: TestMessage; message?: TestMessage;
err_msg?: string; err_msg?: string;
|}; |};
function serialize_test_TestNoStreamRequest(arg: TestNoStreamRequest) { function serialize_test_TestNoStreamRequest(arg: TestNoStreamRequest) {
if (!(arg instanceof pbFile.TestNoStreamRequest)) { if (!(arg instanceof pbFile.TestNoStreamRequest)) {
throw new Error('Expected argument of type TestNoStreamRequest') throw new Error('Expected argument of type TestNoStreamRequest')

View File

@ -27,10 +27,19 @@ message TestMessage {
repeated int64 p = 16; repeated int64 p = 16;
repeated float q = 17; repeated float q = 17;
repeated double r = 18; repeated double r = 18;
enum s {
message TestNestedMessage {
string s = 1;
int32 t = 2;
}
enum TestNestedEnum {
ELEMENT_C = 0; ELEMENT_C = 0;
ELEMENT_D = 1; ELEMENT_D = 1;
} }
TestEnum u = 19;
TestNestedEnum v = 20;
repeated TestNestedMessage w = 21;
} }
message TestNoStreamRequest { TestMessage message = 1; } message TestNoStreamRequest { TestMessage message = 1; }

View File

@ -3,18 +3,40 @@
{{$Package:=.File.Package}} {{$Package:=.File.Package}}
import grpc from 'grpc' import grpc from 'grpc'
import pbFile from './pbFile.js' import pbFile from './pbFile.js'
{{range .File.EnumType}} {{range .File.EnumType}}
export type {{.Name}} = {{range .Value}} export type {{.Name}} = {|
| '{{.Name}}'{{end}} {{range .Value}}
;{{end}} {{.Name}}?: {{.Number}};
{{range .File.MessageType}} {{end}}
export type {{.Name}} = {|{{range .Field}}
{{.Name}}?: {{. | jsType}};{{end}}{{range .EnumType}}
{{.Name}}?:{{range .Value}}
| '{{.Name}}'{{end}}
;{{end}}
|}; |};
{{end}} {{end}}
{{range .File.MessageType}}
{{$MessageType := .Name}}
{{range .EnumType}}
export type {{$MessageType}}${{.Name}} = {|
{{range .Value}}
{{.Name}}?: {{.Number}};
{{end}}
|};
{{end}}
{{range .NestedType}}
export type {{$MessageType}}${{.Name}} = {|
{{range .Field}}
{{.Name}}?: {{. | jsType}};
{{end}}
|};
{{end}}
export type {{.Name}} = {|
{{range .Field}}
{{.Name}}?: {{. | jsType}};
{{end}}
|};
{{end}}
{{range .File.Service}}{{range .Method}} {{range .File.Service}}{{range .Method}}
function serialize_{{$Package}}_{{.InputType | shortType}}(arg: {{.InputType | shortType}}) { function serialize_{{$Package}}_{{.InputType | shortType}}(arg: {{.InputType | shortType}}) {
if (!(arg instanceof pbFile.{{.InputType | shortType}})) { if (!(arg instanceof pbFile.{{.InputType | shortType}})) {

View File

@ -148,8 +148,9 @@ func jsType(f *descriptor.FieldDescriptorProto) string {
} }
switch *f.Type { switch *f.Type {
case descriptor.FieldDescriptorProto_TYPE_MESSAGE: case descriptor.FieldDescriptorProto_TYPE_MESSAGE,
return fmt.Sprintf(template, shortType(*f.TypeName)) descriptor.FieldDescriptorProto_TYPE_ENUM:
return fmt.Sprintf(template, namespacedFlowType(*f.TypeName))
case descriptor.FieldDescriptorProto_TYPE_DOUBLE, case descriptor.FieldDescriptorProto_TYPE_DOUBLE,
descriptor.FieldDescriptorProto_TYPE_FLOAT, descriptor.FieldDescriptorProto_TYPE_FLOAT,
descriptor.FieldDescriptorProto_TYPE_INT64, descriptor.FieldDescriptorProto_TYPE_INT64,
@ -169,8 +170,6 @@ func jsType(f *descriptor.FieldDescriptorProto) string {
return fmt.Sprintf(template, "Array<number>") return fmt.Sprintf(template, "Array<number>")
case descriptor.FieldDescriptorProto_TYPE_STRING: case descriptor.FieldDescriptorProto_TYPE_STRING:
return fmt.Sprintf(template, "string") return fmt.Sprintf(template, "string")
case descriptor.FieldDescriptorProto_TYPE_ENUM:
return fmt.Sprintf(template, "Object")
default: default:
return fmt.Sprintf(template, "any") return fmt.Sprintf(template, "any")
} }
@ -181,6 +180,12 @@ func shortType(s string) string {
return t[len(t)-1] return t[len(t)-1]
} }
func namespacedFlowType(s string) string {
trimmed := strings.TrimLeft(s, ".")
splitted := strings.Split(trimmed, ".")
return strings.Join(splitted[1:], "$")
}
func httpPath(m *descriptor.MethodDescriptorProto) string { func httpPath(m *descriptor.MethodDescriptorProto) string {
ext, err := proto.GetExtension(m.Options, options.E_Http) ext, err := proto.GetExtension(m.Options, options.E_Http)