From 47277242aed45ee627e07cd22f6eba89a53ad8f9 Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Wed, 11 Jan 2017 14:52:00 +0100 Subject: [PATCH 1/4] Array -> Uint8Array --- examples/flow/output/test/test_grpc_js.js | 312 ++++++++---------- .../{{.File.Package}}_grpc_js.js.tmpl | 88 +++-- helpers.go | 2 +- 3 files changed, 174 insertions(+), 228 deletions(-) diff --git a/examples/flow/output/test/test_grpc_js.js b/examples/flow/output/test/test_grpc_js.js index b61f1c7..460dcb4 100644 --- a/examples/flow/output/test/test_grpc_js.js +++ b/examples/flow/output/test/test_grpc_js.js @@ -1,16 +1,13 @@ // @flow // GENERATED CODE -- DO NOT EDIT! -import grpc from 'grpc' -import pbFile from './pbFile.js' +import base64 from 'base64-js' +import test_pb from './test_pb' export type TestEnum = {| - - ELEMENT_A?: 0; - - ELEMENT_B?: 1; - + ELEMENT_A?: 0; + ELEMENT_B?: 1; |}; @@ -18,288 +15,241 @@ export type TestEnum = {| export type TestMessage$TestNestedEnum = {| - - ELEMENT_C?: 0; - - ELEMENT_D?: 1; - + ELEMENT_C?: 0; + ELEMENT_D?: 1; |}; - export type TestMessage$TestNestedMessage = {| - - s?: string; - - t?: number; - + 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; - - u?: TestEnum; - - v?: TestMessage$TestNestedEnum; - - w?: Array; - + 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; |}; +const serializeToBase64 = (byteArray: Uint8Array): string => + base64.fromByteArray(byteArray) -function serialize_test_TestNoStreamRequest(arg: TestNoStreamRequest) { - if (!(arg instanceof pbFile.TestNoStreamRequest)) { +const deserializeFromBase64 = (base64Encoded: string): Uint8Array => + new Uint8Array(base64.toByteArray(base64Encoded)) + + +function serialize_test_TestNoStreamRequest(arg : TestNoStreamRequest): string { + if (!(arg instanceof test_pb.TestNoStreamRequest)) { throw new Error('Expected argument of type TestNoStreamRequest') } - return new Buffer(arg.serializeBinary()) + return serializeToBase64(arg.serializeBinary()) } -function deserialize_test_TestNoStreamRequest(buffer_arg: Array) { - return pbFile.TestNoStreamRequest.deserializeBinary(new Uint8Array(buffer_arg)) +function deserialize_test_TestNoStreamRequest(base64Encoded: string): TestNoStreamRequest { + return test_pb.TestNoStreamRequest.deserializeBinary(deserializeFromBase64(base64Encoded)) } -function serialize_test_TestNoStreamReply(arg: TestNoStreamReply) { - if (!(arg instanceof pbFile.TestNoStreamReply)) { +function serialize_test_TestNoStreamReply(arg : TestNoStreamReply): string { + if (!(arg instanceof test_pb.TestNoStreamReply)) { throw new Error('Expected argument of type TestNoStreamReply') } - return new Buffer(arg.serializeBinary()) + return serializeToBase64(arg.serializeBinary()) } -function deserialize_test_TestNoStreamReply(buffer_arg: Array) { - return pbFile.TestNoStreamReply.deserializeBinary(new Uint8Array(buffer_arg)) +function deserialize_test_TestNoStreamReply(base64Encoded: string): TestNoStreamReply { + return test_pb.TestNoStreamReply.deserializeBinary(deserializeFromBase64(base64Encoded)) } -function serialize_test_TestStreamRequestRequest(arg: TestStreamRequestRequest) { - if (!(arg instanceof pbFile.TestStreamRequestRequest)) { + +function serialize_test_TestStreamRequestRequest(arg : TestStreamRequestRequest): string { + if (!(arg instanceof test_pb.TestStreamRequestRequest)) { throw new Error('Expected argument of type TestStreamRequestRequest') } - return new Buffer(arg.serializeBinary()) + return serializeToBase64(arg.serializeBinary()) } -function deserialize_test_TestStreamRequestRequest(buffer_arg: Array) { - return pbFile.TestStreamRequestRequest.deserializeBinary(new Uint8Array(buffer_arg)) +function deserialize_test_TestStreamRequestRequest(base64Encoded: string): TestStreamRequestRequest { + return test_pb.TestStreamRequestRequest.deserializeBinary(deserializeFromBase64(base64Encoded)) } -function serialize_test_TestStreamRequestReply(arg: TestStreamRequestReply) { - if (!(arg instanceof pbFile.TestStreamRequestReply)) { +function serialize_test_TestStreamRequestReply(arg : TestStreamRequestReply): string { + if (!(arg instanceof test_pb.TestStreamRequestReply)) { throw new Error('Expected argument of type TestStreamRequestReply') } - return new Buffer(arg.serializeBinary()) + return serializeToBase64(arg.serializeBinary()) } -function deserialize_test_TestStreamRequestReply(buffer_arg: Array) { - return pbFile.TestStreamRequestReply.deserializeBinary(new Uint8Array(buffer_arg)) +function deserialize_test_TestStreamRequestReply(base64Encoded: string): TestStreamRequestReply { + return test_pb.TestStreamRequestReply.deserializeBinary(deserializeFromBase64(base64Encoded)) } -function serialize_test_TestStreamReplyRequest(arg: TestStreamReplyRequest) { - if (!(arg instanceof pbFile.TestStreamReplyRequest)) { + +function serialize_test_TestStreamReplyRequest(arg : TestStreamReplyRequest): string { + if (!(arg instanceof test_pb.TestStreamReplyRequest)) { throw new Error('Expected argument of type TestStreamReplyRequest') } - return new Buffer(arg.serializeBinary()) + return serializeToBase64(arg.serializeBinary()) } -function deserialize_test_TestStreamReplyRequest(buffer_arg: Array) { - return pbFile.TestStreamReplyRequest.deserializeBinary(new Uint8Array(buffer_arg)) +function deserialize_test_TestStreamReplyRequest(base64Encoded: string): TestStreamReplyRequest { + return test_pb.TestStreamReplyRequest.deserializeBinary(deserializeFromBase64(base64Encoded)) } -function serialize_test_TestStreamReplyReply(arg: TestStreamReplyReply) { - if (!(arg instanceof pbFile.TestStreamReplyReply)) { +function serialize_test_TestStreamReplyReply(arg : TestStreamReplyReply): string { + if (!(arg instanceof test_pb.TestStreamReplyReply)) { throw new Error('Expected argument of type TestStreamReplyReply') } - return new Buffer(arg.serializeBinary()) + return serializeToBase64(arg.serializeBinary()) } -function deserialize_test_TestStreamReplyReply(buffer_arg: Array) { - return pbFile.TestStreamReplyReply.deserializeBinary(new Uint8Array(buffer_arg)) +function deserialize_test_TestStreamReplyReply(base64Encoded: string): TestStreamReplyReply { + return test_pb.TestStreamReplyReply.deserializeBinary(deserializeFromBase64(base64Encoded)) } -function serialize_test_TestStreamBothRequest(arg: TestStreamBothRequest) { - if (!(arg instanceof pbFile.TestStreamBothRequest)) { + +function serialize_test_TestStreamBothRequest(arg : TestStreamBothRequest): string { + if (!(arg instanceof test_pb.TestStreamBothRequest)) { throw new Error('Expected argument of type TestStreamBothRequest') } - return new Buffer(arg.serializeBinary()) + return serializeToBase64(arg.serializeBinary()) } -function deserialize_test_TestStreamBothRequest(buffer_arg: Array) { - return pbFile.TestStreamBothRequest.deserializeBinary(new Uint8Array(buffer_arg)) +function deserialize_test_TestStreamBothRequest(base64Encoded: string): TestStreamBothRequest { + return test_pb.TestStreamBothRequest.deserializeBinary(deserializeFromBase64(base64Encoded)) } -function serialize_test_TestStreamBothReply(arg: TestStreamBothReply) { - if (!(arg instanceof pbFile.TestStreamBothReply)) { +function serialize_test_TestStreamBothReply(arg : TestStreamBothReply): string { + if (!(arg instanceof test_pb.TestStreamBothReply)) { throw new Error('Expected argument of type TestStreamBothReply') } - return new Buffer(arg.serializeBinary()) + return serializeToBase64(arg.serializeBinary()) } -function deserialize_test_TestStreamBothReply(buffer_arg: Array) { - return pbFile.TestStreamBothReply.deserializeBinary(new Uint8Array(buffer_arg)) +function deserialize_test_TestStreamBothReply(base64Encoded: string): TestStreamBothReply { + return test_pb.TestStreamBothReply.deserializeBinary(deserializeFromBase64(base64Encoded)) } -export const TestServiceService = { +export default { - testNoStream: { - path: '/test.TestService/TestNoStream', - requestStream: false, - responseStream: false, - requestType: pbFile.TestNoStreamRequest, - responseType: pbFile.TestNoStreamReply, - requestSerialize: serialize_test_TestNoStreamRequest, - requestDeserialize: deserialize_test_TestNoStreamRequest, - responseSerialize: serialize_test_TestNoStreamReply, - responseDeserialize: deserialize_test_TestNoStreamReply, - }, - testStreamRequest: { - path: '/test.TestService/TestStreamRequest', - requestStream: true, - responseStream: false, - requestType: pbFile.TestStreamRequestRequest, - responseType: pbFile.TestStreamRequestReply, - requestSerialize: serialize_test_TestStreamRequestRequest, - requestDeserialize: deserialize_test_TestStreamRequestRequest, - responseSerialize: serialize_test_TestStreamRequestReply, - responseDeserialize: deserialize_test_TestStreamRequestReply, - }, - testStreamReply: { - path: '/test.TestService/TestStreamReply', - requestStream: false, - responseStream: true, - requestType: pbFile.TestStreamReplyRequest, - responseType: pbFile.TestStreamReplyReply, - requestSerialize: serialize_test_TestStreamReplyRequest, - requestDeserialize: deserialize_test_TestStreamReplyRequest, - responseSerialize: serialize_test_TestStreamReplyReply, - responseDeserialize: deserialize_test_TestStreamReplyReply, - }, - testStreamBoth: { - path: '/test.TestService/TestStreamBoth', - requestStream: true, - responseStream: true, - requestType: pbFile.TestStreamBothRequest, - responseType: pbFile.TestStreamBothReply, - requestSerialize: serialize_test_TestStreamBothRequest, - requestDeserialize: deserialize_test_TestStreamBothRequest, - responseSerialize: serialize_test_TestStreamBothReply, - responseDeserialize: deserialize_test_TestStreamBothReply, - }, + TestService: { + + testNoStream: { + path: '/test.TestService/TestNoStream', + requestStream: false, + responseStream: false, + requestType: test_pb.TestNoStreamRequest, + responseType: test_pb.TestNoStreamReply, + requestSerialize: serialize_test_TestNoStreamRequest, + requestDeserialize: deserialize_test_TestNoStreamRequest, + responseSerialize: serialize_test_TestNoStreamReply, + responseDeserialize: deserialize_test_TestNoStreamReply, + }, + testStreamRequest: { + path: '/test.TestService/TestStreamRequest', + requestStream: true, + responseStream: false, + requestType: test_pb.TestStreamRequestRequest, + responseType: test_pb.TestStreamRequestReply, + requestSerialize: serialize_test_TestStreamRequestRequest, + requestDeserialize: deserialize_test_TestStreamRequestRequest, + responseSerialize: serialize_test_TestStreamRequestReply, + responseDeserialize: deserialize_test_TestStreamRequestReply, + }, + testStreamReply: { + path: '/test.TestService/TestStreamReply', + requestStream: false, + responseStream: true, + requestType: test_pb.TestStreamReplyRequest, + responseType: test_pb.TestStreamReplyReply, + requestSerialize: serialize_test_TestStreamReplyRequest, + requestDeserialize: deserialize_test_TestStreamReplyRequest, + responseSerialize: serialize_test_TestStreamReplyReply, + responseDeserialize: deserialize_test_TestStreamReplyReply, + }, + testStreamBoth: { + path: '/test.TestService/TestStreamBoth', + requestStream: true, + responseStream: true, + requestType: test_pb.TestStreamBothRequest, + responseType: test_pb.TestStreamBothReply, + requestSerialize: serialize_test_TestStreamBothRequest, + requestDeserialize: deserialize_test_TestStreamBothRequest, + responseSerialize: serialize_test_TestStreamBothReply, + responseDeserialize: deserialize_test_TestStreamBothReply, + }, + + } } - -export const TestServiceClient = grpc.makeGenericClientConstructor(TestServiceService) 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 5e0ba9e..580ed35 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 @@ -1,80 +1,76 @@ // @flow // GENERATED CODE -- DO NOT EDIT! {{$Package:=.File.Package}} -import grpc from 'grpc' -import pbFile from './pbFile.js' +import base64 from 'base64-js' +import {{$Package}}_pb from './{{$Package}}_pb'{{range .File.Dependency}} +import {{. | replace "/" "_" | trimSuffix ".proto" }}_pb from '../{{. | trimSuffix ".proto" }}_pb'{{end}} {{range .File.EnumType}} -export type {{.Name}} = {| - {{range .Value}} - {{.Name}}?: {{.Number}}; - {{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}} +export type {{$MessageType}}${{.Name}} = {|{{range .Value}} + {{.Name}}?: {{.Number}};{{end}} |}; {{end}} - {{range .NestedType}} -export type {{$MessageType}}${{.Name}} = {| - {{range .Field}} - {{.Name}}?: {{. | jsType}}; - {{end}} +export type {{$MessageType}}${{.Name}} = {|{{range .Field}} + {{.Name}}?: {{. | jsType}};{{end}} +|}; +{{end}} +export type {{.Name}} = {|{{range .Field}} + {{.Name}}?: {{. | jsType}};{{end}} |}; {{end}} -export type {{.Name}} = {| - {{range .Field}} - {{.Name}}?: {{. | jsType}}; - {{end}} -|}; -{{end}} +const serializeToBase64 = (byteArray: Uint8Array): string => base64.fromByteArray(byteArray) +const deserializeFromBase64 = (base64Encoded: string): Uint8Array => new Uint8Array(base64.toByteArray(base64Encoded)) {{range .File.Service}}{{range .Method}} -function serialize_{{$Package}}_{{.InputType | shortType}}(arg: {{.InputType | shortType}}) { - if (!(arg instanceof pbFile.{{.InputType | shortType}})) { +function serialize_{{$Package}}_{{.InputType | shortType}}(arg : {{.InputType | shortType}}): string { + if (!(arg instanceof {{$Package}}_pb.{{.InputType | shortType}})) { throw new Error('Expected argument of type {{.InputType | shortType}}') } - return new Buffer(arg.serializeBinary()) + return serializeToBase64(arg.serializeBinary()) } -function deserialize_{{$Package}}_{{.InputType | shortType}}(buffer_arg: Array) { - return pbFile.{{.InputType | shortType}}.deserializeBinary(new Uint8Array(buffer_arg)) +function deserialize_{{$Package}}_{{.InputType | shortType}}(base64Encoded: string): {{.InputType | shortType}} { + return {{$Package}}_pb.{{.InputType | shortType}}.deserializeBinary(deserializeFromBase64(base64Encoded)) } -function serialize_{{$Package}}_{{.OutputType | shortType}}(arg: {{.OutputType | shortType}}) { - if (!(arg instanceof pbFile.{{.OutputType | shortType}})) { +function serialize_{{$Package}}_{{.OutputType | shortType}}(arg : {{.OutputType | shortType}}): string { + if (!(arg instanceof {{$Package}}_pb.{{.OutputType | shortType}})) { throw new Error('Expected argument of type {{.OutputType | shortType}}') } - return new Buffer(arg.serializeBinary()) + return serializeToBase64(arg.serializeBinary()) } -function deserialize_{{$Package}}_{{.OutputType | shortType}}(buffer_arg: Array) { - return pbFile.{{.OutputType | shortType}}.deserializeBinary(new Uint8Array(buffer_arg)) +function deserialize_{{$Package}}_{{.OutputType | shortType}}(base64Encoded: string): {{.OutputType | shortType}} { + return {{$Package}}_pb.{{.OutputType | shortType}}.deserializeBinary(deserializeFromBase64(base64Encoded)) } + {{end}}{{end}} -{{range .File.Service}} -export const {{.Name}}Service = { +export default { + {{range .File.Service}} + {{.Name}}: { {{$serviceName:=.Name}} - {{range .Method}}{{.Name | lowerCamelCase}}: { - path: '/{{$Package}}.{{$serviceName}}/{{.Name}}', - requestStream: {{.ClientStreaming | default "false"}}, - responseStream: {{.ServerStreaming | default "false"}}, - 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}}, - }, + {{range .Method}}{{.Name | lowerCamelCase}}: { + path: '/{{$Package}}.{{$serviceName}}/{{.Name}}', + requestStream: {{.ClientStreaming | default "false"}}, + responseStream: {{.ServerStreaming | default "false"}}, + requestType: {{$Package}}_pb.{{.InputType | shortType}}, + responseType: {{$Package}}_pb.{{.OutputType | shortType}}, + requestSerialize: serialize_{{$Package}}_{{.InputType | shortType}}, + requestDeserialize: deserialize_{{$Package}}_{{.InputType | shortType}}, + responseSerialize: serialize_{{$Package}}_{{.OutputType | shortType}}, + responseDeserialize: deserialize_{{$Package}}_{{.OutputType | shortType}}, + }, + {{end}} + } {{end}} } - -export const {{.Name}}Client = grpc.makeGenericClientConstructor({{.Name}}Service){{end}} diff --git a/helpers.go b/helpers.go index fa0a983..f7108e9 100644 --- a/helpers.go +++ b/helpers.go @@ -167,7 +167,7 @@ func jsType(f *descriptor.FieldDescriptorProto) string { case descriptor.FieldDescriptorProto_TYPE_BOOL: return fmt.Sprintf(template, "boolean") case descriptor.FieldDescriptorProto_TYPE_BYTES: - return fmt.Sprintf(template, "Array") + return fmt.Sprintf(template, "Uint8Array") case descriptor.FieldDescriptorProto_TYPE_STRING: return fmt.Sprintf(template, "string") default: From b3b9aec439a0a96a480c8653328058235b3aa8d7 Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Wed, 11 Jan 2017 15:20:01 +0100 Subject: [PATCH 2/4] Update example, remove strict types --- examples/flow/output/test/test_grpc_js.js | 135 +++++++++--------- .../{{.File.Package}}_grpc_js.js.tmpl | 59 +++++--- 2 files changed, 106 insertions(+), 88 deletions(-) diff --git a/examples/flow/output/test/test_grpc_js.js b/examples/flow/output/test/test_grpc_js.js index 460dcb4..cef1073 100644 --- a/examples/flow/output/test/test_grpc_js.js +++ b/examples/flow/output/test/test_grpc_js.js @@ -5,108 +5,109 @@ import base64 from 'base64-js' import test_pb from './test_pb' -export type TestEnum = {| - ELEMENT_A?: 0; - ELEMENT_B?: 1; -|}; +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; + u?: TestEnum; + v?: TestMessage$TestNestedEnum; + w?: Array; +}; -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; - u?: TestEnum; - v?: TestMessage$TestNestedEnum; - w?: Array; -|}; +export type TestNoStreamRequest = { + message?: TestMessage; +}; -export type TestNoStreamRequest = {| - message?: TestMessage; -|}; + +export type TestNoStreamReply = { + message?: TestMessage; + err_msg?: string; +}; -export type TestNoStreamReply = {| - message?: TestMessage; - err_msg?: string; -|}; + +export type TestStreamRequestRequest = { + message?: TestMessage; +}; -export type TestStreamRequestRequest = {| - message?: TestMessage; -|}; + +export type TestStreamRequestReply = { + message?: TestMessage; + err_msg?: string; +}; -export type TestStreamRequestReply = {| - message?: TestMessage; - err_msg?: string; -|}; + +export type TestStreamReplyRequest = { + message?: TestMessage; +}; -export type TestStreamReplyRequest = {| - message?: TestMessage; -|}; + +export type TestStreamReplyReply = { + message?: TestMessage; + err_msg?: string; +}; -export type TestStreamReplyReply = {| - message?: TestMessage; - err_msg?: string; -|}; + +export type TestStreamBothRequest = { + message?: TestMessage; +}; -export type TestStreamBothRequest = {| - message?: TestMessage; -|}; +export type TestStreamBothReply = { + message?: TestMessage; + err_msg?: string; +}; - - -export type TestStreamBothReply = {| - message?: TestMessage; - err_msg?: string; -|}; - - -const serializeToBase64 = (byteArray: Uint8Array): string => - base64.fromByteArray(byteArray) - -const deserializeFromBase64 = (base64Encoded: string): Uint8Array => - new Uint8Array(base64.toByteArray(base64Encoded)) +const serializeToBase64 = (byteArray: Uint8Array): string => base64.fromByteArray(byteArray) +const deserializeFromBase64 = (base64Encoded: string): Uint8Array => new Uint8Array(base64.toByteArray(base64Encoded)) function serialize_test_TestNoStreamRequest(arg : TestNoStreamRequest): 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 580ed35..376c519 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 @@ -1,32 +1,49 @@ // @flow // GENERATED CODE -- DO NOT EDIT! -{{$Package:=.File.Package}} + +{{- $Package:=.File.Package}} + import base64 from 'base64-js' -import {{$Package}}_pb from './{{$Package}}_pb'{{range .File.Dependency}} -import {{. | replace "/" "_" | trimSuffix ".proto" }}_pb from '../{{. | trimSuffix ".proto" }}_pb'{{end}} +import {{$Package}}_pb from './{{$Package}}_pb' + +{{- range .File.Dependency}} +import {{. | replace "/" "_" | trimSuffix ".proto" }}_pb from '../{{. | trimSuffix ".proto" }}_pb' +{{- end}} {{range .File.EnumType}} -export type {{.Name}} = {|{{range .Value}} - {{.Name}}?: {{.Number}};{{end}} -|}; -{{end}} +export type {{.Name}} = { + {{- range .Value}} + {{.Name}}?: {{.Number}}; + {{- end}} +}; +{{- end}} + +{{- range .File.MessageType}} +{{- $MessageType := .Name}} -{{range .File.MessageType}} -{{$MessageType := .Name}} {{range .EnumType}} -export type {{$MessageType}}${{.Name}} = {|{{range .Value}} - {{.Name}}?: {{.Number}};{{end}} -|}; -{{end}} +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}} +export type {{$MessageType}}${{.Name}} = { + {{- range .Field}} + {{.Name}}?: {{. | jsType}}; + {{- end}} +}; +{{- end}} + +export type {{.Name}} = { + {{- range .Field}} + {{.Name}}?: {{. | jsType}}; + {{- end}} +}; + +{{- end}} const serializeToBase64 = (byteArray: Uint8Array): string => base64.fromByteArray(byteArray) const deserializeFromBase64 = (base64Encoded: string): Uint8Array => new Uint8Array(base64.toByteArray(base64Encoded)) From 0ce8ebe54beb8bfadbc95377f7cfd6ac3f779526 Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Wed, 11 Jan 2017 15:39:51 +0100 Subject: [PATCH 3/4] Basic getter & setter support --- examples/flow/output/test/test_grpc_js.js | 97 ++++++++++++------- .../{{.File.Package}}_grpc_js.js.tmpl | 18 ++-- helpers.go | 25 +++-- 3 files changed, 88 insertions(+), 52 deletions(-) diff --git a/examples/flow/output/test/test_grpc_js.js b/examples/flow/output/test/test_grpc_js.js index cef1073..deec380 100644 --- a/examples/flow/output/test/test_grpc_js.js +++ b/examples/flow/output/test/test_grpc_js.js @@ -5,37 +5,52 @@ import base64 from 'base64-js' import test_pb from './test_pb' -export type TestEnum = { - ELEMENT_A?: 0; - ELEMENT_B?: 1; -}; +export type TestEnum = {| + ELEMENT_A: 0; + ELEMENT_B: 1; +|}; -export type TestMessage$TestNestedEnum = { - ELEMENT_C?: 0; - ELEMENT_D?: 1; -}; +export type TestMessage$TestNestedEnum = {| + ELEMENT_C: 0; + ELEMENT_D: 1; +|}; export type TestMessage$TestNestedMessage = { - s?: string; - t?: number; + getS: () => string; + setS: (s: string) => void; + getT: () => number; + setT: (t: number) => void; }; export type TestMessage = { - 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; + getA: () => string; + setA: (a: string) => void; + getB: () => number; + setB: (b: number) => void; + getC: () => number; + setC: (c: number) => void; + getD: () => number; + setD: (d: number) => void; + getE: () => number; + setE: (e: number) => void; + getN: () => Array; + setN: (n: Array) => void; + getO: () => Array; + setO: (o: Array) => void; + getP: () => Array; + setP: (p: Array) => void; + getQ: () => Array; + setQ: (q: Array) => void; + getR: () => Array; + setR: (r: Array) => void; + getU: () => TestEnum; + setU: (u: TestEnum) => void; + getV: () => TestMessage$TestNestedEnum; + setV: (v: TestMessage$TestNestedEnum) => void; + getW: () => Array; + setW: (w: Array) => void; }; @@ -43,7 +58,8 @@ export type TestMessage = { export type TestNoStreamRequest = { - message?: TestMessage; + getMessage: () => TestMessage; + setMessage: (message: TestMessage) => void; }; @@ -51,8 +67,10 @@ export type TestNoStreamRequest = { export type TestNoStreamReply = { - message?: TestMessage; - err_msg?: string; + getMessage: () => TestMessage; + setMessage: (message: TestMessage) => void; + getErrMsg: () => string; + setErrMsg: (err_msg: string) => void; }; @@ -60,7 +78,8 @@ export type TestNoStreamReply = { export type TestStreamRequestRequest = { - message?: TestMessage; + getMessage: () => TestMessage; + setMessage: (message: TestMessage) => void; }; @@ -68,8 +87,10 @@ export type TestStreamRequestRequest = { export type TestStreamRequestReply = { - message?: TestMessage; - err_msg?: string; + getMessage: () => TestMessage; + setMessage: (message: TestMessage) => void; + getErrMsg: () => string; + setErrMsg: (err_msg: string) => void; }; @@ -77,7 +98,8 @@ export type TestStreamRequestReply = { export type TestStreamReplyRequest = { - message?: TestMessage; + getMessage: () => TestMessage; + setMessage: (message: TestMessage) => void; }; @@ -85,8 +107,10 @@ export type TestStreamReplyRequest = { export type TestStreamReplyReply = { - message?: TestMessage; - err_msg?: string; + getMessage: () => TestMessage; + setMessage: (message: TestMessage) => void; + getErrMsg: () => string; + setErrMsg: (err_msg: string) => void; }; @@ -94,7 +118,8 @@ export type TestStreamReplyReply = { export type TestStreamBothRequest = { - message?: TestMessage; + getMessage: () => TestMessage; + setMessage: (message: TestMessage) => void; }; @@ -102,8 +127,10 @@ export type TestStreamBothRequest = { export type TestStreamBothReply = { - message?: TestMessage; - err_msg?: string; + getMessage: () => TestMessage; + setMessage: (message: TestMessage) => void; + getErrMsg: () => string; + setErrMsg: (err_msg: string) => void; }; const serializeToBase64 = (byteArray: Uint8Array): string => base64.fromByteArray(byteArray) 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 376c519..4b7eae7 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 @@ -11,35 +11,37 @@ import {{. | replace "/" "_" | trimSuffix ".proto" }}_pb from '../{{. | trimSuff {{- end}} {{range .File.EnumType}} -export type {{.Name}} = { +export type {{.Name}} = {| {{- range .Value}} - {{.Name}}?: {{.Number}}; + {{.Name}}: {{.Number}}; {{- end}} -}; +|}; {{- end}} {{- range .File.MessageType}} {{- $MessageType := .Name}} {{range .EnumType}} -export type {{$MessageType}}${{.Name}} = { +export type {{$MessageType}}${{.Name}} = {| {{- range .Value}} - {{.Name}}?: {{.Number}}; + {{.Name}}: {{.Number}}; {{- end}} -}; +|}; {{- end}} {{range .NestedType}} export type {{$MessageType}}${{.Name}} = { {{- range .Field}} - {{.Name}}?: {{. | jsType}}; + {{.Name | getter}}: () => {{. | jsType}}; + {{.Name | setter}}: ({{.Name}}: {{. | jsType}}) => void; {{- end}} }; {{- end}} export type {{.Name}} = { {{- range .Field}} - {{.Name}}?: {{. | jsType}}; + {{.Name | getter}}: () => {{. | jsType}}; + {{.Name | setter}}: ({{.Name}}: {{. | jsType}}) => void; {{- end}} }; diff --git a/helpers.go b/helpers.go index f7108e9..d4648bf 100644 --- a/helpers.go +++ b/helpers.go @@ -50,19 +50,18 @@ var ProtoHelpersFuncMap = template.FuncMap{ return strings.ToUpper(s[:1]) }, - "lowerCamelCase": func(s string) string { - if len(s) > 1 { - s = xstrings.ToCamelCase(s) - } - return strings.ToLower(s[:1]) + s[1:] - }, - "snakeCase": func(s string) string { - return xstrings.ToSnakeCase(s) - }, "kebabCase": func(s string) string { return strings.Replace(xstrings.ToSnakeCase(s), "_", "-", -1) }, + "getter": func(s string) string { + return toLowerCamelCase("get_" + s) + }, + "setter": func(s string) string { + return toLowerCamelCase("set_" + s) + }, + "lowerCamelCase": toLowerCamelCase, + "snakeCase": xstrings.ToSnakeCase, "getMessageType": getMessageType, "isFieldMessage": isFieldMessage, "isFieldRepeated": isFieldRepeated, @@ -79,6 +78,14 @@ func init() { } } +func toLowerCamelCase(s string) string { + if len(s) > 1 { + s = xstrings.ToCamelCase(s) + } + + return strings.ToLower(s[:1]) + s[1:] +} + func getMessageType(f *descriptor.FileDescriptorProto, name string) *descriptor.DescriptorProto { // name is in the form .packageName.MessageTypeName.InnerMessageTypeName... // e.g. .article.ProductTag From 4149bd5c255728a675c16960fa89efeb3d17fe65 Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Wed, 11 Jan 2017 16:33:03 +0100 Subject: [PATCH 4/4] Support List / Message types --- examples/flow/output/test/test_grpc_js.js | 144 +++++++++++------- .../{{.File.Package}}_grpc_js.js.tmpl | 27 +++- helpers.go | 21 +-- 3 files changed, 113 insertions(+), 79 deletions(-) diff --git a/examples/flow/output/test/test_grpc_js.js b/examples/flow/output/test/test_grpc_js.js index deec380..27088ef 100644 --- a/examples/flow/output/test/test_grpc_js.js +++ b/examples/flow/output/test/test_grpc_js.js @@ -6,51 +6,63 @@ import test_pb from './test_pb' export type TestEnum = {| - ELEMENT_A: 0; - ELEMENT_B: 1; + ELEMENT_A?: 0; + ELEMENT_B?: 1; |}; export type TestMessage$TestNestedEnum = {| - ELEMENT_C: 0; - ELEMENT_D: 1; + ELEMENT_C?: 0; + ELEMENT_D?: 1; |}; export type TestMessage$TestNestedMessage = { - getS: () => string; - setS: (s: string) => void; - getT: () => number; - setT: (t: number) => void; + getS?: () => string; + setS?: (s: string) => void; + getT?: () => number; + setT?: (t: number) => void; }; export type TestMessage = { - getA: () => string; - setA: (a: string) => void; - getB: () => number; - setB: (b: number) => void; - getC: () => number; - setC: (c: number) => void; - getD: () => number; - setD: (d: number) => void; - getE: () => number; - setE: (e: number) => void; - getN: () => Array; - setN: (n: Array) => void; - getO: () => Array; - setO: (o: Array) => void; - getP: () => Array; - setP: (p: Array) => void; - getQ: () => Array; - setQ: (q: Array) => void; - getR: () => Array; - setR: (r: Array) => void; - getU: () => TestEnum; - setU: (u: TestEnum) => void; - getV: () => TestMessage$TestNestedEnum; - setV: (v: TestMessage$TestNestedEnum) => void; - getW: () => Array; - setW: (w: Array) => void; + getA?: () => string; + setA?: (a: string) => void; + getB?: () => number; + setB?: (b: number) => void; + getC?: () => number; + setC?: (c: number) => void; + getD?: () => number; + setD?: (d: number) => void; + getE?: () => number; + setE?: (e: number) => void; + getNList?: () => Array; + setNList?: (n: Array) => void; + addN?: (n: string) => void; + clearNList?: () => void; + getOList?: () => Array; + setOList?: (o: Array) => void; + addO?: (o: number) => void; + clearOList?: () => void; + getPList?: () => Array; + setPList?: (p: Array) => void; + addP?: (p: number) => void; + clearPList?: () => void; + getQList?: () => Array; + setQList?: (q: Array) => void; + addQ?: (q: number) => void; + clearQList?: () => void; + getRList?: () => Array; + setRList?: (r: Array) => void; + addR?: (r: number) => void; + clearRList?: () => void; + getU?: () => TestEnum; + setU?: (u: TestEnum) => void; + getV?: () => TestMessage$TestNestedEnum; + setV?: (v: TestMessage$TestNestedEnum) => void; + getWList?: () => Array; + setWList?: (w: Array) => void; + addW?: (w: TestMessage$TestNestedMessage) => void; + clearWList?: () => void; }; @@ -58,8 +70,10 @@ export type TestMessage = { export type TestNoStreamRequest = { - getMessage: () => TestMessage; - setMessage: (message: TestMessage) => void; + getMessage?: () => TestMessage; + setMessage?: (message: TestMessage) => void; + clearMessage?: () => void; + hasMessage?: () => boolean; }; @@ -67,10 +81,12 @@ export type TestNoStreamRequest = { export type TestNoStreamReply = { - getMessage: () => TestMessage; - setMessage: (message: TestMessage) => void; - getErrMsg: () => string; - setErrMsg: (err_msg: string) => void; + getMessage?: () => TestMessage; + setMessage?: (message: TestMessage) => void; + clearMessage?: () => void; + hasMessage?: () => boolean; + getErrMsg?: () => string; + setErrMsg?: (err_msg: string) => void; }; @@ -78,8 +94,10 @@ export type TestNoStreamReply = { export type TestStreamRequestRequest = { - getMessage: () => TestMessage; - setMessage: (message: TestMessage) => void; + getMessage?: () => TestMessage; + setMessage?: (message: TestMessage) => void; + clearMessage?: () => void; + hasMessage?: () => boolean; }; @@ -87,10 +105,12 @@ export type TestStreamRequestRequest = { export type TestStreamRequestReply = { - getMessage: () => TestMessage; - setMessage: (message: TestMessage) => void; - getErrMsg: () => string; - setErrMsg: (err_msg: string) => void; + getMessage?: () => TestMessage; + setMessage?: (message: TestMessage) => void; + clearMessage?: () => void; + hasMessage?: () => boolean; + getErrMsg?: () => string; + setErrMsg?: (err_msg: string) => void; }; @@ -98,8 +118,10 @@ export type TestStreamRequestReply = { export type TestStreamReplyRequest = { - getMessage: () => TestMessage; - setMessage: (message: TestMessage) => void; + getMessage?: () => TestMessage; + setMessage?: (message: TestMessage) => void; + clearMessage?: () => void; + hasMessage?: () => boolean; }; @@ -107,10 +129,12 @@ export type TestStreamReplyRequest = { export type TestStreamReplyReply = { - getMessage: () => TestMessage; - setMessage: (message: TestMessage) => void; - getErrMsg: () => string; - setErrMsg: (err_msg: string) => void; + getMessage?: () => TestMessage; + setMessage?: (message: TestMessage) => void; + clearMessage?: () => void; + hasMessage?: () => boolean; + getErrMsg?: () => string; + setErrMsg?: (err_msg: string) => void; }; @@ -118,8 +142,10 @@ export type TestStreamReplyReply = { export type TestStreamBothRequest = { - getMessage: () => TestMessage; - setMessage: (message: TestMessage) => void; + getMessage?: () => TestMessage; + setMessage?: (message: TestMessage) => void; + clearMessage?: () => void; + hasMessage?: () => boolean; }; @@ -127,10 +153,12 @@ export type TestStreamBothRequest = { export type TestStreamBothReply = { - getMessage: () => TestMessage; - setMessage: (message: TestMessage) => void; - getErrMsg: () => string; - setErrMsg: (err_msg: string) => void; + getMessage?: () => TestMessage; + setMessage?: (message: TestMessage) => void; + clearMessage?: () => void; + hasMessage?: () => boolean; + getErrMsg?: () => string; + setErrMsg?: (err_msg: string) => void; }; const serializeToBase64 = (byteArray: Uint8Array): string => base64.fromByteArray(byteArray) 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 4b7eae7..e4cc024 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 @@ -10,10 +10,27 @@ import {{$Package}}_pb from './{{$Package}}_pb' import {{. | replace "/" "_" | trimSuffix ".proto" }}_pb from '../{{. | trimSuffix ".proto" }}_pb' {{- end}} +{{- define "fieldMethods"}} + {{- if isFieldRepeated .}} + get{{.Name | camelCase}}List?: () => {{. | jsType}}; + set{{.Name | camelCase}}List?: ({{.Name}}: {{. | jsType}}) => void; + add{{.Name | camelCase}}?: ({{.Name}}: {{. | jsType | trimPrefix "Array<" | trimSuffix ">"}}) => void; + clear{{.Name | camelCase}}List?: () => void; + {{- else}} + get{{.Name | camelCase}}?: () => {{. | jsType}}; + set{{.Name | camelCase}}?: ({{.Name}}: {{. | jsType}}) => void; + + {{- if isFieldMessage .}} + clear{{.Name | camelCase}}?: () => void; + has{{.Name | camelCase}}?: () => boolean; + {{- end}} + {{- end}} +{{- end}} + {{range .File.EnumType}} export type {{.Name}} = {| {{- range .Value}} - {{.Name}}: {{.Number}}; + {{.Name}}?: {{.Number}}; {{- end}} |}; {{- end}} @@ -24,7 +41,7 @@ export type {{.Name}} = {| {{range .EnumType}} export type {{$MessageType}}${{.Name}} = {| {{- range .Value}} - {{.Name}}: {{.Number}}; + {{.Name}}?: {{.Number}}; {{- end}} |}; {{- end}} @@ -32,16 +49,14 @@ export type {{$MessageType}}${{.Name}} = {| {{range .NestedType}} export type {{$MessageType}}${{.Name}} = { {{- range .Field}} - {{.Name | getter}}: () => {{. | jsType}}; - {{.Name | setter}}: ({{.Name}}: {{. | jsType}}) => void; + {{- template "fieldMethods" .}} {{- end}} }; {{- end}} export type {{.Name}} = { {{- range .Field}} - {{.Name | getter}}: () => {{. | jsType}}; - {{.Name | setter}}: ({{.Name}}: {{. | jsType}}) => void; + {{- template "fieldMethods" .}} {{- end}} }; diff --git a/helpers.go b/helpers.go index d4648bf..6aaccf5 100644 --- a/helpers.go +++ b/helpers.go @@ -50,17 +50,16 @@ var ProtoHelpersFuncMap = template.FuncMap{ return strings.ToUpper(s[:1]) }, + "lowerCamelCase": func(s string) string { + if len(s) > 1 { + s = xstrings.ToCamelCase(s) + } + return strings.ToLower(s[:1]) + s[1:] + }, "kebabCase": func(s string) string { return strings.Replace(xstrings.ToSnakeCase(s), "_", "-", -1) }, - "getter": func(s string) string { - return toLowerCamelCase("get_" + s) - }, - "setter": func(s string) string { - return toLowerCamelCase("set_" + s) - }, - "lowerCamelCase": toLowerCamelCase, "snakeCase": xstrings.ToSnakeCase, "getMessageType": getMessageType, "isFieldMessage": isFieldMessage, @@ -78,14 +77,6 @@ func init() { } } -func toLowerCamelCase(s string) string { - if len(s) > 1 { - s = xstrings.ToCamelCase(s) - } - - return strings.ToLower(s[:1]) + s[1:] -} - func getMessageType(f *descriptor.FileDescriptorProto, name string) *descriptor.DescriptorProto { // name is in the form .packageName.MessageTypeName.InnerMessageTypeName... // e.g. .article.ProductTag