-
Notifications
You must be signed in to change notification settings - Fork 350
/
Copy pathencode.ts
60 lines (48 loc) · 1.9 KB
/
encode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { Context } from './context';
import { code, Code, imp, Import } from 'ts-poet';
import { messageToTypeName, wrapperTypeName } from './types';
import { LongOption } from './options';
export function generateEncoder(ctx: Context, typeName: string): Code {
const name = wrapperTypeName(typeName);
if (!name) {
return code`${messageToTypeName(ctx, typeName)}.encode(value).finish()`;
}
if (name == 'Timestamp') {
const TimestampValue = imp(`${name}@./google/protobuf/timestamp`);
return code`${TimestampValue}.encode(${ctx.utils.toTimestamp}(value)).finish()`;
}
const TypeValue = imp(`${name}@./google/protobuf/wrappers`);
switch (name) {
case 'StringValue':
return code`${TypeValue}.encode({value: value ?? ""}).finish()`;
case 'Int32Value':
case 'UInt32Value':
case 'DoubleValue':
case 'FloatValue':
return code`${TypeValue}.encode({value: value ?? 0}).finish()`;
case 'Int64Value':
case 'UInt64Value':
if (ctx.options.forceLong === LongOption.LONG) {
return code`${TypeValue}.encode({value: value ? value.toNumber(): 0}).finish()`;
}
return code`${TypeValue}.encode({value: value ?? 0 }).finish()`;
case 'BoolValue':
return code`${TypeValue}.encode({value: value ?? false}).finish()`;
case 'BytesValue':
return code`${TypeValue}.encode({value: value ?? new Uint8Array()}).finish()`;
}
throw new Error(`unknown wrapper type: ${name}`);
}
export function generateDecoder(ctx: Context, typeName: string): Code {
let name = wrapperTypeName(typeName);
if (!name) {
return code`${messageToTypeName(ctx, typeName)}.decode(value)`;
}
let TypeValue: Import;
if (name == 'Timestamp') {
TypeValue = imp(`${name}@./google/protobuf/timestamp`);
return code`${TypeValue}.decode(value)`;
}
TypeValue = imp(`${name}@./google/protobuf/wrappers`);
return code`${TypeValue}.decode(value).value`;
}