Calculated default value for a variable

Objectives

In this section we will reuse the value of a variable for the default value of another variable.

We will first build the https_proxy family which will be used to illustrate this functionality.

Prerequisites

  • We assume that Rougail’s library is installed on your computer.

  • It is possible to retrieve the current state of the various Rougail files manipulated in this tutorial step by checking out the corresponding tag of the rougail-tutorials git repository. Each tag corresponds to a stage of progress in the tutorial. Of course, you can also decide to copy/paste or download the tutorial files contents while following the tutorial steps.

If you want to follow this tutorial with the help of the corresponding rougail-tutorials git repository, this workshop page corresponds to the tags v1.1_040 to v1.1_041 in the repository.

git clone https://forge.cloud.silique.fr/stove/rougail-tutorials.git
git switch --detach v1.1_040

HTTPS family

We have split the definition of the manual family into two specific files, the 10-manual.yml and the 20-manual.yml structure file:

.
└── firefox
    ├── 00-proxy.yml
    ├── 10-manual.yml
    └── 20-manual.yml


We will continue to complete the 20-manual.yml structure file, with variables entirely equivalent to those found in the http_proxy family from the 10-manual.yml structure file. Until now, we only had the use_for_https boolean variable in this structure file. Now we are going to define an address and a port in the https_proxy family:

The updated manual family in the firefox/20-manual.yml structure file with the https_proxy family.
 1%YAML 1.2
 2---
 3version: 1.1
 4
 5manual:
 6
 7  use_for_https: true  # Also use this proxy for HTTPS
 8
 9  https_proxy:  # HTTPS Proxy
10
11    address:
12      description: HTTPS address
13      type: domainname
14      params:
15        allow_ip: true
16
17    port:
18      description: HTTPS Port
19      type: port
20      default: 8080
21...

A default value calculated from another variable

For those who follow the tutorial with the help of the git repository

Now you need to checkout the v1.1_041 version:

git switch --detach v1.1_041

A contextualized default value

A contextualized default value is a default value that changes according to the overall context of the configuration. A contextualized default value can come from, as we see here, a copy of the value of another variable.

The dynamic setting of a default value can be expressed this way: the default value is a pointer to another variable’s value. Here, the defaut value of manual.https_proxy.address points to the value of manual.http_proxy.address.

This is the same thing for the default value of the manual.https_proxy.port variable, which points to the manual.http_proxy.port value.

Note

In the following we will see that the manual.https_proxy.address type is domainname. Indeed, this domainname type comes from the type of the variable it points to.

Reminder: here is the HTTP firefox/10-manual.yml structure file:

The firefox/10-manual.yml structure file with the http_proxy family and the disabled property
 1%YAML 1.2
 2---
 3version: 1.1
 4
 5manual:  # Manual proxy configuration
 6
 7  http_proxy:  # HTTP Proxy
 8
 9    address:
10      description: HTTP address
11      type: domainname
12      params:
13        allow_ip: true
14
15    port:
16      description: HTTP Port
17      type: port
18      default: 8080
19...

And here is the firefox/20-manual.yml structure file where the calculated default values are:

The firefox/20-manual.yml structure file with the hidden property on the https_proxy family.
 1%YAML 1.2
 2---
 3version: 1.1
 4
 5manual:
 6
 7  use_for_https: true  # Also use this proxy for HTTPS
 8
 9  https_proxy:  # HTTPS Proxy
10
11    address:
12      description: HTTPS address
13      default:
14        variable: __.http_proxy.address
15
16    port:
17      description: HTTPS Port
18      default:
19        variable: __.http_proxy.port
20...

We can see here that the address variable’s default value is conditionned by the __.http_proxy.address variable’s value. The target variable is manual.http_proxy.address.

Note

The __. notation means the parent path of the current subfamily path.

In the python quasi algorithmic notation we could say that:

__.http_proxy.address == http_proxy.address

This is true only because in our use case http_proxy.address is located in the same manual subfamiliy than https_proxy.address.

We then say that the manual.https_proxy.address and the manual.https_proxy.port default values are calculated.

calculated

We say that a variable’s value or a default variable’s value is calculated when there is a pointer which refers to another variable’s value.

Other types of calculations exists, in which this type of behavior does not occur (the “pointer” behavior, notably type copying).

The other types of calculation we will be explained later in this tutorial.

We’re going to load some user data to see what happens:

The config/01/config.yml user data
1---
2proxy_mode: Manual proxy configuration
3manual:
4  http_proxy:
5    address: http.proxy.net
6    port: 3128
7  use_for_https: false
8  https_proxy:
9    address: https.proxy.net

Let’s launch the Rougail CLI on it:

rougail -m firefox/ -u yaml -yf config/01/config.yml

We have this result:

╭────────────── Caption ───────────────╮
│ Variable Default value               │
│          Modified value              │
│          (⏳ Original default value) │
╰──────────────────────────────────────╯
Variables:
┣━━ 📓 proxy_mode (Configure Proxy Access to the Internet): Manual proxy 
configuration ◀ loaded from the YAML file "config/01/config.yml" (⏳ No 
proxy)
┗━━ 📂 manual (Manual proxy configuration)
    ┣━━ 📂 http_proxy (HTTP Proxy)
    ┣━━ 📓 address (HTTP address): http.proxy.net ◀ loaded from the YAML 
    file "config/01/config.yml"
    ┗━━ 📓 port (HTTP Port): 3128 ◀ loaded from the YAML file 
        "config/01/config.yml" (⏳ 8080)
    ┣━━ 📓 use_for_https (Also use this proxy for HTTPS): false ◀ loaded from 
    the YAML file "config/01/config.yml" (⏳ true)
    ┗━━ 📂 https_proxy (HTTPS Proxy)
        ┣━━ 📓 address (HTTPS address): https.proxy.net ◀ loaded from the YAML 
        file "config/01/config.yml" (⏳ http.proxy.net)
        ┗━━ 📓 port (HTTPS Port): 3128

Notice that the default value is not changed when some values are settled from the user data file.

The https_proxy variable’s value is indeed modified, but the default value is still a calculated value (so the default value is indeed the value of the variable http_proxy.address, i.e., "http.proxy.net"), while the value defined by the user is https.proxy.net.

With the standard output of the Rougail CLI, the terminal display output, we can see the default value of the variable.

By interpreting the results of this standard output, we can see that even if a value has been assigned to this variable (meaning the default value is not used) the variable’s default value is not changed, but rather its actual value.

Key points progress

summary

We have learned how to set the default value of the https_proxy.address variable with the value of the http_proxy.address variable. We did the same https_proxy.port and the https_proxy.port variables.

notation

We have learned a notation very usefull for the subfamilies traversal:

  • the _. notation means the current path of the family you’re currently in

  • the __. notation means the parent path of the current subfamily path.