Skip to content

Commit

Permalink
fix(typescript): use string values for enum snippets (#4617)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Sep 11, 2024
1 parent 4eaee58 commit eff6477
Show file tree
Hide file tree
Showing 94 changed files with 427 additions and 506 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ export abstract class AbstractGeneratedType<Shape, Context> implements BaseGener
}
for (const example of this.examples) {
const exampleStr =
"@example\n" + getTextOfTsNode(this.buildExample(example.shape, context, { isForComment: true }));
"@example\n" +
getTextOfTsNode(
this.buildExample(example.shape, context, { isForComment: true, isForTypeDeclarationComment: true })
);
groups.push(exampleStr.replaceAll("\n", `\n${EXAMPLE_PREFIX}`));
}
if (groups.length === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,14 @@ export class GeneratedEnumTypeImpl<Context extends BaseContext>
if (enumValue == null) {
throw new Error("No enum with wire value: " + example.value.wireValue);
}
return ts.factory.createPropertyAccessExpression(
this.getReferenceToSelf(context).getExpression(opts),
this.getEnumValueName(enumValue)
);
if (opts.isForTypeDeclarationComment) {
return ts.factory.createPropertyAccessExpression(
this.getReferenceToSelf(context).getExpression(opts),
this.getEnumValueName(enumValue)
);
} else {
return ts.factory.createStringLiteral(example.value.wireValue);
}
}

private getEnumValueName(enumValue: EnumValue): string {
Expand Down
22 changes: 22 additions & 0 deletions generators/typescript/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.39.8] - 2024-08-27

- Fix: The generated enum examples now reference the value of the enum directly instead
of using the enum itself.

### Before

```ts
{
"genre": Imdb.Genre.Humor,
}
```

### After

```ts
{
"genre": "humor"
}
```


## [0.39.7] - 2024-08-27

- Chore: The SDK now produces a `version.ts` file where we export a constant called `SDK_VERSION`.
Expand Down
2 changes: 1 addition & 1 deletion generators/typescript/sdk/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.39.7
0.39.8
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface Reference {
}

export interface GetReferenceOpts {
isForTypeDeclarationComment?: boolean;
isForComment?: boolean;
isForSnippet?: boolean;
}
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 0 additions & 104 deletions seed/ts-express/grpc-proto/.mock/proto/google/api/field_behavior.proto

This file was deleted.

6 changes: 3 additions & 3 deletions seed/ts-sdk/enum/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 9 additions & 14 deletions seed/ts-sdk/enum/reference.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit eff6477

Please sign in to comment.