Skip to content

Commit e2937cf

Browse files
committed
Use key-value-pairs instead of positional arguments
1 parent f871d2f commit e2937cf

File tree

21 files changed

+43
-35
lines changed

21 files changed

+43
-35
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
@@ -644,10 +644,10 @@ export default async function validateModel (apiModel: model.Model, restSpec: Ma
644644
for (const parent of typeDef.behaviors) {
645645
validateTypeRef(parent.type, parent.generics, openGenerics)
646646

647-
if (parent.type.name === 'AdditionalProperty' && (parent.meta == null || parent.meta.length < 2 || parent.meta.length > 3)) {
647+
if (parent.type.name === 'AdditionalProperty' && (parent.meta == null || parent.meta.name == null || parent.meta.value == null)) {
648648
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)`)
649649
}
650-
if (parent.type.name === 'AdditionalProperties' && (parent.meta == null || parent.meta.length !== 2)) {
650+
if (parent.type.name === 'AdditionalProperties' && (parent.meta == null || parent.meta.name == null || parent.meta.description == null)) {
651651
modelError(`AdditionalProperties behavior for type '${fqn(typeDef.name)}' requires a 'behavior_meta' decorator with exactly 2 arguments (name, description)`)
652652
}
653653
}

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>

specification/_global/search/_types/suggester.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export class TermSuggestOption {
9999
}
100100

101101
/**
102-
* @behavior_meta AdditionalProperties suggesters, "The named suggesters"
102+
* @behavior_meta AdditionalProperties name=suggesters description="The named suggesters"
103103
*/
104104
export class Suggester implements AdditionalProperties<string, FieldSuggester> {
105105
/** Global suggest text, to avoid repetition when the same text is used in several suggesters */

specification/_types/Errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { AdditionalProperties } from '@spec_utils/behaviors'
2626
* Cause and details about a request failure. This class defines the properties common to all error types.
2727
* Additional details are also provided, that depend on the error type.
2828
*
29-
* @behavior_meta AdditionalProperties metadata, "Additional details about the error"
29+
* @behavior_meta AdditionalProperties name=metadata description="Additional details about the error"
3030
*/
3131
export class ErrorCause
3232
implements AdditionalProperties<string, UserDefinedValue>

specification/_types/aggregations/Aggregate.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ export class MultiBucketAggregateBase<TBucket> extends AggregateBase {
331331
/**
332332
* Base type for multi-bucket aggregation results that can hold sub-aggregations results.
333333
*
334-
* @behavior_meta AdditionalProperties aggregations, "Nested aggregations"
334+
* @behavior_meta AdditionalProperties name=aggregations description="Nested aggregations"
335335
*/
336336
export class MultiBucketBase
337337
implements AdditionalProperties<AggregateName, Aggregate>
@@ -475,7 +475,7 @@ export class MultiTermsBucket extends MultiBucketBase {
475475
/**
476476
* Base type for single-bucket aggregation results that can hold sub-aggregations results.
477477
*
478-
* @behavior_meta AdditionalProperties aggregations, "Nested aggregations"
478+
* @behavior_meta AdditionalProperties name=aggregations description="Nested aggregations"
479479
*/
480480
export class SingleBucketAggregateBase
481481
extends AggregateBase
@@ -662,7 +662,7 @@ export class TopHitsAggregate extends AggregateBase {
662662

663663
/**
664664
* @variant name=inference
665-
* @behavior_meta AdditionalProperties data, "Additional data"
665+
* @behavior_meta AdditionalProperties name=data description="Additional data"
666666
*/
667667
// This is a union with widely different fields, many of them being runtime-defined. We mimic below the few fields
668668
// present in `ParsedInference` with an additional properties spillover to not loose any data.

specification/_types/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ export enum WaitForEvents {
318318
}
319319

320320
/**
321-
* @behavior_meta AdditionalProperties metadata, "Document metadata"
321+
* @behavior_meta AdditionalProperties name=metadata description="Document metadata"
322322
*/
323323
// Additional properties are the meta fields
324324
export class InlineGet<TDocument>

specification/_types/query_dsl/compound.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,21 +180,21 @@ export class DecayFunctionBase {
180180
}
181181

182182
/**
183-
* @behavior_meta AdditionalProperty field, placement
183+
* @behavior_meta AdditionalProperty name=field value=placement
184184
*/
185185
export class NumericDecayFunction
186186
extends DecayFunctionBase
187187
implements AdditionalProperty<Field, DecayPlacement<double, double>> {}
188188

189189
/**
190-
* @behavior_meta AdditionalProperty field, placement
190+
* @behavior_meta AdditionalProperty name=field value=placement
191191
*/
192192
export class DateDecayFunction
193193
extends DecayFunctionBase
194194
implements AdditionalProperty<Field, DecayPlacement<DateMath, Duration>> {}
195195

196196
/**
197-
* @behavior_meta AdditionalProperty field, placement
197+
* @behavior_meta AdditionalProperty name=field value=placement
198198
*/
199199
export class GeoDecayFunction
200200
extends DecayFunctionBase

specification/_types/query_dsl/geo.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { FieldLookup, QueryBase } from './abstractions'
3030
import { Field } from '@_types/common'
3131

3232
/**
33-
* @behavior_meta AdditionalProperty field, bounding_box
33+
* @behavior_meta AdditionalProperty name=field value=bounding_box
3434
*/
3535
export class GeoBoundingBoxQuery
3636
extends QueryBase
@@ -58,7 +58,7 @@ export enum GeoExecution {
5858
}
5959

6060
/**
61-
* @behavior_meta AdditionalProperty field, location
61+
* @behavior_meta AdditionalProperty name=field value=location
6262
*/
6363
export class GeoDistanceQuery
6464
extends QueryBase
@@ -96,7 +96,7 @@ export class GeoPolygonPoints {
9696

9797
/**
9898
* @deprecated 7.12.0 Use geo-shape instead.
99-
* @behavior_meta AdditionalProperty field, polygon
99+
* @behavior_meta AdditionalProperty name=field value=polygon
100100
*/
101101
export class GeoPolygonQuery
102102
extends QueryBase
@@ -126,7 +126,7 @@ export class GeoShapeFieldQuery {
126126
}
127127

128128
/**
129-
* @behavior_meta AdditionalProperty field, shape
129+
* @behavior_meta AdditionalProperty name=field value=shape
130130
*/
131131
// GeoShape query doesn't follow the common pattern of having a single field-name property
132132
// holding also the query base fields (boost and _name)

specification/_types/query_dsl/specialized.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ export class ScriptScoreQuery extends QueryBase {
340340
}
341341

342342
/**
343-
* @behavior_meta AdditionalProperty field, shape
343+
* @behavior_meta AdditionalProperty name=field value=shape
344344
*/
345345
// Shape query doesn't follow the common pattern of having a single field-name property
346346
// holding also the query base fields (boost and _name)

specification/_types/query_dsl/term.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ export class TermQuery extends QueryBase {
252252
}
253253

254254
/**
255-
* @behavior_meta AdditionalProperty field, term
255+
* @behavior_meta AdditionalProperty name=field value=term
256256
*/
257257
export class TermsQuery
258258
extends QueryBase

specification/_types/sort.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class ScoreSort {
5757
}
5858

5959
/**
60-
* @behavior_meta AdditionalProperty field, location
60+
* @behavior_meta AdditionalProperty name=field value=location
6161
*/
6262
export class GeoDistanceSort
6363
implements AdditionalProperty<Field, GeoLocation | GeoLocation[]>
@@ -86,7 +86,7 @@ export enum ScriptSortType {
8686
/**
8787
* @doc_id sort-search-results
8888
* @variants container
89-
* @behavior_meta AdditionalProperty field, sort
89+
* @behavior_meta AdditionalProperty name=field value=sort
9090
*/
9191
export class SortOptions implements AdditionalProperty<Field, FieldSort> {
9292
_score?: ScoreSort

specification/indices/_types/IndexSettings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class RetentionLease {
6969
/**
7070
* @doc_id index-modules-settings
7171
*
72-
* @behavior_meta AdditionalProperties other_settings, "Additional settings not covered in this type.
72+
* @behavior_meta AdditionalProperties name=other_settings description="Additional settings not covered in this type."
7373
*/
7474
export class IndexSettings
7575
implements AdditionalProperties<string, UserDefinedValue>

specification/indices/analyze/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class CharFilterDetail {
5050

5151
// Additional properties are attributes that can be set by plugin-defined tokenizers
5252
/**
53-
* @behavior_meta AdditionalProperties attributes, "Additional tokenizer-specific attributes"
53+
* @behavior_meta AdditionalProperties name=attributes description="Additional tokenizer-specific attributes"
5454
*/
5555
export class ExplainAnalyzeToken
5656
implements AdditionalProperties<string, UserDefinedValue>

specification/indices/field_usage_stats/IndicesFieldUsageStatsResponse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class Response {
3030
}
3131

3232
/**
33-
* @behavior_meta AdditionalProperties stats, "Per index statistics"
33+
* @behavior_meta AdditionalProperties name=stats description="Per index statistics"
3434
*/
3535
export class FieldsUsageBody
3636
implements AdditionalProperties<IndexName, UsageStatsIndex>

specification/indices/shard_stores/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class IndicesShardStores {
2828
}
2929

3030
/**
31-
* @behavior_meta AdditionalProperty node_id, node
31+
* @behavior_meta AdditionalProperty name=node_id value=node
3232
*/
3333
export class ShardStore implements AdditionalProperty<NodeId, ShardStoreNode> {
3434
allocation: ShardStoreAllocation

specification/ingest/simulate/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class Document {
5757
/**
5858
* The simulated document, with optional metadata.
5959
*
60-
* @behavior_meta AdditionalProperties metadata, "Additional metadata"
60+
* @behavior_meta AdditionalProperties name=metadata description="Additional metadata"
6161
*/
6262
export class DocumentSimulation
6363
implements AdditionalProperties<string, string>

specification/nodes/info/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export class NodeInfoRepositoriesUrl {
171171
}
172172

173173
/**
174-
* @behavior_meta AdditionalProperties settings, "Additional or alternative settings"
174+
* @behavior_meta AdditionalProperties name=settings description="Additional or alternative settings"
175175
*/
176176
export class NodeInfoDiscover
177177
implements AdditionalProperties<string, UserDefinedValue>

specification/watcher/_types/Conditions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class ArrayCompareOpParams {
3030
}
3131

3232
/**
33-
* @behavior_meta AdditionalProperty operator, params
33+
* @behavior_meta AdditionalProperty name=operator value=params
3434
*/
3535
export class ArrayCompareCondition
3636
implements AdditionalProperty<ConditionOp, ArrayCompareOpParams>

typescript-generator/src/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
/**

0 commit comments

Comments
 (0)