Skip to content

Commit

Permalink
✨ feat: Add Feature to Contract
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Jul 3, 2023
1 parent 6731179 commit 112f059
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 31 deletions.
49 changes: 34 additions & 15 deletions scripts/node/jaspiler/jaspiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,39 +203,53 @@ const JTTypeKind = Object.freeze({

function canBeIgnoredByAnnotations(annotations, context) {
if (annotations) {
const annotation = annotations.find(annotation => annotation.annotationType.toString() == 'JaspilerContract.Ignore');
const annotation = findAnnotation(annotations, 'JaspilerContract.Ignore');
return evaluateAnnotationAttribute(annotation, context, 'condition', true, false);
}
return false;
}

function evaluateAnnotationAttribute(annotation, context, attributeName, defaultValueForAttributeFound, defaultValueForAttributeNotFound) {
if (annotation) {
const attributeValue = getAnnotationAttributeValueByName(annotation, attributeName);
if (attributeValue && attributeValue.kind == JTKind.STRING_LITERAL) {
const script = new vm.Script(attributeValue.value);
if (!(context instanceof Object)) {
context = {};
}
if (!vm.isContext(context)) {
vm.createContext(context);
}
return script.runInContext(context);
}
return defaultValueForAttributeFound;
}
return defaultValueForAttributeNotFound;
}

function findAnnotation(annotations, annotationName) {
if (annotations) {
return annotations.find(annotation => annotation.annotationType.toString() == annotationName);
}
return undefined;
}

function getAnnotationAttributeValueByName(annotation, attributeName) {
if (annotation) {
const args = annotation.arguments;
for (let i = 0; i < args.length; ++i) {
const arg = args[i];
if (arg.kind == JTKind.ASSIGNMENT && arg.variable.toString() == attributeName) {
const expression = arg.expression;
if (expression.kind == JTKind.STRING_LITERAL) {
const script = new vm.Script(arg.expression.value);
if (!(context instanceof Object)) {
context = {};
}
if (!vm.isContext(context)) {
vm.createContext(context);
}
return script.runInContext(context);
}
return arg.expression;
}
}
return defaultValueForAttributeFound;
}
return defaultValueForAttributeNotFound;
return undefined;
}

function getChangeInstructionByAnnotations(annotations, context) {
if (annotations) {
const annotation = annotations.find(annotation => annotation.annotationType.toString() == 'JaspilerContract.Change');
const annotation = findAnnotation(annotations, 'JaspilerContract.Change');
if (evaluateAnnotationAttribute(annotation, context, 'condition', true, false)) {
return evaluateAnnotationAttribute(annotation, context, 'instruction', undefined, undefined);
}
Expand Down Expand Up @@ -343,6 +357,11 @@ const PluginContractChangeMethod = Object.freeze({
});

module.exports = Object.freeze({
helpers: Object.freeze({
evaluateAnnotationAttribute: evaluateAnnotationAttribute,
findAnnotation: findAnnotation,
getAnnotationAttributeValueByName: getAnnotationAttributeValueByName,
}),
JTBodyKind: JTBodyKind,
JTCaseKind: JTCaseKind,
JTKind: JTKind,
Expand Down
40 changes: 24 additions & 16 deletions src/main/java/com/caoccao/jaspiler/JaspilerContract.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,6 @@ public boolean isNoChange() {
*/
@Retention(RetentionPolicy.SOURCE)
public @interface Change {
/**
* Identifier.
*
* @return the identifier
* @since 0.1.0
*/
String id() default "";

/**
* Condition is a JavaScript expression / statement / statements that returns a boolean
* indicating whether certain condition is true or false.
Expand All @@ -85,6 +77,30 @@ public boolean isNoChange() {
String instruction() default "";
}

/**
* The interface Feature provides .
*
* @since 0.1.0
*/
@Retention(RetentionPolicy.SOURCE)
public @interface Feature {
/**
* Feature identifier.
*
* @return the feature identifier
* @since 0.1.0
*/
String id() default "";

/**
* Feature name.
*
* @return the feature name
* @since 0.1.0
*/
String name() default "";
}

/**
* The annotation Ignore provides conditional ignore over decorated class, method, property, etc.
*
Expand All @@ -100,13 +116,5 @@ public boolean isNoChange() {
* @since 0.1.0
*/
String condition() default "";

/**
* Identifier.
*
* @return the identifier
* @since 0.1.0
*/
String id() default "";
}
}

0 comments on commit 112f059

Please sign in to comment.