From 2c01e8b298f07665e68e8c405d80b0c04cd0daf8 Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Thu, 5 Jan 2017 18:03:52 +0100 Subject: [PATCH 1/7] Delete js example --- .../{{.File.Package}}_grpc_js.js.tmpl | 44 ------------------- 1 file changed, 44 deletions(-) delete mode 100644 examples/js/templates/{{.File.Package}}/{{.File.Package}}_grpc_js.js.tmpl diff --git a/examples/js/templates/{{.File.Package}}/{{.File.Package}}_grpc_js.js.tmpl b/examples/js/templates/{{.File.Package}}/{{.File.Package}}_grpc_js.js.tmpl deleted file mode 100644 index d30655e..0000000 --- a/examples/js/templates/{{.File.Package}}/{{.File.Package}}_grpc_js.js.tmpl +++ /dev/null @@ -1,44 +0,0 @@ -// GENERATED CODE -- DO NOT EDIT! -{{$Package:=.File.Package}} -'use strict'; -var grpc = require('grpc'); -var {{$Package}}_pb = require('./{{$Package}}_pb.js'); -{{range .File.Service}}{{range .Method}} -function serialize_{{$Package}}_{{.InputType | splitArray "." | last}}(arg) { - if (!(arg instanceof {{$Package}}_pb.{{.InputType | splitArray "." | last}})) { - throw new Error('Expected argument of type {{$Package}}.{{.InputType | splitArray "." | last}}'); - } - return new Buffer(arg.serializeBinary()); -} - -function deserialize_{{$Package}}_{{.InputType | splitArray "." | last}}(buffer_arg) { - return {{$Package}}_pb.{{.InputType | splitArray "." | last}}.deserializeBinary(new Uint8Array(buffer_arg)); -} - -function serialize_{{$Package}}_{{.OutputType | splitArray "." | last}}(arg) { - if (!(arg instanceof {{$Package}}_pb.{{.OutputType | splitArray "." | last}})) { - throw new Error('Expected argument of type {{$Package}}.{{.OutputType | splitArray "." | last}}'); - } - return new Buffer(arg.serializeBinary()); -} - -function deserialize_{{$Package}}_{{.OutputType | splitArray "." | last}}(buffer_arg) { - return {{$Package}}_pb.{{.OutputType | splitArray "." | last}}.deserializeBinary(new Uint8Array(buffer_arg)); -} -{{end}}{{end}} -{{range .File.Service}} -var {{.Name}}Service = exports.{{.Name}}Service = { - {{$serviceName:=.Name}}{{range .Method}}{{.Name | lowerCamelCase}}: { - path: '/{{$Package}}.{{$serviceName}}/{{.Name}}', - requestStream: {{.ClientStreaming | default "false"}}, - responseStream: {{.ServerStreaming | default "false"}}, - requestType: {{$Package}}_pb.{{.InputType | splitArray "." | last}}, - responseType: {{$Package}}_pb.{{.OutputType | splitArray "." | last}}, - requestSerialize: serialize_{{$Package}}_{{.InputType | splitArray "." | last}}, - requestDeserialize: deserialize_{{$Package}}_{{.InputType | splitArray "." | last}}, - responseSerialize: serialize_{{$Package}}_{{.OutputType | splitArray "." | last}}, - responseDeserialize: deserialize_{{$Package}}_{{.OutputType | splitArray "." | last}}, - },{{end}} -} - -exports.{{.Name}}Client = grpc.makeGenericClientConstructor({{.Name}}Service);{{end}} From 81310a7f0ff23d9614682145596deb856a5a47ad Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Thu, 5 Jan 2017 18:04:20 +0100 Subject: [PATCH 2/7] Update go helpers --- helpers.go | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/helpers.go b/helpers.go index 065f9c4..d11c967 100644 --- a/helpers.go +++ b/helpers.go @@ -70,6 +70,7 @@ var ProtoHelpersFuncMap = template.FuncMap{ "jsType": jsType, "httpVerb": httpVerb, "httpPath": httpPath, + "shortType": shortType, } func init() { @@ -141,31 +142,35 @@ func goType(pkg string, f *descriptor.FieldDescriptorProto) string { } func jsType(f *descriptor.FieldDescriptorProto) string { + template := "%s" + if isFieldRepeated(f) == true { + template = "Array<%s>" + } + switch *f.Type { - case descriptor.FieldDescriptorProto_TYPE_DOUBLE: - return "number" - case descriptor.FieldDescriptorProto_TYPE_FLOAT: - return "number" - case descriptor.FieldDescriptorProto_TYPE_INT64: - return "number" - case descriptor.FieldDescriptorProto_TYPE_UINT64: - return "number" - case descriptor.FieldDescriptorProto_TYPE_INT32: - return "number" - case descriptor.FieldDescriptorProto_TYPE_BOOL: - return "boolean" - case descriptor.FieldDescriptorProto_TYPE_STRING: - return "string" case descriptor.FieldDescriptorProto_TYPE_MESSAGE: - return "message" - case descriptor.FieldDescriptorProto_TYPE_BYTES: - return "number" - case descriptor.FieldDescriptorProto_TYPE_UINT32: - return "number" + return fmt.Sprintf(template, shortType(*f.TypeName)) + case descriptor.FieldDescriptorProto_TYPE_DOUBLE, + descriptor.FieldDescriptorProto_TYPE_FLOAT, + descriptor.FieldDescriptorProto_TYPE_INT64, + descriptor.FieldDescriptorProto_TYPE_UINT64, + descriptor.FieldDescriptorProto_TYPE_INT32, + descriptor.FieldDescriptorProto_TYPE_FIXED64, + descriptor.FieldDescriptorProto_TYPE_FIXED32, + descriptor.FieldDescriptorProto_TYPE_UINT32, + descriptor.FieldDescriptorProto_TYPE_SFIXED32, + descriptor.FieldDescriptorProto_TYPE_SFIXED64, + descriptor.FieldDescriptorProto_TYPE_SINT32, + descriptor.FieldDescriptorProto_TYPE_SINT64: + return fmt.Sprintf(template, "number") + case descriptor.FieldDescriptorProto_TYPE_BOOL: + return fmt.Sprintf(template, "boolean") + case descriptor.FieldDescriptorProto_TYPE_STRING: + return fmt.Sprintf(template, "string") case descriptor.FieldDescriptorProto_TYPE_ENUM: - return "message" + return fmt.Sprintf(template, "Object") default: - return "object" + return fmt.Sprintf(template, "any") } } From 54eb016142b3e6b78341cc1181b98914eb2a77ae Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Thu, 5 Jan 2017 18:06:24 +0100 Subject: [PATCH 3/7] Update example template --- examples/js-grpc/output/test/test_grpc_js.js | 172 +++++++++++------- .../{{.File.Package}}_grpc_js.js.tmpl | 58 +++--- 2 files changed, 145 insertions(+), 85 deletions(-) diff --git a/examples/js-grpc/output/test/test_grpc_js.js b/examples/js-grpc/output/test/test_grpc_js.js index 4790084..746fe1e 100644 --- a/examples/js-grpc/output/test/test_grpc_js.js +++ b/examples/js-grpc/output/test/test_grpc_js.js @@ -1,107 +1,157 @@ +// @flow // GENERATED CODE -- DO NOT EDIT! -'use strict'; -var grpc = require('grpc'); -var test_pb = require('./test_pb.js'); +import grpc from 'grpc' +import pbFile from './pbFile.js' -function serialize_test_TestNoStreamRequest(arg) { - if (!(arg instanceof test_pb.TestNoStreamRequest)) { - throw new Error('Expected argument of type test.TestNoStreamRequest'); +export type TestMessage = {| + a?: string; + b?: number; + c?: number; + d?: number; + e?: number; + n?: Array; + o?: Array; + p?: Array; + q?: Array; + r?: Array; +|}; + +export type TestNoStreamRequest = {| + message?: TestMessage; +|}; + +export type TestNoStreamReply = {| + message?: TestMessage; + err_msg?: string; +|}; + +export type TestStreamRequestRequest = {| + message?: TestMessage; +|}; + +export type TestStreamRequestReply = {| + message?: TestMessage; + err_msg?: string; +|}; + +export type TestStreamReplyRequest = {| + message?: TestMessage; +|}; + +export type TestStreamReplyReply = {| + message?: TestMessage; + err_msg?: string; +|}; + +export type TestStreamBothRequest = {| + message?: TestMessage; +|}; + +export type TestStreamBothReply = {| + message?: TestMessage; + err_msg?: string; +|}; + + +function serialize_test_TestNoStreamRequest(arg: TestNoStreamRequest) { + if (!(arg instanceof pbFile.TestNoStreamRequest)) { + throw new Error('Expected argument of type TestNoStreamRequest') } - return new Buffer(arg.serializeBinary()); + return new Buffer(arg.serializeBinary()) } -function deserialize_test_TestNoStreamRequest(buffer_arg) { - return test_pb.TestNoStreamRequest.deserializeBinary(new Uint8Array(buffer_arg)); +function deserialize_test_TestNoStreamRequest(buffer_arg: Array) { + return pbFile.TestNoStreamRequest.deserializeBinary(new Uint8Array(buffer_arg)) } -function serialize_test_TestNoStreamReply(arg) { - if (!(arg instanceof test_pb.TestNoStreamReply)) { - throw new Error('Expected argument of type test.TestNoStreamReply'); +function serialize_test_TestNoStreamReply(arg: TestNoStreamReply) { + if (!(arg instanceof pbFile.TestNoStreamReply)) { + throw new Error('Expected argument of type TestNoStreamReply') } - return new Buffer(arg.serializeBinary()); + return new Buffer(arg.serializeBinary()) } -function deserialize_test_TestNoStreamReply(buffer_arg) { - return test_pb.TestNoStreamReply.deserializeBinary(new Uint8Array(buffer_arg)); +function deserialize_test_TestNoStreamReply(buffer_arg: Array) { + return pbFile.TestNoStreamReply.deserializeBinary(new Uint8Array(buffer_arg)) } -function serialize_test_TestStreamRequestRequest(arg) { - if (!(arg instanceof test_pb.TestStreamRequestRequest)) { - throw new Error('Expected argument of type test.TestStreamRequestRequest'); +function serialize_test_TestStreamRequestRequest(arg: TestStreamRequestRequest) { + if (!(arg instanceof pbFile.TestStreamRequestRequest)) { + throw new Error('Expected argument of type TestStreamRequestRequest') } - return new Buffer(arg.serializeBinary()); + return new Buffer(arg.serializeBinary()) } -function deserialize_test_TestStreamRequestRequest(buffer_arg) { - return test_pb.TestStreamRequestRequest.deserializeBinary(new Uint8Array(buffer_arg)); +function deserialize_test_TestStreamRequestRequest(buffer_arg: Array) { + return pbFile.TestStreamRequestRequest.deserializeBinary(new Uint8Array(buffer_arg)) } -function serialize_test_TestStreamRequestReply(arg) { - if (!(arg instanceof test_pb.TestStreamRequestReply)) { - throw new Error('Expected argument of type test.TestStreamRequestReply'); +function serialize_test_TestStreamRequestReply(arg: TestStreamRequestReply) { + if (!(arg instanceof pbFile.TestStreamRequestReply)) { + throw new Error('Expected argument of type TestStreamRequestReply') } - return new Buffer(arg.serializeBinary()); + return new Buffer(arg.serializeBinary()) } -function deserialize_test_TestStreamRequestReply(buffer_arg) { - return test_pb.TestStreamRequestReply.deserializeBinary(new Uint8Array(buffer_arg)); +function deserialize_test_TestStreamRequestReply(buffer_arg: Array) { + return pbFile.TestStreamRequestReply.deserializeBinary(new Uint8Array(buffer_arg)) } -function serialize_test_TestStreamReplyRequest(arg) { - if (!(arg instanceof test_pb.TestStreamReplyRequest)) { - throw new Error('Expected argument of type test.TestStreamReplyRequest'); +function serialize_test_TestStreamReplyRequest(arg: TestStreamReplyRequest) { + if (!(arg instanceof pbFile.TestStreamReplyRequest)) { + throw new Error('Expected argument of type TestStreamReplyRequest') } - return new Buffer(arg.serializeBinary()); + return new Buffer(arg.serializeBinary()) } -function deserialize_test_TestStreamReplyRequest(buffer_arg) { - return test_pb.TestStreamReplyRequest.deserializeBinary(new Uint8Array(buffer_arg)); +function deserialize_test_TestStreamReplyRequest(buffer_arg: Array) { + return pbFile.TestStreamReplyRequest.deserializeBinary(new Uint8Array(buffer_arg)) } -function serialize_test_TestStreamReplyReply(arg) { - if (!(arg instanceof test_pb.TestStreamReplyReply)) { - throw new Error('Expected argument of type test.TestStreamReplyReply'); +function serialize_test_TestStreamReplyReply(arg: TestStreamReplyReply) { + if (!(arg instanceof pbFile.TestStreamReplyReply)) { + throw new Error('Expected argument of type TestStreamReplyReply') } - return new Buffer(arg.serializeBinary()); + return new Buffer(arg.serializeBinary()) } -function deserialize_test_TestStreamReplyReply(buffer_arg) { - return test_pb.TestStreamReplyReply.deserializeBinary(new Uint8Array(buffer_arg)); +function deserialize_test_TestStreamReplyReply(buffer_arg: Array) { + return pbFile.TestStreamReplyReply.deserializeBinary(new Uint8Array(buffer_arg)) } -function serialize_test_TestStreamBothRequest(arg) { - if (!(arg instanceof test_pb.TestStreamBothRequest)) { - throw new Error('Expected argument of type test.TestStreamBothRequest'); +function serialize_test_TestStreamBothRequest(arg: TestStreamBothRequest) { + if (!(arg instanceof pbFile.TestStreamBothRequest)) { + throw new Error('Expected argument of type TestStreamBothRequest') } - return new Buffer(arg.serializeBinary()); + return new Buffer(arg.serializeBinary()) } -function deserialize_test_TestStreamBothRequest(buffer_arg) { - return test_pb.TestStreamBothRequest.deserializeBinary(new Uint8Array(buffer_arg)); +function deserialize_test_TestStreamBothRequest(buffer_arg: Array) { + return pbFile.TestStreamBothRequest.deserializeBinary(new Uint8Array(buffer_arg)) } -function serialize_test_TestStreamBothReply(arg) { - if (!(arg instanceof test_pb.TestStreamBothReply)) { - throw new Error('Expected argument of type test.TestStreamBothReply'); +function serialize_test_TestStreamBothReply(arg: TestStreamBothReply) { + if (!(arg instanceof pbFile.TestStreamBothReply)) { + throw new Error('Expected argument of type TestStreamBothReply') } - return new Buffer(arg.serializeBinary()); + return new Buffer(arg.serializeBinary()) } -function deserialize_test_TestStreamBothReply(buffer_arg) { - return test_pb.TestStreamBothReply.deserializeBinary(new Uint8Array(buffer_arg)); +function deserialize_test_TestStreamBothReply(buffer_arg: Array) { + return pbFile.TestStreamBothReply.deserializeBinary(new Uint8Array(buffer_arg)) } -var TestServiceService = exports.TestServiceService = { +export const TestServiceService = { testNoStream: { path: '/test.TestService/TestNoStream', requestStream: false, responseStream: false, - requestType: test_pb.TestNoStreamRequest, - responseType: test_pb.TestNoStreamReply, + requestType: pbFile.TestNoStreamRequest, + responseType: pbFile.TestNoStreamReply, requestSerialize: serialize_test_TestNoStreamRequest, requestDeserialize: deserialize_test_TestNoStreamRequest, responseSerialize: serialize_test_TestNoStreamReply, @@ -111,8 +161,8 @@ var TestServiceService = exports.TestServiceService = { path: '/test.TestService/TestStreamRequest', requestStream: true, responseStream: false, - requestType: test_pb.TestStreamRequestRequest, - responseType: test_pb.TestStreamRequestReply, + requestType: pbFile.TestStreamRequestRequest, + responseType: pbFile.TestStreamRequestReply, requestSerialize: serialize_test_TestStreamRequestRequest, requestDeserialize: deserialize_test_TestStreamRequestRequest, responseSerialize: serialize_test_TestStreamRequestReply, @@ -122,8 +172,8 @@ var TestServiceService = exports.TestServiceService = { path: '/test.TestService/TestStreamReply', requestStream: false, responseStream: true, - requestType: test_pb.TestStreamReplyRequest, - responseType: test_pb.TestStreamReplyReply, + requestType: pbFile.TestStreamReplyRequest, + responseType: pbFile.TestStreamReplyReply, requestSerialize: serialize_test_TestStreamReplyRequest, requestDeserialize: deserialize_test_TestStreamReplyRequest, responseSerialize: serialize_test_TestStreamReplyReply, @@ -133,8 +183,8 @@ var TestServiceService = exports.TestServiceService = { path: '/test.TestService/TestStreamBoth', requestStream: true, responseStream: true, - requestType: test_pb.TestStreamBothRequest, - responseType: test_pb.TestStreamBothReply, + requestType: pbFile.TestStreamBothRequest, + responseType: pbFile.TestStreamBothReply, requestSerialize: serialize_test_TestStreamBothRequest, requestDeserialize: deserialize_test_TestStreamBothRequest, responseSerialize: serialize_test_TestStreamBothReply, @@ -143,4 +193,4 @@ var TestServiceService = exports.TestServiceService = { } -exports.TestServiceClient = grpc.makeGenericClientConstructor(TestServiceService); +export const TestServiceClient = grpc.makeGenericClientConstructor(TestServiceService) diff --git a/examples/js-grpc/templates/{{.File.Package}}/{{.File.Package}}_grpc_js.js.tmpl b/examples/js-grpc/templates/{{.File.Package}}/{{.File.Package}}_grpc_js.js.tmpl index 61dc4d2..16a98fb 100644 --- a/examples/js-grpc/templates/{{.File.Package}}/{{.File.Package}}_grpc_js.js.tmpl +++ b/examples/js-grpc/templates/{{.File.Package}}/{{.File.Package}}_grpc_js.js.tmpl @@ -1,47 +1,57 @@ +// @flow // GENERATED CODE -- DO NOT EDIT! {{$Package:=.File.Package}} -'use strict'; -var grpc = require('grpc'); -var {{$Package}}_pb = require('./{{$Package}}_pb.js'); - +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}} +|}; +{{end}} {{range .File.Service}}{{range .Method}} -function serialize_{{$Package}}_{{.InputType | splitArray "." | last}}(arg) { - if (!(arg instanceof {{$Package}}_pb.{{.InputType | splitArray "." | last}})) { - throw new Error('Expected argument of type {{$Package}}.{{.InputType | splitArray "." | last}}'); +function serialize_{{$Package}}_{{.InputType | shortType}}(arg: {{.InputType | shortType}}) { + if (!(arg instanceof pbFile.{{.InputType | shortType}})) { + throw new Error('Expected argument of type {{.InputType | shortType}}') } - return new Buffer(arg.serializeBinary()); + return new Buffer(arg.serializeBinary()) } -function deserialize_{{$Package}}_{{.InputType | splitArray "." | last}}(buffer_arg) { - return {{$Package}}_pb.{{.InputType | splitArray "." | last}}.deserializeBinary(new Uint8Array(buffer_arg)); +function deserialize_{{$Package}}_{{.InputType | shortType}}(buffer_arg: Array) { + return pbFile.{{.InputType | shortType}}.deserializeBinary(new Uint8Array(buffer_arg)) } -function serialize_{{$Package}}_{{.OutputType | splitArray "." | last}}(arg) { - if (!(arg instanceof {{$Package}}_pb.{{.OutputType | splitArray "." | last}})) { - throw new Error('Expected argument of type {{$Package}}.{{.OutputType | splitArray "." | last}}'); +function serialize_{{$Package}}_{{.OutputType | shortType}}(arg: {{.OutputType | shortType}}) { + if (!(arg instanceof pbFile.{{.OutputType | shortType}})) { + throw new Error('Expected argument of type {{.OutputType | shortType}}') } - return new Buffer(arg.serializeBinary()); + return new Buffer(arg.serializeBinary()) } -function deserialize_{{$Package}}_{{.OutputType | splitArray "." | last}}(buffer_arg) { - return {{$Package}}_pb.{{.OutputType | splitArray "." | last}}.deserializeBinary(new Uint8Array(buffer_arg)); +function deserialize_{{$Package}}_{{.OutputType | shortType}}(buffer_arg: Array) { + return pbFile.{{.OutputType | shortType}}.deserializeBinary(new Uint8Array(buffer_arg)) } {{end}}{{end}} {{range .File.Service}} -var {{.Name}}Service = exports.{{.Name}}Service = { +export const {{.Name}}Service = { {{$serviceName:=.Name}} {{range .Method}}{{.Name | lowerCamelCase}}: { path: '/{{$Package}}.{{$serviceName}}/{{.Name}}', requestStream: {{.ClientStreaming | default "false"}}, responseStream: {{.ServerStreaming | default "false"}}, - requestType: {{$Package}}_pb.{{.InputType | splitArray "." | last}}, - responseType: {{$Package}}_pb.{{.OutputType | splitArray "." | last}}, - requestSerialize: serialize_{{$Package}}_{{.InputType | splitArray "." | last}}, - requestDeserialize: deserialize_{{$Package}}_{{.InputType | splitArray "." | last}}, - responseSerialize: serialize_{{$Package}}_{{.OutputType | splitArray "." | last}}, - responseDeserialize: deserialize_{{$Package}}_{{.OutputType | splitArray "." | last}}, + requestType: pbFile.{{.InputType | shortType}}, + responseType: pbFile.{{.OutputType | shortType}}, + requestSerialize: serialize_{{$Package}}_{{.InputType | shortType}}, + requestDeserialize: deserialize_{{$Package}}_{{.InputType | shortType}}, + responseSerialize: serialize_{{$Package}}_{{.OutputType | shortType}}, + responseDeserialize: deserialize_{{$Package}}_{{.OutputType | shortType}}, }, {{end}} } -exports.{{.Name}}Client = grpc.makeGenericClientConstructor({{.Name}}Service);{{end}} +export const {{.Name}}Client = grpc.makeGenericClientConstructor({{.Name}}Service){{end}} From af1ed8a2fb8432138b69f392f2362f0fb9a63fee Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Thu, 5 Jan 2017 18:26:35 +0100 Subject: [PATCH 4/7] Don't export types --- examples/js-grpc/output/test/test_grpc_js.js | 18 +++++++++--------- .../{{.File.Package}}_grpc_js.js.tmpl | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/examples/js-grpc/output/test/test_grpc_js.js b/examples/js-grpc/output/test/test_grpc_js.js index 746fe1e..dfb27c4 100644 --- a/examples/js-grpc/output/test/test_grpc_js.js +++ b/examples/js-grpc/output/test/test_grpc_js.js @@ -5,7 +5,7 @@ import grpc from 'grpc' import pbFile from './pbFile.js' -export type TestMessage = {| +type TestMessage = {| a?: string; b?: number; c?: number; @@ -18,38 +18,38 @@ export type TestMessage = {| r?: Array; |}; -export type TestNoStreamRequest = {| +type TestNoStreamRequest = {| message?: TestMessage; |}; -export type TestNoStreamReply = {| +type TestNoStreamReply = {| message?: TestMessage; err_msg?: string; |}; -export type TestStreamRequestRequest = {| +type TestStreamRequestRequest = {| message?: TestMessage; |}; -export type TestStreamRequestReply = {| +type TestStreamRequestReply = {| message?: TestMessage; err_msg?: string; |}; -export type TestStreamReplyRequest = {| +type TestStreamReplyRequest = {| message?: TestMessage; |}; -export type TestStreamReplyReply = {| +type TestStreamReplyReply = {| message?: TestMessage; err_msg?: string; |}; -export type TestStreamBothRequest = {| +type TestStreamBothRequest = {| message?: TestMessage; |}; -export type TestStreamBothReply = {| +type TestStreamBothReply = {| message?: TestMessage; err_msg?: string; |}; diff --git a/examples/js-grpc/templates/{{.File.Package}}/{{.File.Package}}_grpc_js.js.tmpl b/examples/js-grpc/templates/{{.File.Package}}/{{.File.Package}}_grpc_js.js.tmpl index 16a98fb..b155759 100644 --- a/examples/js-grpc/templates/{{.File.Package}}/{{.File.Package}}_grpc_js.js.tmpl +++ b/examples/js-grpc/templates/{{.File.Package}}/{{.File.Package}}_grpc_js.js.tmpl @@ -3,11 +3,11 @@ {{$Package:=.File.Package}} import grpc from 'grpc' import pbFile from './pbFile.js' -{{range .File.EnumType}}export type {{.Name}} = {{range .Value}} +{{range .File.EnumType}}type {{.Name}} = {{range .Value}} | '{{.Name}}'{{end}} ;{{end}} {{range .File.MessageType}} -export type {{.Name}} = {|{{range .Field}} +type {{.Name}} = {|{{range .Field}} {{.Name}}?: {{. | jsType}};{{end}}{{range .EnumType}} {{.Name}}?:{{range .Value}} | '{{.Name}}'{{end}} From a8d641cd8d9b65f13d4c53a92fbf69a5cd8beed6 Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Thu, 5 Jan 2017 18:32:54 +0100 Subject: [PATCH 5/7] Add Bytes type --- helpers.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/helpers.go b/helpers.go index d11c967..038f3ee 100644 --- a/helpers.go +++ b/helpers.go @@ -165,6 +165,8 @@ func jsType(f *descriptor.FieldDescriptorProto) string { return fmt.Sprintf(template, "number") case descriptor.FieldDescriptorProto_TYPE_BOOL: return fmt.Sprintf(template, "boolean") + case descriptor.FieldDescriptorProto_TYPE_BYTES: + return fmt.Sprintf(template, "Array") case descriptor.FieldDescriptorProto_TYPE_STRING: return fmt.Sprintf(template, "string") case descriptor.FieldDescriptorProto_TYPE_ENUM: From ab2a5d51816b8c4b1ea61ebcacce6fb261e104ac Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Fri, 6 Jan 2017 11:38:48 +0100 Subject: [PATCH 6/7] Rename js-grpc folder, add enums to example file --- Makefile | 2 +- examples/{js-grpc => flow}/Makefile | 0 examples/{js-grpc => flow}/output/test/test_grpc_js.js | 9 ++++++++- examples/{js-grpc => flow}/protos/test.proto | 9 +++++++++ .../{{.File.Package}}/{{.File.Package}}_grpc_js.js.tmpl | 0 5 files changed, 18 insertions(+), 2 deletions(-) rename examples/{js-grpc => flow}/Makefile (100%) rename examples/{js-grpc => flow}/output/test/test_grpc_js.js (98%) rename examples/{js-grpc => flow}/protos/test.proto (92%) rename examples/{js-grpc => flow}/templates/{{.File.Package}}/{{.File.Package}}_grpc_js.js.tmpl (100%) diff --git a/Makefile b/Makefile index 83cdc7a..e898e34 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ install: .PHONY: test test: build cd examples/dummy && make - cd examples/js-grpc && make + cd examples/flow && make .PHONY: docker.build docker.build: diff --git a/examples/js-grpc/Makefile b/examples/flow/Makefile similarity index 100% rename from examples/js-grpc/Makefile rename to examples/flow/Makefile diff --git a/examples/js-grpc/output/test/test_grpc_js.js b/examples/flow/output/test/test_grpc_js.js similarity index 98% rename from examples/js-grpc/output/test/test_grpc_js.js rename to examples/flow/output/test/test_grpc_js.js index dfb27c4..be399ae 100644 --- a/examples/js-grpc/output/test/test_grpc_js.js +++ b/examples/flow/output/test/test_grpc_js.js @@ -3,7 +3,10 @@ import grpc from 'grpc' import pbFile from './pbFile.js' - +type TestEnum = + | 'ELEMENT_A' + | 'ELEMENT_B' +; type TestMessage = {| a?: string; @@ -16,6 +19,10 @@ type TestMessage = {| p?: Array; q?: Array; r?: Array; + s?: + | 'ELEMENT_C' + | 'ELEMENT_D' + ; |}; type TestNoStreamRequest = {| diff --git a/examples/js-grpc/protos/test.proto b/examples/flow/protos/test.proto similarity index 92% rename from examples/js-grpc/protos/test.proto rename to examples/flow/protos/test.proto index cd7ebe7..e15ee88 100644 --- a/examples/js-grpc/protos/test.proto +++ b/examples/flow/protos/test.proto @@ -11,6 +11,11 @@ service TestService { rpc TestStreamBoth(stream TestStreamBothRequest) returns (stream TestStreamBothReply); } +enum TestEnum { + ELEMENT_A = 0; + ELEMENT_B = 1; +} + message TestMessage { string a = 1; int32 b = 2; @@ -22,6 +27,10 @@ message TestMessage { repeated int64 p = 16; repeated float q = 17; repeated double r = 18; + enum s { + ELEMENT_C = 0; + ELEMENT_D = 1; + } } message TestNoStreamRequest { TestMessage message = 1; } diff --git a/examples/js-grpc/templates/{{.File.Package}}/{{.File.Package}}_grpc_js.js.tmpl b/examples/flow/templates/{{.File.Package}}/{{.File.Package}}_grpc_js.js.tmpl similarity index 100% rename from examples/js-grpc/templates/{{.File.Package}}/{{.File.Package}}_grpc_js.js.tmpl rename to examples/flow/templates/{{.File.Package}}/{{.File.Package}}_grpc_js.js.tmpl From 37d97c5693d12697315b2a270c7fbfb9b1d492cf Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Fri, 6 Jan 2017 11:41:42 +0100 Subject: [PATCH 7/7] Export types --- examples/flow/output/test/test_grpc_js.js | 21 ++++++++++--------- .../{{.File.Package}}_grpc_js.js.tmpl | 5 +++-- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/examples/flow/output/test/test_grpc_js.js b/examples/flow/output/test/test_grpc_js.js index be399ae..e8a87e2 100644 --- a/examples/flow/output/test/test_grpc_js.js +++ b/examples/flow/output/test/test_grpc_js.js @@ -3,12 +3,13 @@ import grpc from 'grpc' import pbFile from './pbFile.js' -type TestEnum = + +export type TestEnum = | 'ELEMENT_A' | 'ELEMENT_B' ; -type TestMessage = {| +export type TestMessage = {| a?: string; b?: number; c?: number; @@ -25,38 +26,38 @@ type TestMessage = {| ; |}; -type TestNoStreamRequest = {| +export type TestNoStreamRequest = {| message?: TestMessage; |}; -type TestNoStreamReply = {| +export type TestNoStreamReply = {| message?: TestMessage; err_msg?: string; |}; -type TestStreamRequestRequest = {| +export type TestStreamRequestRequest = {| message?: TestMessage; |}; -type TestStreamRequestReply = {| +export type TestStreamRequestReply = {| message?: TestMessage; err_msg?: string; |}; -type TestStreamReplyRequest = {| +export type TestStreamReplyRequest = {| message?: TestMessage; |}; -type TestStreamReplyReply = {| +export type TestStreamReplyReply = {| message?: TestMessage; err_msg?: string; |}; -type TestStreamBothRequest = {| +export type TestStreamBothRequest = {| message?: TestMessage; |}; -type TestStreamBothReply = {| +export type TestStreamBothReply = {| message?: TestMessage; err_msg?: string; |}; 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 b155759..8acd68d 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,11 +3,12 @@ {{$Package:=.File.Package}} import grpc from 'grpc' import pbFile from './pbFile.js' -{{range .File.EnumType}}type {{.Name}} = {{range .Value}} +{{range .File.EnumType}} +export type {{.Name}} = {{range .Value}} | '{{.Name}}'{{end}} ;{{end}} {{range .File.MessageType}} -type {{.Name}} = {|{{range .Field}} +export type {{.Name}} = {|{{range .Field}} {{.Name}}?: {{. | jsType}};{{end}}{{range .EnumType}} {{.Name}}?:{{range .Value}} | '{{.Name}}'{{end}}