Basic getter & setter support

This commit is contained in:
Mathieu Acthernoene 2017-01-11 15:39:51 +01:00
parent b3b9aec439
commit 0ce8ebe54b
3 changed files with 88 additions and 52 deletions

View File

@ -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<string>;
o?: Array<number>;
p?: Array<number>;
q?: Array<number>;
r?: Array<number>;
u?: TestEnum;
v?: TestMessage$TestNestedEnum;
w?: Array<TestMessage$TestNestedMessage>;
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;
};
@ -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)

View File

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

View File

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