Azure / Azure/azure-functions-java-library
Change design to a Method-level annotation model
- Lenguaje dominante
- Java
- Estrellas
- 45
- Forks
- 49
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
Currently, triggers and bindings are defined at the parameter-level. Given the amount of config parameters that the annotations may require, or that the user may want to set, this approach often makes the code extremely hard to read.
Only one trigger is allowed on a (Java method) function, making Trigger a definitive candidate to be a Method annotation, instead of a method-parameter annotation.
Example of Cosmos DB trigger:
```java
@FunctionName("cosmosDBMonitor")
public void cosmosDbProcessor(
@CosmosDBTrigger(name = "items",
databaseName = "ToDoList",
collectionName = "Items",
leaseCollectionName = "leases",
reateLeaseCollectionIfNotExists = true,
connectionStringSetting = "AzureCosmosDBConnection") String[] items,
final ExecutionContext context ) {
context.getLogger().info(items.length + "item(s) is/are changed.");
}
```
In the example above, it is hard to quickly find where the actual method body starts.
A better approach would be to move the annotation to the method-level:
```java
@FunctionName("cosmosDBMonitor")
@CosmosDBTrigger(name = "items", databaseName = "ToDoList",
collectionName = "Items", leaseCollectionName = "leases",
createLeaseCollectionIfNotExists = true,
connectionStringSetting = "AzureCosmosDBConnection",
parameter = "items")
public void cosmosDbProcessor(String[] items, final ExecutionContext context ) {
context.getLogger().info(items.length + "item(s) is/are changed.");
}
```
Moving the annotation to the method level, requires a new way to bind the trigger and the input object. This can be done in two ways:
1. Add a new annotation parameter called `parameter` where the user defines the name of the method parameter that will take the input
2. Add a new method-parameter annotation type to link the trigger with the input object.
The code snippet above covers option 1.
For option 2, an example would be:
```java
@FunctionName("cosmosDBMonitor")
@CosmosDBTrigger(name = "items", databaseName = "ToDoList",
collectionName = "Items", leaseCollectionName = "leases",
createLeaseCollectionIfNotExists = true,
connectionStringSetting = "AzureCosmosDBConnection")
public void cosmosDbProcessor(@TriggerObject String[] items, final ExecutionContext context ) {
context.getLogger().info(items.length + "item(s) is/are changed.");
}
```
In the above example for option 2, a new annotation called `@TriggerObject` is defined to bind the trigger with the method-parameter.
This structure provides two benefits:
1. Prevents developers from attempting to add two triggers to the same method, and only finding out if this works or note after they try to run on Azure Functions (whether local or on Azure).
2. Makes the function method body easier to read.
The same approach should be considered for other types: Bindings, and Outputs.
Request for feedback: @JonathanGiles, @pragnagopa, @asavaritayal, @eduardolaureano, @jeffhollan
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
Línea de trabajo
Comience comparando el modelo de activación a nivel de método propuesto con el modelo actual a nivel de parámetro descrito en el issue. Determine si el enlace de parámetros debe usar un parámetro con nombre o una nueva anotación @TriggerObject; después, considere la misma decisión para los enlaces y las salidas. Se considera terminado cuando el diseño de la anotación impide múltiples activadores por método y facilita la lectura de las firmas de las funciones.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- java
- Área
- developer-experience
- Tipo de issue
- Refactorización
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 25/100