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' 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 = {
s?: string; getS: () => string;
t?: number; setS: (s: string) => void;
getT: () => number;
setT: (t: number) => void;
}; };
export type TestMessage = { export type TestMessage = {
a?: string; getA: () => string;
b?: number; setA: (a: string) => void;
c?: number; getB: () => number;
d?: number; setB: (b: number) => void;
e?: number; getC: () => number;
n?: Array<string>; setC: (c: number) => void;
o?: Array<number>; getD: () => number;
p?: Array<number>; setD: (d: number) => void;
q?: Array<number>; getE: () => number;
r?: Array<number>; setE: (e: number) => void;
u?: TestEnum; getN: () => Array<string>;
v?: TestMessage$TestNestedEnum; setN: (n: Array<string>) => void;
w?: Array<TestMessage$TestNestedMessage>; 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 = { export type TestNoStreamRequest = {
message?: TestMessage; getMessage: () => TestMessage;
setMessage: (message: TestMessage) => void;
}; };
@ -51,8 +67,10 @@ export type TestNoStreamRequest = {
export type TestNoStreamReply = { export type TestNoStreamReply = {
message?: TestMessage; getMessage: () => TestMessage;
err_msg?: string; setMessage: (message: TestMessage) => void;
getErrMsg: () => string;
setErrMsg: (err_msg: string) => void;
}; };
@ -60,7 +78,8 @@ export type TestNoStreamReply = {
export type TestStreamRequestRequest = { export type TestStreamRequestRequest = {
message?: TestMessage; getMessage: () => TestMessage;
setMessage: (message: TestMessage) => void;
}; };
@ -68,8 +87,10 @@ export type TestStreamRequestRequest = {
export type TestStreamRequestReply = { export type TestStreamRequestReply = {
message?: TestMessage; getMessage: () => TestMessage;
err_msg?: string; setMessage: (message: TestMessage) => void;
getErrMsg: () => string;
setErrMsg: (err_msg: string) => void;
}; };
@ -77,7 +98,8 @@ export type TestStreamRequestReply = {
export type TestStreamReplyRequest = { export type TestStreamReplyRequest = {
message?: TestMessage; getMessage: () => TestMessage;
setMessage: (message: TestMessage) => void;
}; };
@ -85,8 +107,10 @@ export type TestStreamReplyRequest = {
export type TestStreamReplyReply = { export type TestStreamReplyReply = {
message?: TestMessage; getMessage: () => TestMessage;
err_msg?: string; setMessage: (message: TestMessage) => void;
getErrMsg: () => string;
setErrMsg: (err_msg: string) => void;
}; };
@ -94,7 +118,8 @@ export type TestStreamReplyReply = {
export type TestStreamBothRequest = { export type TestStreamBothRequest = {
message?: TestMessage; getMessage: () => TestMessage;
setMessage: (message: TestMessage) => void;
}; };
@ -102,8 +127,10 @@ export type TestStreamBothRequest = {
export type TestStreamBothReply = { export type TestStreamBothReply = {
message?: TestMessage; getMessage: () => TestMessage;
err_msg?: string; setMessage: (message: TestMessage) => void;
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

@ -11,35 +11,37 @@ import {{. | replace "/" "_" | trimSuffix ".proto" }}_pb from '../{{. | trimSuff
{{- 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}}
{{- range .File.MessageType}} {{- range .File.MessageType}}
{{- $MessageType := .Name}} {{- $MessageType := .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}}
{{range .NestedType}} {{range .NestedType}}
export type {{$MessageType}}${{.Name}} = { export type {{$MessageType}}${{.Name}} = {
{{- range .Field}} {{- range .Field}}
{{.Name}}?: {{. | jsType}}; {{.Name | getter}}: () => {{. | jsType}};
{{.Name | setter}}: ({{.Name}}: {{. | jsType}}) => void;
{{- end}} {{- end}}
}; };
{{- end}} {{- end}}
export type {{.Name}} = { export type {{.Name}} = {
{{- range .Field}} {{- range .Field}}
{{.Name}}?: {{. | jsType}}; {{.Name | getter}}: () => {{. | jsType}};
{{.Name | setter}}: ({{.Name}}: {{. | jsType}}) => void;
{{- end}} {{- end}}
}; };

View File

@ -50,19 +50,18 @@ 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:]
},
"snakeCase": func(s string) string {
return xstrings.ToSnakeCase(s)
},
"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,
"getMessageType": getMessageType, "getMessageType": getMessageType,
"isFieldMessage": isFieldMessage, "isFieldMessage": isFieldMessage,
"isFieldRepeated": isFieldRepeated, "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 { 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