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

Add keep_enum_prefix plugin option #187

Merged
merged 3 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ Available plugin options:
with our option (ts.exclude_options). Set this option if you are certain you do not want
to include any options at all.

- "keep_enum_prefix"
By default, if all enum values share a prefix that corresponds with the enum's name,
the prefix is dropped from the value names. Set this option if you want to disable this behavior
and keep the prefix intact.

- "client_none"
Do not generate rpc clients.
Only applies to services that do *not* use the option `ts.client`.
Expand Down Expand Up @@ -510,7 +515,7 @@ enum Foo {
}
```

The prefix "FOO_" is dropped in TypeScript:
The prefix "FOO_" is dropped in TypeScript (unless `keep_enum_prefix` option is provided to the plugin):

```typescript
enum Foo {
Expand Down Expand Up @@ -1120,7 +1125,7 @@ string-based 64 bit integer support (see [Bigint support](#bigint-support)).



#### Unknown field handling
#### Unknown field handling

Unknown fields occur when a message has been created with a newer version
of a proto file, or when proto2 extensions are used (see [proto2 support](#proto2-support)).
Expand Down
1 change: 1 addition & 0 deletions packages/plugin/spec/interpreter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('interpreter', function () {
synthesizeEnumZeroValue: 'UNSPECIFIED$',
oneofKindDiscriminator: 'oneofKind',
forceExcludeAllOptions: false,
keepEnumPrefix: false,
});
const messageType = interpreter.getMessageType('spec.LongsMessage');

Expand Down
5 changes: 4 additions & 1 deletion packages/plugin/src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class Interpreter {
oneofKindDiscriminator: string,
synthesizeEnumZeroValue: string | false,
forceExcludeAllOptions: boolean,
keepEnumPrefix: boolean,
},
) {
}
Expand Down Expand Up @@ -543,7 +544,9 @@ export class Interpreter {


protected buildEnumInfo(descriptor: EnumDescriptorProto): rt.EnumInfo {
let sharedPrefix = this.registry.findEnumSharedPrefix(descriptor, `${descriptor.name}`);
let sharedPrefix = this.options.keepEnumPrefix
? undefined
: this.registry.findEnumSharedPrefix(descriptor, `${descriptor.name}`);
let hasZero = descriptor.value.some(v => v.number === 0);
let builder = new RuntimeEnumBuilder();
if (!hasZero && typeof this.options.synthesizeEnumZeroValue == 'string') {
Expand Down
2 changes: 2 additions & 0 deletions packages/plugin/src/our-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export interface InternalOptions {
readonly runtimeRpcImportPath: string;
readonly runtimeImportPath: string;
readonly forceExcludeAllOptions: boolean;
readonly keepEnumPrefix: boolean;
}

export function makeInternalOptions(options?: Partial<InternalOptions>): InternalOptions {
Expand All @@ -211,6 +212,7 @@ const defaultOptions: InternalOptions = {
angularCoreImportPath: '@angular/core',
runtimeImportPath: '@protobuf-ts/runtime',
forceExcludeAllOptions: false,
keepEnumPrefix: false,
} as const;


Expand Down
6 changes: 6 additions & 0 deletions packages/plugin/src/protobufts-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ export class ProtobuftsPlugin extends PluginBase<OutFile> {
"with our option (ts.exclude_options). Set this option if you are certain you do not want \n" +
"to include any options at all.",
},
keep_enum_prefix: {
description: "By default, if all enum values share a prefix that corresponds with the enum's name, \n" +
"the prefix is dropped from the value names. Set this option if you want to disable this behavior \n" +
"and keep the prefix intact.",
},

// client
client_none: {
Expand Down Expand Up @@ -154,6 +159,7 @@ export class ProtobuftsPlugin extends PluginBase<OutFile> {
emitAngularAnnotations: params.enable_angular_annotations,
normalLongType: params.long_type_string ? rt.LongType.STRING : params.long_type_number ? rt.LongType.NUMBER : rt.LongType.BIGINT,
forceExcludeAllOptions: params.force_exclude_all_options,
keepEnumPrefix: params.keep_enum_prefix,
}),
registry = DescriptorRegistry.createFrom(request),
symbols = new SymbolTable(),
Expand Down