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

View File

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

View File

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

View File

@ -148,8 +148,9 @@ func jsType(f *descriptor.FieldDescriptorProto) string {
}
switch *f.Type {
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
return fmt.Sprintf(template, shortType(*f.TypeName))
case descriptor.FieldDescriptorProto_TYPE_MESSAGE,
descriptor.FieldDescriptorProto_TYPE_ENUM:
return fmt.Sprintf(template, namespacedFlowType(*f.TypeName))
case descriptor.FieldDescriptorProto_TYPE_DOUBLE,
descriptor.FieldDescriptorProto_TYPE_FLOAT,
descriptor.FieldDescriptorProto_TYPE_INT64,
@ -169,8 +170,6 @@ func jsType(f *descriptor.FieldDescriptorProto) string {
return fmt.Sprintf(template, "Array<number>")
case descriptor.FieldDescriptorProto_TYPE_STRING:
return fmt.Sprintf(template, "string")
case descriptor.FieldDescriptorProto_TYPE_ENUM:
return fmt.Sprintf(template, "Object")
default:
return fmt.Sprintf(template, "any")
}
@ -181,6 +180,12 @@ func shortType(s string) string {
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 {
ext, err := proto.GetExtension(m.Options, options.E_Http)