vendredi 9 février 2024

Diffusion d'une valeur par défaut / Diffusion of a default value

Comme c'est Vendredi, on va faire un exercice plutôt simple (mais qui provient d'un vrai besoin): diffuser une valeur d'un objet vers les objets dans une liste qu'il contient. On pose deux contraintes, histoire de se donner un petit peu de challenge:

  • La solution doit être au maximum indépendante de la structure de l'objet,
  • Les objets de la liste incluse peuvent déjà potentiellement posséder ledit champ qui donc devient prioritaire

Le champ à diffuser est en quelque une valeur par défaut qui ne sera prise en compte dans les objets de la liste incluse que lorsque ceux ci n'en dispose pas déjà. Par exemple, au départ nous avons:

{
"field": "a value",
"flag": "X",
"articles": [
{
"field": "a first value"
},
{
"field": "a second value",
"flag": "Y"
},
{
"field": "a third value"
}
]
}

Et nous voulons obtenir:

{
"field": "a value",
"articles": [
{
"field": "a first value",
"flag": "X"
},
{
"field": "a second value",
"flag": "Y"
},
{
"field": "a third value",
"flag": "X"
}
]
}

Nous pouvons voir que le champs "flag" a disparu de l'objet père pour apparaître dans les objets "articles". Noter que pour l'article "a second value" qui disposait déjà d'un champ "flag", celui-ci n'est pas modifié.

Le code qui fait la transformation est le suivant:

%dw 2.0
output application/json
fun shareOut(o) = o - "flag" - "articles" ++
"articles": o.articles map (a) -> a - "flag" ++
        "flag": (if (a.flag != null) a.flag else o.flag)
---
shareOut(payload)

Ce qui est notable est l'usage des opérateur "-" et "++" sur la structure d'un objet. Le premier permet de "retirer" un champ, le second d'en ajouter un. Le remplacement d'un champ se fait en le retirant plus en l'ajoutant (avec valeur modifiée). C'est ainsi qu'on peut rendre une transformation indépendante de la structure des objets sur lesquels elle s'applique.
_______________________________________________________________

As it's Friday, we're going to do a rather simple exercise (but one that comes from a real need): broadcast a value from an object to the objects in a list it contains. We'll set two constraints, just to give ourselves a bit of a challenge:

  • The solution must be as independent as possible of the object's structure,
  • The objects in the included list may already potentially possess the said field, which therefore takes priority.

The field to be distributed is a kind of default value that will only be taken into account by objects in the included list if they don't already have one. For example, initially we have:

{
"field": "a value",
"flag": "X",
"articles": [
{
"field": "a first value"
},
{
"field": "a second value",
"flag": "Y"
},
{
"field": "a third value"
}
]
}

And we want to get:

{
"field": "a value",
"articles": [
{
"field": "a first value",
"flag": "X"
},
{
"field": "a second value",
"flag": "Y"
},
{
"field": "a third value",
"flag": "X"
}
]
}

We can see that the "flag" field has disappeared from the parent object to appear in the "item" objects. Note that for the "a second value" item, which already had a "flag" field, this has not been modified.

The code that performs the transformation is as follows:

%dw 2.0
output application/json
fun shareOut(o) = o - "flag" - "articles" ++
"articles": o.articles map (a) -> a - "flag" ++
        "flag": (if (a.flag != null) a.flag else o.flag)
---
shareOut(payload)

What's particularly noteworthy is the use of the "-" and "++" operators on the structure of an object. The former is used to "remove" a field, the latter to add one. Replacing a field is done by removing it plus adding it (with modified value). This is how you can make a transformation independent of the structure of the objects to which it applies.

Aucun commentaire:

Enregistrer un commentaire

Pourquoi ce blog ? / Why this blog?

Mulesoft est un ESB du monde Salesforce utilisé pour construire des flots permettant aux pièces logicielles d'un Système d'Informati...