Azure / Azure/azure-functions-java-library

Change design to a Method-level annotation model

Aperta
#51 6 commenti 0 reazioni 0 assegnatari Vedi su GitHub
area:java-functions
Lingua principale
Java
Stelle
45
Fork
49
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Direzione di ricerca

Inizia confrontando il modello di trigger a livello di metodo proposto con l’attuale modello a livello di parametro descritto nell’issue. Determina se il binding dei parametri debba usare un parametro denominato o una nuova annotazione @TriggerObject, quindi valuta la stessa decisione per binding e output. Il lavoro è completato quando il design dell’annotazione impedisce più trigger per metodo e rende più leggibili le firme delle funzioni.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
java
Ambito
developer-experience
Tipo di issue
Refactoring
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.