Open source

Operators

You learned how to reference component exports to connect your pipelines in the previous topic. Now you’ll learn how to use operators to manipulate, transform, and combine values in your expressions.

Operators are essential for creating dynamic configurations that can:

  1. Calculate values: Perform arithmetic operations on component exports and literals.
  2. Compare data: Make decisions based on component states or configuration values.
  3. Combine strings: Build dynamic identifiers, paths, or configuration strings.
  4. Access nested data: Extract values from complex objects and arrays.

The component controller evaluates all operators following the standard PEMDAS order of mathematical operations.

Arithmetic operators

OperatorDescription
+Adds two numbers.
-Subtracts two numbers.
*Multiplies two numbers.
/Divides two numbers.
%Computes the remainder after dividing two numbers.
^Raises a number to the specified power.

String operators

OperatorDescription
+Concatenate two strings or two secrets, or a string and a secret.

String concatenation is particularly useful for building dynamic values from component exports:

Alloy
prometheus.scrape "app" {
  targets = [{ "__address__" = local.file.host.content + ":" + local.file.port.content }]
  job_name = "app-" + sys.env("ENVIRONMENT")
}

Comparison operators

OperatorDescription
==Returns true when two values are equal.
!=Returns true when two values aren’t equal.
<Returns true when the left value is less than the right value.
<=Returns true when the left value is less than or equal to the right value.
>Returns true when the left value is greater than the right value.
>=Returns true when the left value is greater than or equal to the right value.

You can use the equality operators == and != with any operands.

The operands in ordering operators <, <=, >, and >= must be orderable and of the same type. The results of these comparisons are:

  1. Boolean values are equal if both are either true or false.
  2. Numerical (integer and floating-point) values follow the usual ordering.
  3. String values follow lexical ordering, byte-wise.
  4. Objects are equal if all their fields are equal.
  5. Arrays are equal if their corresponding elements are equal.

Comparison operators are commonly used for conditional configuration:

Alloy
// Check if environment matches
remote_write_enabled = sys.env("ENVIRONMENT") == "production"

Logical operators

OperatorDescription
&&Returns true when both the left and right values are true.
||Returns true when either the left or right value is true.
!Negates a boolean value.

Logical operators work with boolean values and return a boolean result.

Assignment operator

The Alloy configuration syntax uses = as the assignment operator.

An assignment statement can only assign a single value. Each value must be assignable to the attribute or object key:

  1. You can assign null to any attribute.
  2. You can assign numerical, string, boolean, array, function, capsule, and object types to attributes of the corresponding type.
  3. You can assign numbers to string attributes with an implicit conversion.
  4. You can assign strings to numerical attributes if they represent a number.
  5. You can’t assign blocks.

The component controller performs type checking during evaluation to ensure assignments are valid before configuring components.

Brackets

BracketsDescription
{ }Define blocks and objects.
( )Group and prioritize expressions.
[ ]Define arrays.

For example, the following code uses curly braces and square brackets to define an object and an array.

Alloy
obj = { app = "alloy", namespace = "dev" }
arr = [1, true, 7 * (1+1), 3]

Access operators

OperatorDescription
[ ]Access a member of an array or object.
.Access a named member of an object or an exported field of a component.

You can use the Alloy access operators to retrieve nested values. Use square brackets to access zero-indexed array elements or object fields by enclosing the field name in double quotes. Use the dot operator to access object fields without double quotes or component exports.

Alloy
// Access array elements and object fields
obj["app"]
arr[1]

// Access object fields and component exports
obj.app
local.file.token.content
discovery.kubernetes.endpoints.targets[0]["__address__"]

// Build complex expressions with nested access
prometheus.scrape "dynamic" {
  targets = discovery.kubernetes.endpoints.targets
  job_name = discovery.kubernetes.endpoints.targets[0]["job"] + "-scraper"
}

If you use the [ ] operator to access a non-existent object member, the result is null.

If you use the . operator to access a non-existent named member of an object, an error occurs.

Next steps

Now that you understand operators, continue learning about expressions:

  • Types and values - Learn how the type system works with operators to ensure expressions evaluate correctly
  • Function calls - Use operators inside function calls and with function return values

For building complete configurations:

  • Component exports - Combine operators with component references to create dynamic pipelines