From 0ce8ebe54beb8bfadbc95377f7cfd6ac3f779526 Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Wed, 11 Jan 2017 15:39:51 +0100 Subject: [PATCH] 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