Skip to content

Commit f0196fa

Browse files
committed
Use key-value-pairs instead of positional arguments
1 parent 43080f9 commit f0196fa

File tree

22 files changed

+140
-131
lines changed

22 files changed

+140
-131
lines changed

compiler/src/model/metamodel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ export class Container extends VariantBase {
213213
export class Inherits {
214214
type: TypeName
215215
generics?: ValueOf[]
216-
meta?: string[]
216+
meta?: { [p: string]: string }
217217
}
218218

219219
/**

compiler/src/model/utils.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import * as model from './metamodel'
4242
import { EOL } from 'os'
4343
import { dirname, join, sep } from 'path'
4444
import { readFileSync } from 'fs'
45+
import {CgroupCpuStat} from "../../../specification/nodes/_types/Stats";
4546

4647
const docIds: string[][] = readFileSync(join(__dirname, '..', '..', '..', 'specification', '_doc_ids', 'table.csv'), 'utf8')
4748
.split('\n')
@@ -418,17 +419,25 @@ export function modelBehaviors (node: ExpressionWithTypeArguments, jsDocs: JSDoc
418419
const behaviorName = node.getExpression().getText()
419420
const generics = node.getTypeArguments().map(node => modelType(node))
420421

421-
let meta: string[] | undefined
422+
let meta: Map<string, string> | undefined
422423
const tags = parseJsDocTagsAllowDuplicates(jsDocs)
423424
if (tags.behavior_meta !== undefined) {
424-
// Splits a string by comma, but preserves comma in quoted strings
425-
const re = /(?<=")[^"]+?(?="(?:\s*?,|\s*?$))|(?<=(?:^|,)\s*?)(?:[^,"\s][^,"]*[^,"\s])|(?:[^,"\s])(?![^"]*?"(?:\s*?,|\s*?$))(?=\s*?(?:,|$))/g
425+
// Extracts whitespace/comma-separated key-value-pairs with a "=" delimiter and handles double-quotes
426+
const re = /(?<key>[^=\s,]+)=(?<value>"([^"]*)"|([^\s,]+))/g
427+
426428
for (const tag of tags.behavior_meta) {
427429
const id = tag.split(' ')
428430
if (id[0].trim() !== behaviorName) {
429431
continue
430432
}
431-
meta = id.slice(1).join(' ').match(re) as string[]
433+
const matches = [...id.slice(1).join(' ').matchAll(re)];
434+
meta = new Map<string, string>()
435+
for (const match of matches) {
436+
if (match.groups == null) {
437+
continue
438+
}
439+
meta.set(match.groups.key, match.groups.value.replace(/^"(.+(?="$))"$/, '$1'))
440+
}
432441
break
433442
}
434443
}
@@ -439,7 +448,7 @@ export function modelBehaviors (node: ExpressionWithTypeArguments, jsDocs: JSDoc
439448
namespace: getNameSpace(node)
440449
},
441450
...(generics.length > 0 && { generics }),
442-
meta
451+
meta: (meta === undefined) ? undefined : Object.fromEntries(meta)
443452
}
444453
}
445454

compiler/src/steps/validate-model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,10 +653,10 @@ export default async function validateModel (apiModel: model.Model, restSpec: Ma
653653
for (const parent of typeDef.behaviors) {
654654
validateTypeRef(parent.type, parent.generics, openGenerics)
655655

656-
if (parent.type.name === 'AdditionalProperty' && (parent.meta == null || parent.meta.length < 2 || parent.meta.length > 3)) {
656+
if (parent.type.name === 'AdditionalProperty' && (parent.meta == null || parent.meta.name == null || parent.meta.value == null)) {
657657
modelError(`AdditionalProperty behavior for type '${fqn(typeDef.name)}' requires a 'behavior_meta' decorator with at least 2 arguments (name of name, name of value, description)`)
658658
}
659-
if (parent.type.name === 'AdditionalProperties' && (parent.meta == null || parent.meta.length !== 2)) {
659+
if (parent.type.name === 'AdditionalProperties' && (parent.meta == null || parent.meta.name == null || parent.meta.description == null)) {
660660
modelError(`AdditionalProperties behavior for type '${fqn(typeDef.name)}' requires a 'behavior_meta' decorator with exactly 2 arguments (name, description)`)
661661
}
662662
}

docs/behaviors.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ We therefore document the requirement to behave like a dictionary for unknown pr
1616

1717
```ts
1818
/**
19-
* @behavior_meta AdditionalProperties sub_aggregations
19+
* @behavior_meta AdditionalProperties name=sub_aggregations
2020
*/
2121
class IpRangeBucket implements AdditionalProperties<AggregateName, Aggregate> {}
2222
```
@@ -25,7 +25,7 @@ There are also many places where we expect only one runtime-defined property, su
2525

2626
```ts
2727
/**
28-
* @behavior_meta AdditionalProperty field, bounding_box
28+
* @behavior_meta AdditionalProperty name=field value=bounding_box
2929
*/
3030
class GeoBoundingBoxQuery extends QueryBase
3131
implements AdditionalProperty<Field, BoundingBox>

0 commit comments

Comments
 (0)