Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle public imports #84

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/compiler/descriptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.GeneratedCodeInfo = exports.SourceCodeInfo = exports.UninterpretedOption = exports.MethodOptions = exports.ServiceOptions = exports.EnumValueOptions = exports.EnumOptions = exports.OneofOptions = exports.FieldOptions = exports.MessageOptions = exports.FileOptions = exports.MethodDescriptorProto = exports.ServiceDescriptorProto = exports.EnumValueDescriptorProto = exports.EnumDescriptorProto = exports.OneofDescriptorProto = exports.FieldDescriptorProto = exports.ExtensionRangeOptions = exports.DescriptorProto = exports.FileDescriptorProto = exports.FileDescriptorSet = void 0;
/**
* Generated by the protoc-gen-ts. DO NOT EDIT!
* compiler version: 3.9.1
* source: src/compiler/descriptor.proto
* git: https://github.com/thesayyn/protoc-gen-ts
* buymeacoffee: https://www.buymeacoffee.com/thesayyn
Expand Down Expand Up @@ -194,9 +195,9 @@ class FileDescriptorProto extends pb_1.Message {
if (this.dependency !== undefined)
writer.writeRepeatedString(3, this.dependency);
if (this.public_dependency !== undefined)
writer.writePackedInt32(10, this.public_dependency);
writer.writeRepeatedInt32(10, this.public_dependency);
if (this.weak_dependency !== undefined)
writer.writePackedInt32(11, this.weak_dependency);
writer.writeRepeatedInt32(11, this.weak_dependency);
if (this.message_type !== undefined)
writer.writeRepeatedMessage(4, this.message_type, (item) => item.serialize(writer));
if (this.enum_type !== undefined)
Expand Down Expand Up @@ -230,10 +231,10 @@ class FileDescriptorProto extends pb_1.Message {
pb_1.Message.addToRepeatedField(message, 3, reader.readString());
break;
case 10:
message.public_dependency = reader.readPackedInt32();
pb_1.Message.addToRepeatedField(message, 10, reader.readInt32());
break;
case 11:
message.weak_dependency = reader.readPackedInt32();
pb_1.Message.addToRepeatedField(message, 11, reader.readInt32());
break;
case 4:
reader.readMessage(message.message_type, () => pb_1.Message.addToRepeatedWrapperField(message, 4, DescriptorProto.deserialize(reader), DescriptorProto));
Expand Down Expand Up @@ -2836,10 +2837,10 @@ exports.SourceCodeInfo = SourceCodeInfo;
break;
switch (reader.getFieldNumber()) {
case 1:
message.path = reader.readPackedInt32();
pb_1.Message.addToRepeatedField(message, 1, reader.readPackedInt32());
break;
case 2:
message.span = reader.readPackedInt32();
pb_1.Message.addToRepeatedField(message, 2, reader.readPackedInt32());
break;
case 3:
message.leading_comments = reader.readString();
Expand Down Expand Up @@ -2990,7 +2991,7 @@ exports.GeneratedCodeInfo = GeneratedCodeInfo;
break;
switch (reader.getFieldNumber()) {
case 1:
message.path = reader.readPackedInt32();
pb_1.Message.addToRepeatedField(message, 1, reader.readPackedInt32());
break;
case 2:
message.source_file = reader.readString();
Expand Down
1 change: 1 addition & 0 deletions src/compiler/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.CodeGeneratorResponse = exports.CodeGeneratorRequest = exports.Version = void 0;
/**
* Generated by the protoc-gen-ts. DO NOT EDIT!
* compiler version: 3.9.1
* source: src/compiler/plugin.proto
* git: https://github.com/thesayyn/protoc-gen-ts
* buymeacoffee: https://www.buymeacoffee.com/thesayyn
Expand Down
17 changes: 11 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const ts = require("typescript");
const type = require("./type");
const descriptor = require("./descriptor");
const rpc = require("./rpc");
const transitive = require("./transitive");

function createImport(identifier, moduleSpecifier) {
return ts.factory.createImportDeclaration(
Expand Down Expand Up @@ -36,16 +37,20 @@ for (const fileDescriptor of request.proto_file) {
// Will keep track of import statements
const importStatements = [];

// Will keep track of export statements for transitive dependencies
const exportStatements = [];

// Create all named imports from dependencies
for (const dependency of fileDescriptor.dependency) {
for (const [index, dependency] of fileDescriptor.dependency.entries()) {
const identifier = ts.factory.createUniqueName("dependency");
const moduleSpecifier = replaceExtension(dependency, "");
type.setIdentifierForDependency(dependency, identifier);
const importedFrom = `./${path.relative(path.dirname(fileDescriptor.name), moduleSpecifier)}`;
importStatements.push(createImport(identifier, importedFrom));
}

transitive.reExport(fileDescriptor, request);

// Create all messages recursively
let statements = [];

Expand Down Expand Up @@ -76,30 +81,30 @@ for (const fileDescriptor of request.proto_file) {
importStatements.push(createImport(grpcIdentifier, process.env.GRPC_PACKAGE_NAME || "@grpc/grpc-js"));
}


const {major, minor, patch} = request.compiler_version || {major: 0, minor: 0, patch: 0};
const { major, minor, patch } = request.compiler_version || { major: 0, minor: 0, patch: 0 };

const doNotEditComment = ts.factory.createJSDocComment(
`Generated by the protoc-gen-ts. DO NOT EDIT!\n` +
`compiler version: ${major}.${minor}.${patch}\n` +
`compiler version: ${major}.${minor}.${patch}\n` +
`source: ${fileDescriptor.name}\n` +
`git: https://github.com/thesayyn/protoc-gen-ts\n` +
`buymeacoffee: https://www.buymeacoffee.com/thesayyn\n`
);

// Wrap statements within the namespace
if (fileDescriptor.package) {

statements = [
doNotEditComment,
...importStatements,
descriptor.createNamespace(fileDescriptor.package, statements),
...exportStatements
]
} else {
statements = [
doNotEditComment,
...importStatements,
...statements
...statements,
...exportStatements
];
}

Expand Down
19 changes: 19 additions & 0 deletions src/transitive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const type = require('./type');
const descriptor = require('./compiler/descriptor');
const ts = require('typescript');
const plugin = require("./compiler/plugin");

/**
*
* @param {descriptor.FileDescriptorProto} descriptor
* @param {plugin.CodeGeneratorRequest} request
*/
function reExport(descriptor, request) {
const statements = [];
for (const index of descriptor.public_dependency) {
const dependency = descriptor.dependency[index];

}
}

