Updating Operon/JSON-values

One really important pattern to learn with Operon is to update the values. Operon has the Update -pattern for this purpose, but it is also possible to use Map -pattern and the object/array:update -functions.

Setting up the test data

We'll start by setting up simple test-data, which we will update in the examples.

Defining the goal

Ok, now we have the simple example-data. Next we want to decide what to update from this structure.

  1. Update the first-person's lname to "baz"
  2. Update all the "fname" fields to "first_name"
  3. Remove all the "parent" -objects
Now we have three simple goals and we can start.

Update value with Update-pattern

The Update-pattern is the proper way to do updates in Operon. Coupled with the deepscan-pattern we get a really powerful combination as we can see from the examples below.

Update the first-person's lname to "baz"

Update all the "fname" fields to "first_name"

Here's the same, but done little bit differently:

Remove all the "parent" -objects

Update value with Map-pattern

Map-pattern iterates through the structure's first level and we can do updates while it does. Let's see how.

Update the first-person's lname to "baz"

As we can see, using Map-pattern for this update results in ridicuosly long statements, which is not Operonic.

Bonus script

Here's a script that we can use to generate test-data and test updates to learn more Operonic-patterns.