Support List / Message types

This commit is contained in:
Mathieu Acthernoene 2017-01-11 16:33:03 +01:00
parent 0ce8ebe54b
commit 4149bd5c25
3 changed files with 113 additions and 79 deletions

View File

@ -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<string>;
setN: (n: Array<string>) => void;
getO: () => Array<number>;
setO: (o: Array<number>) => void;
getP: () => Array<number>;
setP: (p: Array<number>) => void;
getQ: () => Array<number>;
setQ: (q: Array<number>) => void;
getR: () => Array<number>;
setR: (r: Array<number>) => void;
getU: () => TestEnum;
setU: (u: TestEnum) => void;
getV: () => TestMessage$TestNestedEnum;
setV: (v: TestMessage$TestNestedEnum) => void;
getW: () => Array<TestMessage$TestNestedMessage>;
setW: (w: Array<TestMessage$TestNestedMessage>) => 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<string>;
setNList?: (n: Array<string>) => void;
addN?: (n: string) => void;
clearNList?: () => void;
getOList?: () => Array<number>;
setOList?: (o: Array<number>) => void;
addO?: (o: number) => void;
clearOList?: () => void;
getPList?: () => Array<number>;
setPList?: (p: Array<number>) => void;
addP?: (p: number) => void;
clearPList?: () => void;
getQList?: () => Array<number>;
setQList?: (q: Array<number>) => void;
addQ?: (q: number) => void;
clearQList?: () => void;
getRList?: () => Array<number>;
setRList?: (r: Array<number>) => void;
addR?: (r: number) => void;
clearRList?: () => void;
getU?: () => TestEnum;
setU?: (u: TestEnum) => void;
getV?: () => TestMessage$TestNestedEnum;
setV?: (v: TestMessage$TestNestedEnum) => void;
getWList?: () => Array<TestMessage$TestNestedMessage>;
setWList?: (w: Array<TestMessage$TestNestedMessage>) => 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)

View File

@ -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}}
};

View File

@ -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