diff --git a/examples/flow/output/test/test_grpc_js.js b/examples/flow/output/test/test_grpc_js.js index e8a87e2..b61f1c7 100644 --- a/examples/flow/output/test/test_grpc_js.js +++ b/examples/flow/output/test/test_grpc_js.js @@ -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; - o?: Array; - p?: Array; - q?: Array; - r?: Array; - s?: - | 'ELEMENT_C' - | 'ELEMENT_D' - ; + + a?: string; + + b?: number; + + c?: number; + + d?: number; + + e?: number; + + n?: Array; + + o?: Array; + + p?: Array; + + q?: Array; + + r?: Array; + + u?: TestEnum; + + v?: TestMessage$TestNestedEnum; + + w?: Array; + |}; + + + + + 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') diff --git a/examples/flow/protos/test.proto b/examples/flow/protos/test.proto index e15ee88..66ea9ac 100644 --- a/examples/flow/protos/test.proto +++ b/examples/flow/protos/test.proto @@ -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; } diff --git a/examples/flow/templates/{{.File.Package}}/{{.File.Package}}_grpc_js.js.tmpl b/examples/flow/templates/{{.File.Package}}/{{.File.Package}}_grpc_js.js.tmpl index 8acd68d..5e0ba9e 100644 --- a/examples/flow/templates/{{.File.Package}}/{{.File.Package}}_grpc_js.js.tmpl +++ b/examples/flow/templates/{{.File.Package}}/{{.File.Package}}_grpc_js.js.tmpl @@ -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}})) { diff --git a/helpers.go b/helpers.go index 038f3ee..fa0a983 100644 --- a/helpers.go +++ b/helpers.go @@ -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") 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)