mirror of https://github.com/grpc/grpc-node.git
Fix imports of messages defined in other messages, add missing comma
This commit is contained in:
parent
29db7a8f88
commit
d8e035445c
|
@ -81,7 +81,12 @@ function stripLeadingPeriod(name: string) {
|
||||||
return name.startsWith('.') ? name.substring(1) : name;
|
return name.startsWith('.') ? name.substring(1) : name;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getImportPath(to: Protobuf.Type | Protobuf.Enum | Protobuf.Service) {
|
function getImportPath(to: Protobuf.Type | Protobuf.Enum | Protobuf.Service): string {
|
||||||
|
/* If the thing we are importing is defined in a message, it is generated in
|
||||||
|
* the same file as that message. */
|
||||||
|
if (to.parent instanceof Protobuf.Type) {
|
||||||
|
return getImportPath(to.parent);
|
||||||
|
}
|
||||||
return stripLeadingPeriod(to.fullName).replace(/\./g, '/');
|
return stripLeadingPeriod(to.fullName).replace(/\./g, '/');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,14 +115,28 @@ function getImportLine(dependency: Protobuf.Type | Protobuf.Enum | Protobuf.Serv
|
||||||
const filePath = from === undefined ? './' + getImportPath(dependency) : getRelativeImportPath(from, dependency);
|
const filePath = from === undefined ? './' + getImportPath(dependency) : getRelativeImportPath(from, dependency);
|
||||||
const typeInterfaceName = getTypeInterfaceName(dependency);
|
const typeInterfaceName = getTypeInterfaceName(dependency);
|
||||||
let importedTypes: string;
|
let importedTypes: string;
|
||||||
if (dependency instanceof Protobuf.Type) {
|
/* If the dependenc is defined within a message, it will be generated in that
|
||||||
importedTypes = `${dependency.name} as ${typeInterfaceName}, ${dependency.name}__Output as ${typeInterfaceName}__Output`;
|
* message's file and exported using its typeInterfaceName. */
|
||||||
} else if (dependency instanceof Protobuf.Enum) {
|
if (dependency.parent instanceof Protobuf.Type) {
|
||||||
importedTypes = `${dependency.name} as ${typeInterfaceName}`;
|
if (dependency instanceof Protobuf.Type) {
|
||||||
} else if (dependency instanceof Protobuf.Service) {
|
importedTypes = `${typeInterfaceName}, ${typeInterfaceName}__Output`;
|
||||||
importedTypes = `${dependency.name}Client as ${typeInterfaceName}Client`;
|
} else if (dependency instanceof Protobuf.Enum) {
|
||||||
|
importedTypes = `${typeInterfaceName}`;
|
||||||
|
} else if (dependency instanceof Protobuf.Service) {
|
||||||
|
importedTypes = `${typeInterfaceName}Client`;
|
||||||
|
} else {
|
||||||
|
throw new Error('Invalid object passed to getImportLine');
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new Error('Invalid object passed to getImportLine');
|
if (dependency instanceof Protobuf.Type) {
|
||||||
|
importedTypes = `${dependency.name} as ${typeInterfaceName}, ${dependency.name}__Output as ${typeInterfaceName}__Output`;
|
||||||
|
} else if (dependency instanceof Protobuf.Enum) {
|
||||||
|
importedTypes = `${dependency.name} as ${typeInterfaceName}`;
|
||||||
|
} else if (dependency instanceof Protobuf.Service) {
|
||||||
|
importedTypes = `${dependency.name}Client as ${typeInterfaceName}Client`;
|
||||||
|
} else {
|
||||||
|
throw new Error('Invalid object passed to getImportLine');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return `import { ${importedTypes} } from '${filePath}';`
|
return `import { ${importedTypes} } from '${filePath}';`
|
||||||
}
|
}
|
||||||
|
@ -585,7 +604,7 @@ function generateRootFile(formatter: TextFormatter, root: Protobuf.Root, options
|
||||||
formatter.writeLine('type ConstructorArguments<Constructor> = Constructor extends new (...args: infer Args) => any ? Args: never;');
|
formatter.writeLine('type ConstructorArguments<Constructor> = Constructor extends new (...args: infer Args) => any ? Args: never;');
|
||||||
formatter.writeLine('type SubtypeConstructor<Constructor, Subtype> = {');
|
formatter.writeLine('type SubtypeConstructor<Constructor, Subtype> = {');
|
||||||
formatter.writeLine(' new(...args: ConstructorArguments<Constructor>): Subtype;');
|
formatter.writeLine(' new(...args: ConstructorArguments<Constructor>): Subtype;');
|
||||||
formatter.writeLine('}');
|
formatter.writeLine('};');
|
||||||
formatter.writeLine('');
|
formatter.writeLine('');
|
||||||
|
|
||||||
formatter.writeLine('export interface ProtoGrpcType {');
|
formatter.writeLine('export interface ProtoGrpcType {');
|
||||||
|
|
|
@ -7,7 +7,7 @@ import { EchoClient as _google_showcase_v1beta1_EchoClient } from './google/show
|
||||||
type ConstructorArguments<Constructor> = Constructor extends new (...args: infer Args) => any ? Args: never;
|
type ConstructorArguments<Constructor> = Constructor extends new (...args: infer Args) => any ? Args: never;
|
||||||
type SubtypeConstructor<Constructor, Subtype> = {
|
type SubtypeConstructor<Constructor, Subtype> = {
|
||||||
new(...args: ConstructorArguments<Constructor>): Subtype;
|
new(...args: ConstructorArguments<Constructor>): Subtype;
|
||||||
}
|
};
|
||||||
|
|
||||||
export interface ProtoGrpcType {
|
export interface ProtoGrpcType {
|
||||||
google: {
|
google: {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@grpc/proto-loader",
|
"name": "@grpc/proto-loader",
|
||||||
"version": "0.6.0-pre11",
|
"version": "0.6.0-pre12",
|
||||||
"author": "Google Inc.",
|
"author": "Google Inc.",
|
||||||
"contributors": [
|
"contributors": [
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue