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 adatablock.
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
}
main.st prints the line:
ability "print" "Line" {
message = "${var.qty} x ${var.item} = ${var.price * 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
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
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.
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}"
}
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
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>
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
}
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.
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}"
}
Reading an output makes the reader run after that module, so Total needs no depends_on.
$ 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
structop graph -ascii draws the run order: Total waits for both modules.
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
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
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
}
1 x tea = 2
1 x juice = 2
1 x water = 2
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)