Reference · reference
The Value type
Value is the one dynamically typed value in a config-weave script. The params argument of every check, apply and gather entry point is a Value, a gatherer returns a Value, the facts a verify script receives is a Value, and the json, toml, xml and data modules parse into and serialise from a Value. The type name is ambient: no use is needed to write Value in a signature. The accessor methods below come from the value module, so a script that calls them starts with use value.
The enum
Null,
Bool,
Int,
Float,
String,
List,
Map,
}
Value is an ordinary enum, so match, if let and let-else all apply to it, and a script constructs one with variant syntax. A map key is always a string.
use value
use sys
Map "family": String,
"cores": Int,
})
}
The params map handed to a resource or gatherer has the package's declared parameter defaults already applied and every declared type validated, so a script does not re-check a parameter's type. It does still need an accessor, because the static type of params.get("path") is Option[Value].
Navigation
get looks up a key in a Map and returns None for any other variant or a missing key. at indexes a List and returns None for any other variant, a negative index, or an index past the end. keys returns a Map's keys sorted, or an empty list for any other variant. len is the element count of a List or Map. is_null is true only for Value::Null.
| Method | Parameter | Returns |
|---|---|---|
| get | key: string | Option[Value] |
| at | idx: int | Option[Value] |
| keys | List[string] | |
| len | int | |
| is_null | bool |
Conversion
Each conversion returns Some when the variant matches and None otherwise. as_float also accepts an Int and widens it. No other conversion coerces: as_string on an Int is None, and as_int on a String is None, so parse the string yourself with parse_int from built-in methods.
| Method | Accepts | Returns |
|---|---|---|
| as_bool | Bool | Option[bool] |
| as_int | Int | Option[int] |
| as_float | Float or Int | Option[float] |
| as_string | String | Option[string] |
| as_list | List | Option[List[Value]] |
| as_map | Map | Option[Map[string, Value]] |
Reading a parameter
if let Some = params.get if let Some = v.as_string return s
}
}
fallback
}
This helper is the idiom the sample resources use: navigate with get, convert with an as_ method, and fall back when either step yields None. A match on the Value itself reads better when a script needs to branch on the variant, for example to accept a parameter given either as a single string or as a list of strings.