@dxos/protobuf-compiler 2.33.5-dev.b7c9d504 → 2.33.5-dev.b9f5bea6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/configure.d.ts +1 -1
- package/dist/src/configure.d.ts.map +1 -1
- package/dist/src/configure.js +2 -2
- package/dist/src/configure.js.map +1 -1
- package/dist/src/generator/declaration-generator.d.ts +1 -1
- package/dist/src/generator/declaration-generator.d.ts.map +1 -1
- package/dist/src/generator/declaration-generator.js +3 -5
- package/dist/src/generator/declaration-generator.js.map +1 -1
- package/dist/src/generator/doc-comment.d.ts +1 -1
- package/dist/src/generator/doc-comment.d.ts.map +1 -1
- package/dist/src/generator/doc-comment.js +1 -3
- package/dist/src/generator/doc-comment.js.map +1 -1
- package/dist/src/generator/enum.d.ts +1 -1
- package/dist/src/generator/enum.d.ts.map +1 -1
- package/dist/src/generator/enum.js +1 -3
- package/dist/src/generator/enum.js.map +1 -1
- package/dist/src/generator/field.d.ts +2 -2
- package/dist/src/generator/field.d.ts.map +1 -1
- package/dist/src/generator/field.js +7 -7
- package/dist/src/generator/field.js.map +1 -1
- package/dist/src/generator/file-generator.d.ts +3 -3
- package/dist/src/generator/file-generator.d.ts.map +1 -1
- package/dist/src/generator/file-generator.js +14 -20
- package/dist/src/generator/file-generator.js.map +1 -1
- package/dist/src/generator/message.d.ts +1 -1
- package/dist/src/generator/message.d.ts.map +1 -1
- package/dist/src/generator/message.js +4 -4
- package/dist/src/generator/message.js.map +1 -1
- package/dist/src/generator/serializer-definition-generator.d.ts +2 -1
- package/dist/src/generator/serializer-definition-generator.d.ts.map +1 -1
- package/dist/src/generator/serializer-definition-generator.js +2 -2
- package/dist/src/generator/serializer-definition-generator.js.map +1 -1
- package/dist/src/generator/service.d.ts +2 -2
- package/dist/src/generator/service.d.ts.map +1 -1
- package/dist/src/generator/service.js +10 -14
- package/dist/src/generator/service.js.map +1 -1
- package/dist/src/generator/types.d.ts +2 -2
- package/dist/src/generator/types.d.ts.map +1 -1
- package/dist/src/generator/types.js +8 -10
- package/dist/src/generator/types.js.map +1 -1
- package/dist/src/module-specifier.js +4 -4
- package/dist/src/module-specifier.js.map +1 -1
- package/dist/src/namespaces.d.ts +10 -10
- package/dist/src/namespaces.d.ts.map +1 -1
- package/dist/src/namespaces.js +26 -34
- package/dist/src/namespaces.js.map +1 -1
- package/dist/src/parser/resolver.d.ts +1 -1
- package/dist/src/parser/resolver.d.ts.map +1 -1
- package/dist/src/parser/resolver.js +2 -2
- package/dist/src/parser/resolver.js.map +1 -1
- package/dist/src/parser/substitutions-parser.d.ts +1 -1
- package/dist/src/parser/substitutions-parser.d.ts.map +1 -1
- package/dist/src/parser/substitutions-parser.js +4 -4
- package/dist/src/parser/substitutions-parser.js.map +1 -1
- package/dist/src/protobuf-json.d.ts +1 -1
- package/dist/src/protobuf-json.d.ts.map +1 -1
- package/dist/src/protobuf-json.js +3 -5
- package/dist/src/protobuf-json.js.map +1 -1
- package/dist/src/type-generator.d.ts +2 -2
- package/dist/src/type-generator.d.ts.map +1 -1
- package/dist/src/type-generator.js +11 -6
- package/dist/src/type-generator.js.map +1 -1
- package/dist/src/util.d.ts +1 -1
- package/dist/src/util.d.ts.map +1 -1
- package/dist/src/util.js +2 -2
- package/dist/src/util.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/configure.ts +2 -2
- package/src/generator/declaration-generator.ts +15 -17
- package/src/generator/doc-comment.ts +4 -6
- package/src/generator/enum.ts +9 -11
- package/src/generator/field.ts +4 -4
- package/src/generator/file-generator.ts +28 -41
- package/src/generator/message.ts +4 -4
- package/src/generator/serializer-definition-generator.ts +2 -6
- package/src/generator/service.ts +22 -26
- package/src/generator/types.ts +16 -18
- package/src/module-specifier.ts +4 -4
- package/src/namespaces.ts +18 -26
- package/src/parser/resolver.ts +2 -2
- package/src/parser/substitutions-parser.ts +4 -4
- package/src/protobuf-json.ts +3 -5
- package/src/type-generator.ts +12 -6
- package/src/util.ts +2 -2
package/src/generator/types.ts
CHANGED
|
@@ -17,20 +17,18 @@ import { SubstitutionsMap } from '../parser';
|
|
|
17
17
|
|
|
18
18
|
const f = ts.factory;
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
f.
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
);
|
|
31
|
-
}
|
|
20
|
+
const createSubstitutionsReference = (type: string): ts.TypeNode => f.createTypeReferenceNode(
|
|
21
|
+
f.createIdentifier('ReturnType'),
|
|
22
|
+
[f.createIndexedAccessTypeNode(
|
|
23
|
+
f.createIndexedAccessTypeNode(
|
|
24
|
+
f.createTypeQueryNode(f.createIdentifier('substitutions')),
|
|
25
|
+
f.createLiteralTypeNode(f.createStringLiteral(type))
|
|
26
|
+
),
|
|
27
|
+
f.createLiteralTypeNode(f.createStringLiteral('decode'))
|
|
28
|
+
)]
|
|
29
|
+
);
|
|
32
30
|
|
|
33
|
-
|
|
31
|
+
const getPrimitiveType = (type: string): ts.TypeNode => {
|
|
34
32
|
switch (type) {
|
|
35
33
|
case 'double':
|
|
36
34
|
return f.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword);
|
|
@@ -65,11 +63,11 @@ function getPrimitiveType (type: string): ts.TypeNode {
|
|
|
65
63
|
default:
|
|
66
64
|
return f.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword);
|
|
67
65
|
}
|
|
68
|
-
}
|
|
66
|
+
};
|
|
69
67
|
|
|
70
68
|
type PbType = protobufjs.Enum | protobufjs.Type | string
|
|
71
69
|
|
|
72
|
-
export
|
|
70
|
+
export const types = (type: PbType, containingObject: protobufjs.ReflectionObject, subs: SubstitutionsMap) => {
|
|
73
71
|
if (typeof type === 'string') {
|
|
74
72
|
return getPrimitiveType(type);
|
|
75
73
|
} else if (type.fullName === '.google.protobuf.Empty') {
|
|
@@ -79,9 +77,9 @@ export function types (type: PbType, containingObject: protobufjs.ReflectionObje
|
|
|
79
77
|
} else {
|
|
80
78
|
return getTypeReference(type, containingObject);
|
|
81
79
|
}
|
|
82
|
-
}
|
|
80
|
+
};
|
|
83
81
|
|
|
84
|
-
export
|
|
82
|
+
export const getTypeReference = (to: protobufjs.ReflectionObject, from?: protobufjs.ReflectionObject) => {
|
|
85
83
|
const toNamespace = getNamespaceName(to);
|
|
86
84
|
const fromNamespace = from && getNamespaceName(from);
|
|
87
85
|
|
|
@@ -92,4 +90,4 @@ export function getTypeReference (to: protobufjs.ReflectionObject, from?: protob
|
|
|
92
90
|
const name = [getSafeNamespaceIdentifier(toNamespace), ...getFullNestedTypeName(to)];
|
|
93
91
|
return f.createTypeReferenceNode(convertNameToIdentifier(name));
|
|
94
92
|
}
|
|
95
|
-
}
|
|
93
|
+
};
|
package/src/module-specifier.ts
CHANGED
|
@@ -49,18 +49,18 @@ export class ModuleSpecifier {
|
|
|
49
49
|
|
|
50
50
|
export const CODEC_MODULE = new ModuleSpecifier('@dxos/codec-protobuf', __dirname);
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
const normalizeRelativePath = (path: string) => {
|
|
53
53
|
if (!path.startsWith('.')) {
|
|
54
54
|
return `./${path}`;
|
|
55
55
|
} else {
|
|
56
56
|
return path;
|
|
57
57
|
}
|
|
58
|
-
}
|
|
58
|
+
};
|
|
59
59
|
|
|
60
|
-
|
|
60
|
+
const removeExtension = (path: string, extension: string) => {
|
|
61
61
|
if (path.endsWith(extension)) {
|
|
62
62
|
return path.slice(0, -extension.length);
|
|
63
63
|
} else {
|
|
64
64
|
return path;
|
|
65
65
|
}
|
|
66
|
-
}
|
|
66
|
+
};
|
package/src/namespaces.ts
CHANGED
|
@@ -10,19 +10,17 @@ const f = ts.factory;
|
|
|
10
10
|
|
|
11
11
|
export type DeclarationFullName = string[];
|
|
12
12
|
|
|
13
|
-
export
|
|
13
|
+
export const getFullNestedTypeName = (type: pb.ReflectionObject): DeclarationFullName => {
|
|
14
14
|
if (type.parent && type.parent instanceof pb.Type) {
|
|
15
15
|
return [...getFullNestedTypeName(type.parent), type.name];
|
|
16
16
|
} else {
|
|
17
17
|
return [type.name];
|
|
18
18
|
}
|
|
19
|
-
}
|
|
19
|
+
};
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
return obj instanceof pb.Type || obj instanceof pb.Enum || obj instanceof pb.Service;
|
|
23
|
-
}
|
|
21
|
+
const isType = (obj: pb.ReflectionObject): obj is (pb.Type | pb.Enum | pb.Service) => obj instanceof pb.Type || obj instanceof pb.Enum || obj instanceof pb.Service;
|
|
24
22
|
|
|
25
|
-
export
|
|
23
|
+
export const getNamespaceName = (type: pb.ReflectionObject): DeclarationFullName => {
|
|
26
24
|
if (type.parent) {
|
|
27
25
|
if (!isType(type)) {
|
|
28
26
|
return [...getNamespaceName(type.parent), type.name];
|
|
@@ -32,9 +30,9 @@ export function getNamespaceName (type: pb.ReflectionObject): DeclarationFullNam
|
|
|
32
30
|
} else {
|
|
33
31
|
return [];
|
|
34
32
|
}
|
|
35
|
-
}
|
|
33
|
+
};
|
|
36
34
|
|
|
37
|
-
export
|
|
35
|
+
export const getRelativeName = (target: DeclarationFullName, base: DeclarationFullName): DeclarationFullName => {
|
|
38
36
|
// TODO(marik-d): Optimization: Remove recursion.
|
|
39
37
|
if (target.length === 1 || base.length === 1) {
|
|
40
38
|
return target;
|
|
@@ -43,42 +41,36 @@ export function getRelativeName (target: DeclarationFullName, base: DeclarationF
|
|
|
43
41
|
} else {
|
|
44
42
|
return target;
|
|
45
43
|
}
|
|
46
|
-
}
|
|
44
|
+
};
|
|
47
45
|
|
|
48
|
-
export
|
|
46
|
+
export const convertNameToIdentifier = (name: DeclarationFullName): ts.QualifiedName | ts.Identifier => {
|
|
49
47
|
if (name.length === 1) {
|
|
50
48
|
return f.createIdentifier(name[0]);
|
|
51
49
|
} else {
|
|
52
50
|
return f.createQualifiedName(convertNameToIdentifier(name.slice(0, -1)), name[name.length - 1]);
|
|
53
51
|
}
|
|
54
|
-
}
|
|
52
|
+
};
|
|
55
53
|
|
|
56
|
-
export
|
|
57
|
-
return name.join('.');
|
|
58
|
-
}
|
|
54
|
+
export const stringifyFullyQualifiedName = (name: DeclarationFullName) => name.join('.');
|
|
59
55
|
|
|
60
|
-
export
|
|
56
|
+
export const normalizeFullyQualifiedName = (name: string) => {
|
|
61
57
|
if (name.startsWith('.')) {
|
|
62
58
|
return name.slice(1);
|
|
63
59
|
} else {
|
|
64
60
|
return name;
|
|
65
61
|
}
|
|
66
|
-
}
|
|
62
|
+
};
|
|
67
63
|
|
|
68
|
-
export
|
|
64
|
+
export const parseFullyQualifiedName = (name: string): DeclarationFullName => {
|
|
69
65
|
const norm = normalizeFullyQualifiedName(name);
|
|
70
66
|
return norm.split('.');
|
|
71
|
-
}
|
|
67
|
+
};
|
|
72
68
|
|
|
73
|
-
export
|
|
74
|
-
return a.length === b.length && a.every((_, i) => a[i] === b[i]);
|
|
75
|
-
}
|
|
69
|
+
export const namesEqual = (a: DeclarationFullName, b: DeclarationFullName) => a.length === b.length && a.every((_, i) => a[i] === b[i]);
|
|
76
70
|
|
|
77
|
-
export
|
|
78
|
-
return name.join('_');
|
|
79
|
-
}
|
|
71
|
+
export const getSafeNamespaceIdentifier = (name: DeclarationFullName) => name.join('_');
|
|
80
72
|
|
|
81
|
-
export
|
|
73
|
+
export const splitSchemaIntoNamespaces = (root: pb.Namespace): Map<string, pb.ReflectionObject[]> => {
|
|
82
74
|
const res = new Map<string, pb.ReflectionObject[]>();
|
|
83
75
|
|
|
84
76
|
const namespace = normalizeFullyQualifiedName(root.fullName);
|
|
@@ -101,4 +93,4 @@ export function splitSchemaIntoNamespaces (root: pb.Namespace): Map<string, pb.R
|
|
|
101
93
|
}
|
|
102
94
|
|
|
103
95
|
return res;
|
|
104
|
-
}
|
|
96
|
+
};
|
package/src/parser/resolver.ts
CHANGED
|
@@ -36,6 +36,6 @@ export function createProtoResolver (original: ProtoResolver): ProtoResolver {
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
const resovler = createProtoResolver(pb.Root.prototype.resolvePath);
|
|
39
|
-
export
|
|
39
|
+
export const registerResolver = () => {
|
|
40
40
|
pb.Root.prototype.resolvePath = resovler;
|
|
41
|
-
}
|
|
41
|
+
};
|
|
@@ -17,19 +17,19 @@ export interface ImportDescriptor {
|
|
|
17
17
|
*/
|
|
18
18
|
export type SubstitutionsMap = Partial<Record<string, string>>
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
const getSubstitutionType = (substitutionProperty: Symbol, typeChecker: TypeChecker) => {
|
|
21
21
|
const substitutionType = typeChecker.getTypeOfSymbolAtLocation(substitutionProperty, substitutionProperty.getValueDeclarationOrThrow());
|
|
22
22
|
|
|
23
23
|
const decode = substitutionType.getPropertyOrThrow('decode');
|
|
24
24
|
const decodeType = typeChecker.getTypeOfSymbolAtLocation(decode, decode.getValueDeclarationOrThrow());
|
|
25
25
|
|
|
26
26
|
return decodeType.getCallSignatures()[0].getReturnType();
|
|
27
|
-
}
|
|
27
|
+
};
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
30
|
* Parse a protobuf-substitutions file and return a map of protobuf FQN => Typescript identifier.
|
|
31
31
|
*/
|
|
32
|
-
export
|
|
32
|
+
export const parseSubstitutionsFile = (fileName: string): SubstitutionsMap => {
|
|
33
33
|
const project = new Project({
|
|
34
34
|
tsConfigFilePath: ts.findConfigFile(fileName, ts.sys.fileExists)
|
|
35
35
|
});
|
|
@@ -49,4 +49,4 @@ export function parseSubstitutionsFile (fileName: string): SubstitutionsMap {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
return substitutions;
|
|
52
|
-
}
|
|
52
|
+
};
|
package/src/protobuf-json.ts
CHANGED
|
@@ -9,7 +9,7 @@ interface ProtobufJson {
|
|
|
9
9
|
[K: string]: any
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
const postprocessProtobufJson = (protobufJson: ProtobufJson): ProtobufJson => {
|
|
13
13
|
if (!protobufJson.nested) {
|
|
14
14
|
return protobufJson;
|
|
15
15
|
}
|
|
@@ -22,8 +22,6 @@ function postprocessProtobufJson (protobufJson: ProtobufJson): ProtobufJson {
|
|
|
22
22
|
...protobufJson,
|
|
23
23
|
nested: newNested
|
|
24
24
|
};
|
|
25
|
-
}
|
|
25
|
+
};
|
|
26
26
|
|
|
27
|
-
export
|
|
28
|
-
return postprocessProtobufJson(root.toJSON({ keepComments: true }));
|
|
29
|
-
}
|
|
27
|
+
export const serializeSchemaToJson = (root: protobuf.Root): any => postprocessProtobufJson(root.toJSON({ keepComments: true }));
|
package/src/type-generator.ts
CHANGED
|
@@ -17,12 +17,18 @@ import { parseSubstitutionsFile, registerResolver, SubstitutionsMap } from './pa
|
|
|
17
17
|
registerResolver();
|
|
18
18
|
preconfigureProtobufjs();
|
|
19
19
|
|
|
20
|
-
export
|
|
20
|
+
export const parseAndGenerateSchema = async (substitutionsModule: ModuleSpecifier | undefined, protoFiles: string[], outDirPath: string) => {
|
|
21
21
|
const substitutions = substitutionsModule ? parseSubstitutionsFile(substitutionsModule.resolve()) : {};
|
|
22
|
-
logger.logParsedSubstitutions(substitutions);
|
|
23
|
-
|
|
24
22
|
const root = await pb.load(protoFiles);
|
|
25
23
|
|
|
24
|
+
for (const fqn of Object.keys(substitutions)) {
|
|
25
|
+
if (!root.lookup(fqn)) {
|
|
26
|
+
throw new Error(`No protobuf definition found matching the substitution: ${fqn}`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
logger.logParsedSubstitutions(substitutions);
|
|
31
|
+
|
|
26
32
|
await generateSchema({
|
|
27
33
|
schema: root,
|
|
28
34
|
outDir: outDirPath,
|
|
@@ -33,7 +39,7 @@ export async function parseAndGenerateSchema (substitutionsModule: ModuleSpecifi
|
|
|
33
39
|
}
|
|
34
40
|
: undefined
|
|
35
41
|
});
|
|
36
|
-
}
|
|
42
|
+
};
|
|
37
43
|
|
|
38
44
|
export interface GenerateSchemaOptions {
|
|
39
45
|
schema: pb.Root
|
|
@@ -47,7 +53,7 @@ export interface GenerateSchemaOptions {
|
|
|
47
53
|
/**
|
|
48
54
|
* Generate typescript definitions for a given schema and write them to `options.outDir`.
|
|
49
55
|
*/
|
|
50
|
-
export
|
|
56
|
+
export const generateSchema = (options: GenerateSchemaOptions) => {
|
|
51
57
|
const namespaces = splitSchemaIntoNamespaces(options.schema);
|
|
52
58
|
|
|
53
59
|
const printer = ts.createPrinter();
|
|
@@ -77,4 +83,4 @@ export function generateSchema (options: GenerateSchemaOptions) {
|
|
|
77
83
|
const source = printer.printFile(generatedSourceFile);
|
|
78
84
|
|
|
79
85
|
writeFileSync(join(options.outDir, 'index.ts'), source);
|
|
80
|
-
}
|
|
86
|
+
};
|
package/src/util.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
export
|
|
5
|
+
export const getFlags = (enumType: any, flags: any) => {
|
|
6
6
|
const res = [];
|
|
7
7
|
for (const variant of Object.keys(enumType)) {
|
|
8
8
|
if (typeof variant !== 'string') {
|
|
@@ -13,4 +13,4 @@ export function getFlags (enumType: any, flags: any) {
|
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
return res;
|
|
16
|
-
}
|
|
16
|
+
};
|