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