Skip to content

Commit 20a4be3

Browse files
authored
Add support for arrays server default values (#921)
1 parent 191f736 commit 20a4be3

File tree

5 files changed

+25
-2
lines changed

5 files changed

+25
-2
lines changed

compiler/model/metamodel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export class Property {
127127
docUrl?: string
128128
docId?: string
129129
since?: string
130-
serverDefault?: boolean | string | number
130+
serverDefault?: boolean | string | number | string[] | number[]
131131
deprecation?: Deprecation
132132
/**
133133
* If specified takes precedence over `name` when generating code. `name` is always the value

compiler/model/utils.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ function hoistPropertyAnnotations (property: model.Property, jsDocs: JSDoc[]): v
684684
assert(jsDocs, value.trim() !== '', `Property ${property.name}'s @doc_id is cannot be empty`)
685685
property.docId = value
686686
} else if (tag === 'server_default') {
687-
assert(jsDocs, property.type.kind === 'instance_of' || property.type.kind === 'union_of', `Default values can only be configured for instance_of or union_of types, you are using ${property.type.kind}`)
687+
assert(jsDocs, property.type.kind === 'instance_of' || property.type.kind === 'union_of' || property.type.kind === 'array_of', `Default values can only be configured for instance_of or union_of types, you are using ${property.type.kind}`)
688688
assert(jsDocs, !property.required, 'Default values can only be specified on optional properties')
689689
if (property.type.kind === 'union_of') {
690690
let valueType = ''
@@ -704,6 +704,14 @@ function hoistPropertyAnnotations (property: model.Property, jsDocs: JSDoc[]): v
704704
})
705705
assert(jsDocs, unionTypes.includes(valueType), `The configured server_default value is not present in the union value: ${unionTypes.join(' | ')}`)
706706
property.serverDefault = value
707+
} else if (property.type.kind === 'array_of') {
708+
try {
709+
value = eval(value) // eslint-disable-line
710+
} catch (err) {
711+
assert(jsDocs, false, 'The default value is not formatted properly')
712+
}
713+
assert(jsDocs, Array.isArray(value), 'The default value should be an array')
714+
property.serverDefault = value
707715
} else {
708716
switch (property.type.type.name) {
709717
case 'boolean':

docs/modeling-guide.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,17 @@ class Foo {
431431
}
432432
```
433433

434+
If you need to specify a server default value for an array, you must use the JavaScript array syntax:
435+
436+
```ts
437+
class Foo {
438+
bar: string
439+
/** @server_default ['hello'] */
440+
baz?: string[]
441+
faz: string
442+
}
443+
```
444+
434445
#### `@doc_url`
435446

436447
The documentation url for the parameter.

output/schema/schema.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

specification/ml/_types/Rule.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { FilterRef } from './Filter'
2525
export class DetectionRule {
2626
/**
2727
* The set of actions to be triggered when the rule applies. If more than one action is specified the effects of all actions are combined.
28+
* @server_default ['skip_result']
2829
*/
2930
actions?: RuleAction[]
3031
/**

0 commit comments

Comments
 (0)