Skip to content

Commit

Permalink
Support complex objects as decorator argument values accordproject#918
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamatha1718 committed Jan 31, 2025
1 parent bf96d0d commit ca92f79
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/concerto-core/lib/introspect/decorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,25 @@ class Decorator {
this.handleError(validationOptions.invalidDecorator, err);
}
break;
case 'DecoratorJSON': {
if (argType !== 'object') {
const err = `Decorator ${this.getName()} has invalid decorator argument. Expected object for JSON. Found ${argType}, with value ${JSON.stringify(arg)}`;
this.handleError(validationOptions.invalidator, err);
}
const keys = Object.keys(arg);
keys.forEach(key => {
if (typeof key !=='string') {
const err = `Decorator ${this.getName()} has an invalid key in the JSON object. Expected string, but found ${typeof key}.`;
this.handleError(validationOptions.invalidDecorator, err);
}
const valueType = typeof arg[key];
if (!['string', 'number', 'boolean'].includes(valueType)) {
const err = `Decorator ${this.getName()} has an invalid value type in the JSON object, Expected string, number, or boolean, but found ${valueType} for key ${key}.`;
this.handleError(validationOptions.invalidDecorator, err);
}
});
break;
}
default: {
if (argType !== 'object' || arg?.type !== 'Identifier') {
const err = `Decorator ${this.getName()} has invalid decorator argument. Expected object. Found ${argType}, with value ${JSON.stringify(arg)}`;
Expand Down
18 changes: 18 additions & 0 deletions packages/concerto-cto/lib/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -918,11 +918,29 @@ DecoratorIdentifier =
}
}
DecoratorJSON =
"{" _ pairs:(KeyValue ("," _ KeyValue)*)? _ "}" {
return {
$class: "[email protected]",
value: pairs ? pairs.reduce(obj, pair) => {
obj[pair.key] = pair.value;
return obj;
}, {} : {},
...buildRange(location())
};
}
KeyValue =
key:StringLiteral _ ":" _ value:DecoratorLiteral {
return { key: key.value, value:value };
}
DecoratorLiteral =
DecoratorString
/ DecoratorBoolean
/ DecoratorNumber
/ DecoratorIdentifier
/ DecoratorJSON
DecoratorArguments
= "(" __ first:(d:DecoratorLiteral __ "," __ {return d;})* last:DecoratorLiteral? __ ")" {
Expand Down

0 comments on commit ca92f79

Please sign in to comment.