Skip to content

Add default value for internal tags #1589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compiler/src/model/metamodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,10 @@ export class ExternalTag {

export class InternalTag {
kind: 'internal_tag'
tag: string // Name of the property that holds the variant tag
/* Name of the property that holds the variant tag */
tag: string
/* Default value for the variant tag if it's missing */
defaultTag?: string
}

export class Container {
Expand Down
27 changes: 21 additions & 6 deletions compiler/src/model/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ export function parseVariantsTag (jsDoc: JSDoc[]): model.Variants | undefined {
return undefined
}

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

assert(jsDoc, type === 'internal', `Bad variant type: ${type}`)
assert(jsDoc, typeof value === 'string', 'Internal variant requires a tag definition')
const [tag, property] = value.split('=')
assert(jsDoc, tag === 'tag', 'The tag name should be "tag"')
assert(jsDoc, typeof property === 'string', 'The tag property is not defined')

const pairs = parseKeyValues(jsDoc, values, 'tag', 'default')
assert(jsDoc, typeof pairs.tag === 'string', 'Internal variant requires a tag definition')

return {
kind: 'internal_tag',
tag: property.replace(/'/g, '')
tag: pairs.tag,
defaultTag: pairs.default
}
}

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

/**
* Parses an array of "key=value" pairs and validate key names. Values can optionally be enclosed with single
* or double quotes.
*/
export function parseKeyValues (node: Node | Node[], pairs: string[], ...validKeys: string[]): Record<string, string> {
const result = {}
pairs.forEach(item => {
const kv = item.split('=')
assert(node, kv.length === 2, 'Malformed key/value list')
assert(node, validKeys.includes(kv[0]), `Unknown key '${kv[0]}'`)
result[kv[0]] = kv[1].replace(/["']/g, '')
})
return result
}

/**
* Given a declaration, returns true if the declaration
* if defined but never used, false otherwise.
Expand Down
2 changes: 2 additions & 0 deletions output/schema/schema.json

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

2 changes: 1 addition & 1 deletion specification/_types/analysis/analyzers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class WhitespaceAnalyzer {
version?: VersionString
}

/** @variants internal tag='type' */
/** @variants internal tag='type' default='custom' */
export type Analyzer =
| CustomAnalyzer
| FingerprintAnalyzer
Expand Down
2 changes: 1 addition & 1 deletion specification/_types/mapping/Property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class PropertyBase {
fields?: Dictionary<PropertyName, Property>
}

/** @variants internal tag='type' */
/** @variants internal tag='type' default='object' */
export type Property =
| FlattenedProperty
| JoinProperty
Expand Down