From 54eb016142b3e6b78341cc1181b98914eb2a77ae Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Thu, 5 Jan 2017 18:06:24 +0100 Subject: [PATCH] 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}}