Skip to content

Commit c61a7c7

Browse files
authored
Add default value for internal tags (#1589)
1 parent 9ac9b57 commit c61a7c7

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

compiler/src/model/metamodel.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,10 @@ export class ExternalTag {
183183

184184
export class InternalTag {
185185
kind: 'internal_tag'
186-
tag: string // Name of the property that holds the variant tag
186+
/* Name of the property that holds the variant tag */
187+
tag: string
188+
/* Default value for the variant tag if it's missing */
189+
defaultTag?: string
187190
}
188191

189192
export class Container {

compiler/src/model/utils.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ export function parseVariantsTag (jsDoc: JSDoc[]): model.Variants | undefined {
954954
return undefined
955955
}
956956

957-
const [type, value] = tags.variants.split(' ')
957+
const [type, ...values] = tags.variants.split(' ')
958958
if (type === 'external') {
959959
return { kind: 'external_tag' }
960960
}
@@ -964,14 +964,14 @@ export function parseVariantsTag (jsDoc: JSDoc[]): model.Variants | undefined {
964964
}
965965

966966
assert(jsDoc, type === 'internal', `Bad variant type: ${type}`)
967-
assert(jsDoc, typeof value === 'string', 'Internal variant requires a tag definition')
968-
const [tag, property] = value.split('=')
969-
assert(jsDoc, tag === 'tag', 'The tag name should be "tag"')
970-
assert(jsDoc, typeof property === 'string', 'The tag property is not defined')
967+
968+
const pairs = parseKeyValues(jsDoc, values, 'tag', 'default')
969+
assert(jsDoc, typeof pairs.tag === 'string', 'Internal variant requires a tag definition')
971970

972971
return {
973972
kind: 'internal_tag',
974-
tag: property.replace(/'/g, '')
973+
tag: pairs.tag,
974+
defaultTag: pairs.default
975975
}
976976
}
977977

@@ -1000,6 +1000,21 @@ export function parseCommaSeparated (value: string): string[] {
10001000
return value.split(',').map(v => v.trim().replace(/["']/g, ''))
10011001
}
10021002

1003+
/**
1004+
* Parses an array of "key=value" pairs and validate key names. Values can optionally be enclosed with single
1005+
* or double quotes.
1006+
*/
1007+
export function parseKeyValues (node: Node | Node[], pairs: string[], ...validKeys: string[]): Record<string, string> {
1008+
const result = {}
1009+
pairs.forEach(item => {
1010+
const kv = item.split('=')
1011+
assert(node, kv.length === 2, 'Malformed key/value list')
1012+
assert(node, validKeys.includes(kv[0]), `Unknown key '${kv[0]}'`)
1013+
result[kv[0]] = kv[1].replace(/["']/g, '')
1014+
})
1015+
return result
1016+
}
1017+
10031018
/**
10041019
* Given a declaration, returns true if the declaration
10051020
* if defined but never used, false otherwise.

output/schema/schema.json

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

specification/_types/analysis/analyzers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export class WhitespaceAnalyzer {
110110
version?: VersionString
111111
}
112112

113-
/** @variants internal tag='type' */
113+
/** @variants internal tag='type' default='custom' */
114114
export type Analyzer =
115115
| CustomAnalyzer
116116
| FingerprintAnalyzer

specification/_types/mapping/Property.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class PropertyBase {
5050
fields?: Dictionary<PropertyName, Property>
5151
}
5252

53-
/** @variants internal tag='type' */
53+
/** @variants internal tag='type' default='object' */
5454
export type Property =
5555
| FlattenedProperty
5656
| JoinProperty

0 commit comments

Comments
 (0)