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 = {| export type TestEnum = {|
ELEMENT_A: 0; ELEMENT_A?: 0;
ELEMENT_B: 1; ELEMENT_B?: 1;
|}; |};
export type TestMessage$TestNestedEnum = {| export type TestMessage$TestNestedEnum = {|
ELEMENT_C: 0; ELEMENT_C?: 0;
ELEMENT_D: 1; ELEMENT_D?: 1;
|}; |};
export type TestMessage$TestNestedMessage = { export type TestMessage$TestNestedMessage = {
getS: () => string; getS?: () => string;
setS: (s: string) => void; setS?: (s: string) => void;
getT: () => number; getT?: () => number;
setT: (t: number) => void; setT?: (t: number) => void;
}; };
export type TestMessage = { export type TestMessage = {
getA: () => string; getA?: () => string;
setA: (a: string) => void; setA?: (a: string) => void;
getB: () => number; getB?: () => number;
setB: (b: number) => void; setB?: (b: number) => void;
getC: () => number; getC?: () => number;
setC: (c: number) => void; setC?: (c: number) => void;
getD: () => number; getD?: () => number;
setD: (d: number) => void; setD?: (d: number) => void;
getE: () => number; getE?: () => number;
setE: (e: number) => void; setE?: (e: number) => void;
getN: () => Array<string>; getNList?: () => Array<string>;
setN: (n: Array<string>) => void; setNList?: (n: Array<string>) => void;
getO: () => Array<number>; addN?: (n: string) => void;
setO: (o: Array<number>) => void; clearNList?: () => void;
getP: () => Array<number>; getOList?: () => Array<number>;
setP: (p: Array<number>) => void; setOList?: (o: Array<number>) => void;
getQ: () => Array<number>; addO?: (o: number) => void;
setQ: (q: Array<number>) => void; clearOList?: () => void;
getR: () => Array<number>; getPList?: () => Array<number>;
setR: (r: Array<number>) => void; setPList?: (p: Array<number>) => void;
getU: () => TestEnum; addP?: (p: number) => void;
setU: (u: TestEnum) => void; clearPList?: () => void;
getV: () => TestMessage$TestNestedEnum; getQList?: () => Array<number>;
setV: (v: TestMessage$TestNestedEnum) => void; setQList?: (q: Array<number>) => void;
getW: () => Array<TestMessage$TestNestedMessage>; addQ?: (q: number) => void;
setW: (w: Array<TestMessage$TestNestedMessage>) => 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 = { export type TestNoStreamRequest = {
getMessage: () => TestMessage; getMessage?: () => TestMessage;
setMessage: (message: TestMessage) => void; setMessage?: (message: TestMessage) => void;
clearMessage?: () => void;
hasMessage?: () => boolean;
}; };
@ -67,10 +81,12 @@ export type TestNoStreamRequest = {
export type TestNoStreamReply = { export type TestNoStreamReply = {
getMessage: () => TestMessage; getMessage?: () => TestMessage;
setMessage: (message: TestMessage) => void; setMessage?: (message: TestMessage) => void;
getErrMsg: () => string; clearMessage?: () => void;
setErrMsg: (err_msg: string) => void; hasMessage?: () => boolean;
getErrMsg?: () => string;
setErrMsg?: (err_msg: string) => void;
}; };
@ -78,8 +94,10 @@ export type TestNoStreamReply = {
export type TestStreamRequestRequest = { export type TestStreamRequestRequest = {
getMessage: () => TestMessage; getMessage?: () => TestMessage;
setMessage: (message: TestMessage) => void; setMessage?: (message: TestMessage) => void;
clearMessage?: () => void;
hasMessage?: () => boolean;
}; };
@ -87,10 +105,12 @@ export type TestStreamRequestRequest = {
export type TestStreamRequestReply = { export type TestStreamRequestReply = {
getMessage: () => TestMessage; getMessage?: () => TestMessage;
setMessage: (message: TestMessage) => void; setMessage?: (message: TestMessage) => void;
getErrMsg: () => string; clearMessage?: () => void;
setErrMsg: (err_msg: string) => void; hasMessage?: () => boolean;
getErrMsg?: () => string;
setErrMsg?: (err_msg: string) => void;
}; };
@ -98,8 +118,10 @@ export type TestStreamRequestReply = {
export type TestStreamReplyRequest = { export type TestStreamReplyRequest = {
getMessage: () => TestMessage; getMessage?: () => TestMessage;
setMessage: (message: TestMessage) => void; setMessage?: (message: TestMessage) => void;
clearMessage?: () => void;
hasMessage?: () => boolean;
}; };
@ -107,10 +129,12 @@ export type TestStreamReplyRequest = {
export type TestStreamReplyReply = { export type TestStreamReplyReply = {
getMessage: () => TestMessage; getMessage?: () => TestMessage;
setMessage: (message: TestMessage) => void; setMessage?: (message: TestMessage) => void;
getErrMsg: () => string; clearMessage?: () => void;
setErrMsg: (err_msg: string) => void; hasMessage?: () => boolean;
getErrMsg?: () => string;
setErrMsg?: (err_msg: string) => void;
}; };
@ -118,8 +142,10 @@ export type TestStreamReplyReply = {
export type TestStreamBothRequest = { export type TestStreamBothRequest = {
getMessage: () => TestMessage; getMessage?: () => TestMessage;
setMessage: (message: TestMessage) => void; setMessage?: (message: TestMessage) => void;
clearMessage?: () => void;
hasMessage?: () => boolean;
}; };
@ -127,10 +153,12 @@ export type TestStreamBothRequest = {
export type TestStreamBothReply = { export type TestStreamBothReply = {
getMessage: () => TestMessage; getMessage?: () => TestMessage;
setMessage: (message: TestMessage) => void; setMessage?: (message: TestMessage) => void;
getErrMsg: () => string; clearMessage?: () => void;
setErrMsg: (err_msg: string) => void; hasMessage?: () => boolean;
getErrMsg?: () => string;
setErrMsg?: (err_msg: string) => void;
}; };
const serializeToBase64 = (byteArray: Uint8Array): string => base64.fromByteArray(byteArray) 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' import {{. | replace "/" "_" | trimSuffix ".proto" }}_pb from '../{{. | trimSuffix ".proto" }}_pb'
{{- end}} {{- 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}} {{range .File.EnumType}}
export type {{.Name}} = {| export type {{.Name}} = {|
{{- range .Value}} {{- range .Value}}
{{.Name}}: {{.Number}}; {{.Name}}?: {{.Number}};
{{- end}} {{- end}}
|}; |};
{{- end}} {{- end}}
@ -24,7 +41,7 @@ export type {{.Name}} = {|
{{range .EnumType}} {{range .EnumType}}
export type {{$MessageType}}${{.Name}} = {| export type {{$MessageType}}${{.Name}} = {|
{{- range .Value}} {{- range .Value}}
{{.Name}}: {{.Number}}; {{.Name}}?: {{.Number}};
{{- end}} {{- end}}
|}; |};
{{- end}} {{- end}}
@ -32,16 +49,14 @@ export type {{$MessageType}}${{.Name}} = {|
{{range .NestedType}} {{range .NestedType}}
export type {{$MessageType}}${{.Name}} = { export type {{$MessageType}}${{.Name}} = {
{{- range .Field}} {{- range .Field}}
{{.Name | getter}}: () => {{. | jsType}}; {{- template "fieldMethods" .}}
{{.Name | setter}}: ({{.Name}}: {{. | jsType}}) => void;
{{- end}} {{- end}}
}; };
{{- end}} {{- end}}
export type {{.Name}} = { export type {{.Name}} = {
{{- range .Field}} {{- range .Field}}
{{.Name | getter}}: () => {{. | jsType}}; {{- template "fieldMethods" .}}
{{.Name | setter}}: ({{.Name}}: {{. | jsType}}) => void;
{{- end}} {{- end}}
}; };

View File

@ -50,17 +50,16 @@ var ProtoHelpersFuncMap = template.FuncMap{
return strings.ToUpper(s[:1]) 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 { "kebabCase": func(s string) string {
return strings.Replace(xstrings.ToSnakeCase(s), "_", "-", -1) 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, "snakeCase": xstrings.ToSnakeCase,
"getMessageType": getMessageType, "getMessageType": getMessageType,
"isFieldMessage": isFieldMessage, "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 { func getMessageType(f *descriptor.FileDescriptorProto, name string) *descriptor.DescriptorProto {
// name is in the form .packageName.MessageTypeName.InnerMessageTypeName... // name is in the form .packageName.MessageTypeName.InnerMessageTypeName...
// e.g. .article.ProductTag // e.g. .article.ProductTag