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

feat: Allow optional suffix for generated files #431

Merged
merged 2 commits into from
Dec 8, 2021

Conversation

sallustfire
Copy link
Contributor

Implements the functionality described in #383 which allows for custom suffixes on generated files, which can be specified via the new plugin option fileSuffix.

@sallustfire
Copy link
Contributor Author

@stephenh Ok, opened the pull request. Please provide feedback.

Due to how the code generation handles imports, it was a little more invasive that I was initially expecting. The ts-poet imp clauses are scattered across the codebase. It might be worth consolidating that logic, but it could be beyond the scope of this issue.

Copy link
Owner

@stephenh stephenh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sallustfire nice, looks great! Let me know if you want to make the imp helper method update; if not, np, I'm good to merge this as-is.

@@ -42,6 +43,13 @@ describe('options', () => {
});
});

it('can set fileSuffix', () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test

@@ -721,7 +722,7 @@ function generateDecode(ctx: Context, fullName: string, messageDesc: DescriptorP
}
} else if (isEnum(field)) {
if (options.stringEnums) {
const fromJson = getEnumMethod(typeMap, field.typeName, 'FromJSON');
const fromJson = getEnumMethod(ctx, field.typeName, 'FromJSON');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, good call passing in the whole ctx instead of both typeMap + options.

src/encode.ts Outdated
const name = wrapperTypeName(typeName);
if (!name) {
return code`${messageToTypeName(ctx, typeName, { keepValueType: true })}.encode(value).finish()`;
}

if (name == 'Timestamp') {
const TimestampValue = imp(`${name}@./google/protobuf/timestamp`);
const TimestampValue = imp(`${name}@./google/protobuf/timestamp${options.fileSuffix}`);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wdyt about making an imp helper method in src/utils that looks something like:

imp(ctx, name, `./google/protobuf/timestamp`);

And then does the stitching together there? Might be nice to centralize the fileSuffix handling.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephenh Ok, I created a helper impProto to consolidate the logic as described, the suffix avoids naming collisions since there are imports of public JS libraries too. All of the imports had template literals with relative paths, so it's really a style call on whether you consider the './' part of the module name or not.

There is also a circular dependency caused by makeUtils if the context is one of the function parameters. Just using the options avoids this.

Anyway, happy to make changes here if you have different preferences.

@stephenh
Copy link
Owner

stephenh commented Dec 8, 2021

@sallustfire cool, looks great!

@stephenh stephenh merged commit d826966 into stephenh:main Dec 8, 2021
stephenh pushed a commit that referenced this pull request Dec 8, 2021
# [1.93.0](v1.92.2...v1.93.0) (2021-12-08)

### Features

* Allow optional suffix for generated files ([#431](#431)) ([d826966](d826966))
@stephenh
Copy link
Owner

stephenh commented Dec 8, 2021

🎉 This PR is included in version 1.93.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@sallustfire sallustfire deleted the file-suffix branch December 8, 2021 19:11
@sallustfire
Copy link
Contributor Author

@stephenh Thanks, that was very fast!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants