From 738a68fd4c674edabbad4e8e2e732af67c029353 Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Tue, 10 Jan 2017 12:24:06 +0100 Subject: [PATCH] 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)