module.exports = {reExport}
4 changes: 2 additions & 2 deletions test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ load("//:index.bzl", "ts_proto_library")
proto_library(
name = "protos",
srcs = glob(
include = ["**/*.proto"],
include = ["import/*.proto"],
exclude = ["**/*experimental*.proto"]
)
)
Expand All @@ -34,7 +34,7 @@ ts_proto_library(

ts_project(
name = "test_lib",
srcs = glob(["**/*.ts"]),
srcs = glob(["import/*.ts"]),
tsconfig = {
"compilerOptions": {
"target": "ES2020",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ syntax = "proto3";

package importdirective;

import "test/imported.proto";
import "test/import/imported.proto";
import "test/import/old.proto";

service ImportedService {
rpc ImportedServiceTest(importdirective.Imported) returns (importdirective.Imported.SubMessage);
Expand All @@ -12,4 +13,6 @@ message Message {
importdirective.Imported importedField = 1;
importdirective.Imported.SubMessage submessageField = 2;
importdirective.Imported.SubMessage.MyEnum enumField = 3;
importdirective.PublicMessage pubMessageField = 4;
importdirective.PublicMessage2 pubMessageField2 = 5;
}
File renamed without changes.
9 changes: 9 additions & 0 deletions test/import/old.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";

import public "test/import/public.proto";
import public "test/import/public2.proto";

message OldMarker {
importdirective.PublicMessage pubMessageField = 4;
importdirective.PublicMessage2 pubMessageField2 = 5;
}
11 changes: 11 additions & 0 deletions test/import/oldpkg.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

package importdirective;

import public "test/import/public.proto";
import public "test/import/public2.proto";

message OldMarker {
importdirective.PublicMessage pubMessageField = 4;
importdirective.PublicMessage2 pubMessageField2 = 5;
}
8 changes: 8 additions & 0 deletions test/import/public.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";

package importdirective;


message PublicMessage {
bool pb = 1;
}
7 changes: 7 additions & 0 deletions test/import/public2.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";

package importdirective;

message PublicMessage2 {
bool pb = 1;
}
2 changes: 1 addition & 1 deletion test/map.proto
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
syntax = "proto3";

import "test/imported.proto";
import "test/import/imported.proto";

message Topic {
string link = 2;
Expand Down
2 changes: 1 addition & 1 deletion test/map.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import { Tags, Topic } from "./map";
import { importdirective } from "./imported";
import { importdirective } from "./import/imported";

describe("maps", () => {
it("should serialize as map", () => {
Expand Down