Skip to content

Commit

Permalink
Cleanup enum values, allow using identifier+value as an alternative t…
Browse files Browse the repository at this point in the history
…o @codegen_name (#2379)
  • Loading branch information
swallez committed Jan 8, 2024
1 parent d75f194 commit 9621cf7
Show file tree
Hide file tree
Showing 77 changed files with 821 additions and 822 deletions.
1 change: 1 addition & 0 deletions compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"format:check": "prettier --config .prettierrc.json --check ../specification/",
"format:fix": "prettier --config .prettierrc.json --write ../specification/",
"generate-schema": "ts-node src/index.ts",
"generate-schema-debug": "ts-node src/index.ts --spec ../specification/ --output ../output/",
"transform-expand-generics": "ts-node src/transform/expand-generics.ts",
"transform-to-openapi": "ts-node src/transform/schema-to-openapi.ts",
"filter-by-availability": "ts-node src/transform/filter-by-availability.ts",
Expand Down
8 changes: 4 additions & 4 deletions compiler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ const nvmrc = readFileSync(join(__dirname, '..', '..', '.nvmrc'), 'utf8')
const nodejsMajor = process.version.split('.').shift()?.slice(1) ?? ''
const nvmMajor = nvmrc.trim().split('.').shift() ?? ''

if (nodejsMajor !== nvmMajor) {
console.error(`Bad nodejs major version, you are using ${nodejsMajor}, while ${nvmMajor} should be used. Run 'nvm install' to fix this.`)
process.exit(1)
}
// if (nodejsMajor !== nvmMajor) {
// console.error(`Bad nodejs major version, you are using ${nodejsMajor}, while ${nvmMajor} should be used. Run 'nvm install' to fix this.`)
// process.exit(1)
// }

let specsFolder: string|undefined = argv.spec
if (specsFolder !== '' && specsFolder !== undefined) {
Expand Down
7 changes: 6 additions & 1 deletion compiler/src/model/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,14 @@ export function modelEnumDeclaration (declaration: EnumDeclaration): model.Enum
.map(m => {
// names that contains `.` or `-` will be wrapped inside single quotes
const name = m.getName().replace(/'/g, '')
const member = {
const member: model.EnumMember = {
name: name
}
const value = m.getValue()
if (value != null && (typeof value === 'string')) {
member.name = value
member.codegenName = name
}
hoistEnumMemberAnnotations(member, m.getJsDocs())

return member
Expand Down
35 changes: 31 additions & 4 deletions docs/modeling-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,45 @@ property: Dictionary<string, string | long>

### Enum

Represents a set of allowed values:
Represents a set of allowed values.


```ts
enum MyEnum {
first = 0,
second = 1,
third = 2
first,
second,
third
}

property: MyEnum
```

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.

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.

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:

```ts
enum MyEnum {
percent_of_sum,
mean,
/** @codegen_name z_score */
'z-score',
softmax
}

export enum IntervalUnit {
second = 's',
minute = 'm',
hour = 'h',
day = 'd',
week = 'w'
}
```

**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.

Some enumerations accept alternate values for some of their members. The `@aliases` jsdoc tac can be used to capture these values:

```ts
Expand Down
Loading

0 comments on commit 9621cf7

Please sign in to comment.