From 66e9d20e1f24d406548a5141201355d310f0e31a Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Tue, 10 Jan 2017 11:22:18 +0100 Subject: [PATCH 1/4] Basic support for nested Enums / Messages --- examples/flow/output/test/test_grpc_js.js | 168 ++++++++++++++---- examples/flow/protos/test.proto | 6 +- .../{{.File.Package}}_grpc_js.js.tmpl | 45 ++++- 3 files changed, 179 insertions(+), 40 deletions(-) diff --git a/examples/flow/output/test/test_grpc_js.js b/examples/flow/output/test/test_grpc_js.js index e8a87e2..db9204d 100644 --- a/examples/flow/output/test/test_grpc_js.js +++ b/examples/flow/output/test/test_grpc_js.js @@ -4,65 +4,173 @@ 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 TestNestedEnum = {| + + ELEMENT_C?: 0; + + ELEMENT_D?: 1; + +|}; + + + +export type 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; + + + TestNestedMessage?: TestNestedMessage; + + + TestNestedEnum?: TestNestedEnum; + |}; + + + + 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..7458188 100644 --- a/examples/flow/protos/test.proto +++ b/examples/flow/protos/test.proto @@ -27,7 +27,11 @@ 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; } 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..fc99da1 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,45 @@ {{$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}} +{{range .EnumType}} +export type {{.Name}} = {| + {{range .Value}} + {{.Name}}?: {{.Number}}; + {{end}} +|}; +{{end}} + +{{range .NestedType}} +export type {{.Name}} = {| + {{range .Field}} + {{.Name}}?: {{. | jsType}}; + {{end}} +|}; +{{end}} + +export type {{.Name}} = {| + {{range .Field}} + {{.Name}}?: {{. | jsType}}; + {{end}} + {{range .NestedType}} + {{.Name}}?: {{.Name}}; + {{end}} + {{range .EnumType}} + {{.Name}}?: {{.Name}}; + {{end}} +|}; +{{end}} + {{range .File.Service}}{{range .Method}} function serialize_{{$Package}}_{{.InputType | shortType}}(arg: {{.InputType | shortType}}) { if (!(arg instanceof pbFile.{{.InputType | shortType}})) { From acbd1bf87b8e94c2ce95fb5fa9e54a2bf3bd0f34 Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Tue, 10 Jan 2017 11:40:48 +0100 Subject: [PATCH 2/4] Plain support of Nested Enums --- examples/flow/output/test/test_grpc_js.js | 4 ++++ examples/flow/protos/test.proto | 4 ++++ helpers.go | 5 ++--- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/examples/flow/output/test/test_grpc_js.js b/examples/flow/output/test/test_grpc_js.js index db9204d..2510017 100644 --- a/examples/flow/output/test/test_grpc_js.js +++ b/examples/flow/output/test/test_grpc_js.js @@ -57,6 +57,10 @@ export type TestMessage = {| r?: Array; + u?: TestEnum; + + v?: TestNestedEnum; + TestNestedMessage?: TestNestedMessage; diff --git a/examples/flow/protos/test.proto b/examples/flow/protos/test.proto index 7458188..93ae06e 100644 --- a/examples/flow/protos/test.proto +++ b/examples/flow/protos/test.proto @@ -27,6 +27,7 @@ message TestMessage { repeated int64 p = 16; repeated float q = 17; repeated double r = 18; + message TestNestedMessage { string s = 1; int32 t = 2; @@ -35,6 +36,9 @@ message TestMessage { ELEMENT_C = 0; ELEMENT_D = 1; } + + TestEnum u = 19; + TestNestedEnum v = 20; } message TestNoStreamRequest { TestMessage message = 1; } diff --git a/helpers.go b/helpers.go index 038f3ee..0550b98 100644 --- a/helpers.go +++ b/helpers.go @@ -148,7 +148,8 @@ func jsType(f *descriptor.FieldDescriptorProto) string { } switch *f.Type { - case descriptor.FieldDescriptorProto_TYPE_MESSAGE: + case descriptor.FieldDescriptorProto_TYPE_MESSAGE, + descriptor.FieldDescriptorProto_TYPE_ENUM: return fmt.Sprintf(template, shortType(*f.TypeName)) case descriptor.FieldDescriptorProto_TYPE_DOUBLE, descriptor.FieldDescriptorProto_TYPE_FLOAT, @@ -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") } From dc8e1910776cfcc87797f3a29d7ae9e1e98cb0c1 Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Tue, 10 Jan 2017 11:43:26 +0100 Subject: [PATCH 3/4] Add nested message usage example --- examples/flow/output/test/test_grpc_js.js | 22 +------------------ examples/flow/protos/test.proto | 1 + .../{{.File.Package}}_grpc_js.js.tmpl | 6 ----- 3 files changed, 2 insertions(+), 27 deletions(-) diff --git a/examples/flow/output/test/test_grpc_js.js b/examples/flow/output/test/test_grpc_js.js index 2510017..cf42431 100644 --- a/examples/flow/output/test/test_grpc_js.js +++ b/examples/flow/output/test/test_grpc_js.js @@ -61,11 +61,7 @@ export type TestMessage = {| v?: TestNestedEnum; - - TestNestedMessage?: TestNestedMessage; - - - TestNestedEnum?: TestNestedEnum; + w?: Array; |}; @@ -77,8 +73,6 @@ export type TestNoStreamRequest = {| message?: TestMessage; - - |}; @@ -91,8 +85,6 @@ export type TestNoStreamReply = {| err_msg?: string; - - |}; @@ -103,8 +95,6 @@ export type TestStreamRequestRequest = {| message?: TestMessage; - - |}; @@ -117,8 +107,6 @@ export type TestStreamRequestReply = {| err_msg?: string; - - |}; @@ -129,8 +117,6 @@ export type TestStreamReplyRequest = {| message?: TestMessage; - - |}; @@ -143,8 +129,6 @@ export type TestStreamReplyReply = {| err_msg?: string; - - |}; @@ -155,8 +139,6 @@ export type TestStreamBothRequest = {| message?: TestMessage; - - |}; @@ -169,8 +151,6 @@ export type TestStreamBothReply = {| err_msg?: string; - - |}; diff --git a/examples/flow/protos/test.proto b/examples/flow/protos/test.proto index 93ae06e..66ea9ac 100644 --- a/examples/flow/protos/test.proto +++ b/examples/flow/protos/test.proto @@ -39,6 +39,7 @@ message TestMessage { 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 fc99da1..d59d033 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 @@ -33,12 +33,6 @@ export type {{.Name}} = {| {{range .Field}} {{.Name}}?: {{. | jsType}}; {{end}} - {{range .NestedType}} - {{.Name}}?: {{.Name}}; - {{end}} - {{range .EnumType}} - {{.Name}}?: {{.Name}}; - {{end}} |}; {{end}} From 738a68fd4c674edabbad4e8e2e732af67c029353 Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Tue, 10 Jan 2017 12:24:06 +0100 Subject: [PATCH 4/4] Add type namespaces --- examples/flow/output/test/test_grpc_js.js | 17 +++++++++++++---- .../{{.File.Package}}_grpc_js.js.tmpl | 5 +++-- helpers.go | 8 +++++++- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/examples/flow/output/test/test_grpc_js.js b/examples/flow/output/test/test_grpc_js.js index cf42431..b61f1c7 100644 --- a/examples/flow/output/test/test_grpc_js.js +++ b/examples/flow/output/test/test_grpc_js.js @@ -16,7 +16,8 @@ export type TestEnum = {| -export type TestNestedEnum = {| + +export type TestMessage$TestNestedEnum = {| ELEMENT_C?: 0; @@ -26,7 +27,7 @@ export type TestNestedEnum = {| -export type TestNestedMessage = {| +export type TestMessage$TestNestedMessage = {| s?: string; @@ -59,9 +60,9 @@ export type TestMessage = {| u?: TestEnum; - v?: TestNestedEnum; + v?: TestMessage$TestNestedEnum; - w?: Array; + w?: Array; |}; @@ -69,6 +70,7 @@ export type TestMessage = {| + export type TestNoStreamRequest = {| message?: TestMessage; @@ -79,6 +81,7 @@ export type TestNoStreamRequest = {| + export type TestNoStreamReply = {| message?: TestMessage; @@ -91,6 +94,7 @@ export type TestNoStreamReply = {| + export type TestStreamRequestRequest = {| message?: TestMessage; @@ -101,6 +105,7 @@ export type TestStreamRequestRequest = {| + export type TestStreamRequestReply = {| message?: TestMessage; @@ -113,6 +118,7 @@ export type TestStreamRequestReply = {| + export type TestStreamReplyRequest = {| message?: TestMessage; @@ -123,6 +129,7 @@ export type TestStreamReplyRequest = {| + export type TestStreamReplyReply = {| message?: TestMessage; @@ -135,6 +142,7 @@ export type TestStreamReplyReply = {| + export type TestStreamBothRequest = {| message?: TestMessage; @@ -145,6 +153,7 @@ export type TestStreamBothRequest = {| + export type TestStreamBothReply = {| message?: TestMessage; 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 d59d033..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 @@ -13,8 +13,9 @@ export type {{.Name}} = {| {{end}} {{range .File.MessageType}} +{{$MessageType := .Name}} {{range .EnumType}} -export type {{.Name}} = {| +export type {{$MessageType}}${{.Name}} = {| {{range .Value}} {{.Name}}?: {{.Number}}; {{end}} @@ -22,7 +23,7 @@ export type {{.Name}} = {| {{end}} {{range .NestedType}} -export type {{.Name}} = {| +export type {{$MessageType}}${{.Name}} = {| {{range .Field}} {{.Name}}?: {{. | jsType}}; {{end}} diff --git a/helpers.go b/helpers.go index 0550b98..fa0a983 100644 --- a/helpers.go +++ b/helpers.go @@ -150,7 +150,7 @@ func jsType(f *descriptor.FieldDescriptorProto) string { switch *f.Type { case descriptor.FieldDescriptorProto_TYPE_MESSAGE, descriptor.FieldDescriptorProto_TYPE_ENUM: - return fmt.Sprintf(template, shortType(*f.TypeName)) + return fmt.Sprintf(template, namespacedFlowType(*f.TypeName)) case descriptor.FieldDescriptorProto_TYPE_DOUBLE, descriptor.FieldDescriptorProto_TYPE_FLOAT, descriptor.FieldDescriptorProto_TYPE_INT64, @@ -180,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)