Skip to content

Commit e0d1082

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

File tree

22 files changed

+139
-131
lines changed

22 files changed

+139
-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: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -418,17 +418,25 @@ export function modelBehaviors (node: ExpressionWithTypeArguments, jsDocs: JSDoc
418418
const behaviorName = node.getExpression().getText()
419419
const generics = node.getTypeArguments().map(node => modelType(node))
420420

421-
let meta: string[] | undefined
421+
let meta: Map<string, string> | undefined
422422
const tags = parseJsDocTagsAllowDuplicates(jsDocs)
423423
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
424+
// Extracts whitespace/comma-separated key-value-pairs with a "=" delimiter and handles double-quotes
425+
const re = /(?<key>[^=\s,]+)=(?<value>"([^"]*)"|([^\s,]+))/g
426+
426427
for (const tag of tags.behavior_meta) {
427428
const id = tag.split(' ')
428429
if (id[0].trim() !== behaviorName) {
429430
continue
430431
}
431-
meta = id.slice(1).join(' ').match(re) as string[]
432+
const matches = [...id.slice(1).join(' ').matchAll(re)]
433+
meta = new Map<string, string>()
434+
for (const match of matches) {
435+
if (match.groups == null) {
436+
continue
437+
}
438+
meta.set(match.groups.key, match.groups.value.replace(/^"(.+(?="$))"$/, '$1'))
439+
}
432440
break
433441
}
434442
}
@@ -439,7 +447,7 @@ export function modelBehaviors (node: ExpressionWithTypeArguments, jsDocs: JSDoc
439447
namespace: getNameSpace(node)
440448
},
441449
...(generics.length > 0 && { generics }),
442-
meta
450+
meta: (meta === undefined) ? undefined : Object.fromEntries(meta)
443451
}
444452
}
445453

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)