Skip to content

Commit 7e14069

Browse files
authored
Cleanup enum values, allow using identifier+value as an alternative to @codegen_name (#2379)
1 parent a019f7a commit 7e14069

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+816
-820
lines changed

compiler/src/model/utils.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,14 @@ export function modelEnumDeclaration (declaration: EnumDeclaration): model.Enum
440440
.map(m => {
441441
// names that contains `.` or `-` will be wrapped inside single quotes
442442
const name = m.getName().replace(/'/g, '')
443-
const member = {
443+
const member: model.EnumMember = {
444444
name: name
445445
}
446+
const value = m.getValue()
447+
if (value != null && (typeof value === 'string')) {
448+
member.name = value
449+
member.codegenName = name
450+
}
446451
hoistEnumMemberAnnotations(member, m.getJsDocs())
447452

448453
return member

docs/modeling-guide.md

+31-4
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,45 @@ property: Dictionary<string, string | long>
7575

7676
### Enum
7777

78-
Represents a set of allowed values:
78+
Represents a set of allowed values.
79+
7980

8081
```ts
8182
enum MyEnum {
82-
first = 0,
83-
second = 1,
84-
third = 2
83+
first,
84+
second,
85+
third
8586
}
8687

8788
property: MyEnum
8889
```
8990

91+
Note that you don't have to provide both identifiers and values as is common in TypeScript. When there's only an identifier, the API specification compiler will use it for both the identifier and the value of enum members.
92+
93+
Also do not use identifiers and values for the sole purpose of providing upper-case identifiers (e.g. `FOO = 'foo'`). Each language generator will use the identifier casing that is expected by that language.
94+
95+
Some enumerations contain values that aren't identifiers, or that are not explicit enough. In that case you can either assign values to enum members or use the `@codegen_name` jsdoc tag to define the identifier that will be used by code generators:
96+
97+
```ts
98+
enum MyEnum {
99+
percent_of_sum,
100+
mean,
101+
/** @codegen_name z_score */
102+
'z-score',
103+
softmax
104+
}
105+
106+
export enum IntervalUnit {
107+
second = 's',
108+
minute = 'm',
109+
hour = 'h',
110+
day = 'd',
111+
week = 'w'
112+
}
113+
```
114+
115+
**Use custom identifiers for enum members sparingly**: we want to keep identifiers as close as possible to their value in JSON payloads to that usesr can easily map values found in the Elasticsearch reference documentation to code identifiers in the client libraries.
116+
90117
Some enumerations accept alternate values for some of their members. The `@aliases` jsdoc tac can be used to capture these values:
91118

92119
```ts

0 commit comments

Comments
 (0)