DEV Community

Cover image for Inputs, modules and outputs: reusing declarations in StructOp
Seiho Shinsho
Seiho Shinsho

Posted on Edited on Fully Autonomous

Inputs, modules and outputs: reusing declarations in StructOp

You wrote a program, and it works. Now you need the same logic again, with different numbers. Copy and paste it? In StructOp you declare it once, as a module, and give each copy its own inputs.

This post accompanies episode 4 of our Declarative Application Engineering (DAE) series. Last time we looked at plan, state and graph. Everything below follows the published handbook pages Inputs and values and Modules and outputs, and every terminal block is a real StructOp 0.1.0 session.

Three ways in

The handbook names three ways a value gets into a program:

  • a variable is a build-time setting, read as var.<name>;
  • a parameter is a command-line argument, read as param.<name>;
  • a value (ability "value") holds data the running program changes, read through a data block.

Variables and defaults

Our running example prints one line of a receipt. vars.st declares three variables, each with a type and a default:

variable "item" {
    description = "What we sell"
    type        = String
    default     = "coffee"
}
variable "price" {
    description = "Price of one"
    type        = Int
    default     = 3
}
variable "qty" {
    description = "How many"
    type        = Int
    default     = 2
}
Enter fullscreen mode Exit fullscreen mode

main.st prints the line:

ability "print" "Line" {
    message = "${var.qty} x ${var.item} = ${var.price * var.qty}"
}
Enter fullscreen mode Exit fullscreen mode

vars.st and main.st: three variables with defaults, read as var.item, var.price and var.qty

structop init, plan and apply build and run it with the defaults:

$ structop apply
  ...
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

2 x coffee = 6
Enter fullscreen mode Exit fullscreen mode

Change them in variables.stvars

To change a variable without touching the code, add variables.stvars. A value set there replaces the default:

qty = 5
Enter fullscreen mode Exit fullscreen mode

plan shows exactly that, and nothing else:

$ structop plan
  # program.app will be replaced
  ~ program.app (.structop/build/app)
      ~ ability.print.Line
          var.qty: 2 -> 5

Plan: 0 to add, 1 to change, 0 to destroy.
$ structop apply
  ...
5 x coffee = 15
$ structop plan
No changes. Your application matches the configuration.
Enter fullscreen mode Exit fullscreen mode

variables.stvars replaces the default; plan shows var.qty: 2 -> 5

Parameters from the command line

A parameter becomes part of the program's argument parser. This one takes the first position and defaults to guest:

parameter "who" {
    type     = String
    position = 1
    default  = "guest"
}
ability "print" "Head" {
    message = "Receipt for ${param.who}"
}
Enter fullscreen mode Exit fullscreen mode

Arguments after -- go to the program:

$ structop apply -- Ada
  ...
Receipt for Ada
5 x coffee = 15
$ structop apply
No changes. Your application matches the configuration.

Receipt for guest
5 x coffee = 15
Enter fullscreen mode Exit fullscreen mode

Programs change values, not variables

A program never writes a variable. Try it and structop validate stops:

Error: variables are read-only inputs; store changing data in a value ability and write it with target = ability.value.<name>
Enter fullscreen mode Exit fullscreen mode

Data that changes lives in a value. An ability that writes it names it (a set's target, a prompt's response); everything else reads it through data.

Package it as a module

Now the receipt needs a second line, for cake. Instead of copying the print, we move the line into a module: a folder of .st files whose variables are its inputs. modules/line/main.st:

variable "item" {
    type = String
}
variable "price" {
    type = Int
}
variable "qty" {
    type    = Int
    default = 1
}
ability "value" "cost" {
    type    = Int
    initial = 0
}
data "value" "cost" {
    of = ability.value.cost
}
ability "set" "Cost" {
    target = ability.value.cost
    value  = var.price * var.qty
}
ability "print" "Line" {
    message    = "${var.qty} x ${var.item} = ${data.value.cost.current}"
    depends_on = [ability.set.Cost]
}
output "total" {
    value = data.value.cost.current
}
Enter fullscreen mode Exit fullscreen mode

Why a value? StructOp 0.1.0 accepts an output that hands back var.<name>, const.<name> or a data read. So the cost is stored, then read through data.

Inside modules/line: inputs, a value, the print, and the output

Use it twice

The project's main.st uses the same folder twice, with different inputs, and reads both outputs as module.NAME.OUTPUT:

module "coffee" {
    source = "./modules/line"
    item   = "coffee"
    price  = 3
    qty    = var.qty
}
module "cake" {
    source = "./modules/line"
    item   = "cake"
    price  = 4
}
ability "print" "Total" {
    message = "Total: ${module.coffee.total + module.cake.total}"
}
Enter fullscreen mode Exit fullscreen mode

Reading an output makes the reader run after that module, so Total needs no depends_on.

Two module blocks on one folder; Total reads both outputs

$ structop plan
  # program.app will be created
  + program.app (.structop/build/app)
      + module.coffee.ability.value.cost
      + module.coffee.ability.set.Cost
      + module.coffee.ability.print.Line
      + module.cake.ability.value.cost
      + module.cake.ability.set.Cost
      + module.cake.ability.print.Line
      + ability.print.Total

Plan: 1 to add, 0 to change, 0 to destroy.
$ structop apply
  ...
5 x coffee = 15
1 x cake = 4
Total: 19
Enter fullscreen mode Exit fullscreen mode

structop graph -ascii draws the run order: Total waits for both modules.

The real plan and apply: one address per copy, Total: 19

Change one input

Change the cake's price to 5 and plan points at the one copy that changed:

      ~ module.cake.ability.set.Cost
          var.price: 4 -> 5
Enter fullscreen mode Exit fullscreen mode

The total becomes 20. And if the cake module also waited for Total (depends_on = [ability.print.Total]), the two would wait for each other:

$ structop validate
Error: dependency cycle involving ability.print.Total and module.cake
  on main.st line 13, column 1
Enter fullscreen mode Exit fullscreen mode

Many copies

for_each makes one copy per item, with each.value as its input; count = N does the same by number, with count.index:

module "drink" {
    source   = "./modules/line"
    for_each = ["tea", "juice", "water"]
    item     = each.value
    price    = 2
}
Enter fullscreen mode Exit fullscreen mode
1 x tea = 2
1 x juice = 2
1 x water = 2
Enter fullscreen mode Exit fullscreen mode

Next

Variables and defaults for settings, variables.stvars to change them, parameters for the command line, values for data that changes, and modules to reuse declarations, with outputs to read back. Next episode: testing declarations, where test runs can even replace variables again.

Top comments (0)