-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathExportMap.ts
81 lines (69 loc) · 2.73 KB
/
ExportMap.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import {
DescriptorProto,
EnumOptions,
FieldDescriptorProto,
FileDescriptorProto,
MessageOptions
} from "google-protobuf/google/protobuf/descriptor_pb";
import Type = FieldDescriptorProto.Type;
export type ExportMessageEntry = {
pkg: string,
fileName: string,
messageOptions: MessageOptions,
mapFieldOptions?: {
key: [Type, string], // Type, StringTypeName
value: [Type, string], // Type, StringTypeName
}
}
export type ExportEnumEntry = {
pkg: string,
fileName: string,
enumOptions: EnumOptions,
}
export class ExportMap {
messageMap: { [key: string]: ExportMessageEntry } = {};
enumMap: { [key: string]: ExportEnumEntry } = {};
exportNested(scope: string, fileDescriptor: FileDescriptorProto, message: DescriptorProto) {
const messageEntry: ExportMessageEntry = {
pkg: fileDescriptor.getPackage(),
fileName: fileDescriptor.getName(),
messageOptions: message.getOptions(),
mapFieldOptions: message.getOptions() && message.getOptions().getMapEntry() ? {
key: [message.getFieldList()[0].getType(), message.getFieldList()[0].getTypeName().slice(1)],
value: [message.getFieldList()[1].getType(), message.getFieldList()[1].getTypeName().slice(1)],
} : undefined,
};
const entryName = `${scope ? scope + "." : ""}${message.getName()}`;
this.messageMap[entryName] = messageEntry;
message.getNestedTypeList().forEach(nested => {
this.exportNested(scope + "." + message.getName(), fileDescriptor, nested);
});
message.getEnumTypeList().forEach(enumType => {
const identifier = scope + "." + message.getName() + "." + enumType.getName();
this.enumMap[identifier] = {
pkg: fileDescriptor.getPackage(),
fileName: fileDescriptor.getName(),
enumOptions: enumType.getOptions(),
};
});
}
addFileDescriptor(fileDescriptor: FileDescriptorProto) {
const scope = fileDescriptor.getPackage();
fileDescriptor.getMessageTypeList().forEach(messageType => {
this.exportNested(scope, fileDescriptor, messageType);
});
fileDescriptor.getEnumTypeList().forEach(enumType => {
this.enumMap[scope + "." + enumType.getName()] = {
pkg: fileDescriptor.getPackage(),
fileName: fileDescriptor.getName(),
enumOptions: enumType.getOptions(),
};
});
}
getMessage(str: string): ExportMessageEntry | undefined {
return this.messageMap[str];
}
getEnum(str: string): ExportEnumEntry | undefined {
return this.enumMap[str];
}
}