Day #6 with Cloud Workflows: arrays and dictionaries

So far, in this series of articles on Cloud Workflows, we have used simple data types, like strings, numbers and boolean values. However, it’s possible to use more complex data structures, like arrays and dictionaries. In this new episode, we’re going to use those new structures.

Arrays can be defined inline (like anArray) or spanning over several lines (like anotherArray):

- assignment:
        - anArray: ["a", "b", "c"]
        - anotherArray:
            - one
            - two
- output:
    return: ${anArray[0] + anotherArray[1]}

The output step will return the string “atwo”.

For dictionaries, you can define them as follows:

- assignment:
        - person:
            firstname: "Guillaume"
            lastname: "Laforge"
            age: 43
            kids: ["Marion", "Erine"]
- output:
    return: ${person.firstname + " and " +[1]}

The output step will return the string “Guillaume and Erine”. 

Notice that we nested an array within a dictionary. So you can easily create dictionaries containing arrays, containing other dictionaries, etc, just like any JSON or YAML structures. 

In the example we were able to access the second kid of the person, mixing both the field (dot) and index (square brackets) notations to access fields of our dictionary, and elements of our array.

This video shows both arrays and dictionaries in action:

In the coming articles, we’ll see that such data structures are handy for dealing with API endpoint calls.

Day #5 with Cloud Workflows: visualizing the structure of your workflow definition

So far, in our Cloud Workflows series, we have seen some of the YAML syntax for defining workflows. However, steps are defined after each other, as a series of step definitions, but in spite of the jump instructions, the conditionals, you don’t really see visually what is going to be the next potential step in a workflow execution. 

Fortunately, a new UI enhancement has landed in the Google Cloud Console: the ability to visualize a workflow definition with a graph, when you’re editing the definition. Furthermore, the graph is updated in quasi real-time as you make updates to the definition.

Let’s see this in action in the video below:

Thanks to this visualization, it’s easier to further understand how your workflow definition is structured, how executions operate. You can more easily track which steps follows a particular step.


Day #4 with Cloud Workflows: jumping with switch conditions

In the previous articles about Google Cloud Workflows, we talked about how to assign variables, create expressions, and also how to jump from a step to another. It’s time to combine both aspects to understand how we can do conditional jumps, thanks to the switch instruction.

Let’s start with a first step defining a variable, whose value we’ll use in our switch condition:

- assignement:
        - number: 42

Then we’re going to create our second step that will use a switch instruction, with an expression:

- evaluate:
        - condition: ${number > 100}
          next: highValue
        - condition: ${number < 100}
          next: lowValue
    next: end

We define two conditions with two expressions, checking if the number is above or below 100, then we go to a different step (highValue or lowValue steps). If none of the conditions are met, we go to the end of the workflow (or we could return some value or raise some error).

We also need our two steps to go to:

- highValue:
    return: "It's high!"
- lowValue:
    return: "It's rather low!"

And we’re done! If the number is 42, like in our case, the execution of the workflow will go through the lowValue step, and return the string saying that it’s a low value.

Thanks to switch conditionals, with expressions and jumps, we can have non-linear logic in our workflow definitions. In upcoming articles, we will also have a look at how to use more complex data structures like arrays and dictionaries, and how to define inputs and outputs.

Day #3 with Cloud Workflows: variable assignment and expressions

Now that we have multiple steps in our workflow definition, let’s see how we can pass data around, from a step to another.

In a step, you can assign values to variables. Those values can be ints, doubles, strings, or booleans (and also null). Use the assign keyword as follows:

- assignments:
        - two: 2
        - pi: 3.14
        - message: "Hello"
        - bool: True

Those variables are available in the whole scope of the workflow, and can be accessed in other steps. So let’s see how we can do something with those variables. Let’s add a second step to our workflow definition:

- twoPi:
    return: ${"Twice pi is " + string(two * pi)}

We are using the ${} notation to create an expression. We’re multiplying two numbers, we’re converting them to a string, and we’re concatenating two strings together, to get our final value. 

Note that not all operations are allowed on all types, so you might need to do some conversions with built-in conversion functions like the string() function in our example. There are all sorts of arithmetic operators or boolean logic operators.

For more information, you can read about variable assignments, data types, and expressions. Next time, we’ll also have a look at more complex data types.

Day #2 with Cloud Workflows: a workflow is made of steps or even jumps!

Let’s continue our discovery of Cloud Workflows!

Yesterday, we discovered the UI of Cloud Workflows. We created our first workflow. We started with a single step, returning a greeting message:

- sayHello:
    return: Hello from Cloud Workflows!

A workflow definition is made of steps. But not just one! You can create several steps. In YAML, the structure of your workflow will be something like:

- stepOne:
    # do something
- stepTwo:
    # do something else
- sayHello:
    return: Hello from Cloud Workflows!

By default, steps are executed in the order they appear, from top to bottom. The execution will finish when either you return a value, or you reach the final step. If there’s no return statement, a null value is returned as result of the workflow execution.

A small step for a workflow execution, but you can also do a jump between steps! For that, you’ll use the next instruction:

- stepOne:
    next: stepTwo
- stepThree:
    next: sayHello
- stepTwo:
    next: stepThree
- sayHello:
    return: Hello from Cloud Workflows!

Here, we jump between steps, back and forth, before going to the final step that will return a value, and thus finish the execution of our workflow.

Of course, we can go beyond a linear series of steps, and in subsequent articles, we’ll see how we can create conditional jumps and switches, for more complex logic, and how we can pass some data and values between steps.

© 2012 Guillaume Laforge | The views and opinions expressed here are mine and don't reflect the ones from my employer.