Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method Annotations #585

Merged
merged 24 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions docs/docs/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,7 @@ anotherTestObj.isInstance(Test); // true

## Annotations

Annotations are metadata that are applied to classes that by themselves have no impact.
They, however, can provide user defined changes at runtime to given classes.
Annotations are metadata that are applied to classes and methods that by themselves have no impact. They, however, can provide user defined changes at runtime.

```cs
@Annotation
Expand All @@ -659,33 +658,52 @@ class AnnotatedClass {
}
```

Annotations are accessed via the `.annotations` property available on all classes. If annotations
are preset a dictionary is returned, otherwise the `.annotations` property is `nil`.
```cs
class ClassWithMethodAnnotation {
init() {}

@MethodAnnotation
method() {}
}
```

Annotations are access via the `.classAnnotations` and `.methodAnnotations` proprties available on all classes.

For class annotations, the returned data structure returned is a dictionary with keys set to the names of the annotations and their values if present. If no value is provided to the annotation, the value associated with the key is set to `nil`.

For method annotations, the returned data structure is also a dictionary, however the keys are the method names and the values are also dictionaries containing the annotation name and associated values. If no value is provided to the annotation, the value associated with the key is set to `nil`.

```cs
print(AnnotatedClass.annotations); // {"Annotation": nil}
print(AnnotatedClass.classAnnotations); // {"Annotation": nil}
```

```cs
print(ClassWithMethodAnnotation.methodAnnotations); // {"method": {"MethodAnnotation": nil}}
```

Annotations can also be supplied a value, however, the value must be of type: nil, boolean, number or string.

```
```cs
@Annotation("Some extra value!")
class AnnotatedClass {

}

print(AnnotatedClass.annotations); // {"Annotation": "Some extra value!"}
print(AnnotatedClass.classAnnotations); // {"Annotation": "Some extra value!"}
```

Multiple annotations can be supplied to classes.
Multiple annotations can be supplied to classes and methods.

```cs
@Annotation
@AnotherAnnotation(10)
@SomeOtherAnnotation
class AnnotatedClass {

@MethodAnnotation
@AnotherMethodAnnotation(10)
@SomeOtherMethodAnnotation("another one")
method() {}

}
```

**Note**: Annotations are not available on methods.
25 changes: 25 additions & 0 deletions examples/methodAnnotations.du
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@ResourceController("/api/v1")
class AnnotatedClass {
init() {}

@BasicAuth @Get("/releases")
releaseHandler() {
// impl
}

@BasicAuth
@Get("/assets")
private assetHandler() {
// impl
}

private someOtherHandler() {}
}

const a = AnnotatedClass();

print(a._class.classAnnotations);
print(a._class.methodAnnotations);
print(type(a._class.methodAnnotations));
const methodAnnotations = a._class.methodAnnotations;
print(methodAnnotations["releaseHandler"]);
1 change: 1 addition & 0 deletions examples/runExamples.du
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ import "factorial.du";
import "pathWalker.du";
import "structuredLogger.du";
import "templateEngine.du";

6 changes: 3 additions & 3 deletions src/optionals/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ json_value* stringifyJson(DictuVM *vm, Value value) {
}

json_object_push(
json,
key,
stringifyJson(vm, entry->value)
json,
key,
stringifyJson(vm, entry->value)
);

if (!IS_STRING(entry->key)) {
Expand Down
Loading