meld
Version:
Meld: A template language for LLM prompts
152 lines (114 loc) • 3.14 kB
Markdown
# @data Directive
The `@data` directive defines a structured data variable that can store objects, arrays, or other complex data types.
## Syntax
```meld
```
Where:
- `identifier` is the variable name (must be a valid identifier)
- `value` can be an object literal, array literal, string literal, or the result of an `@embed`, `@run`, or `@call` directive
## Identifier Requirements
- Must start with a letter or underscore
- Can contain letters, numbers, and underscores
- Case-sensitive
- Cannot be empty
## Supported Data Types
The @data directive supports all standard JSON data types:
- Objects (key-value pairs)
- Arrays
- Strings
- Numbers
- Booleans
- null
## Object and Array Literals
Data objects can be defined using object literal syntax:
```meld
```
For multi-line objects:
```meld
name: "Alice",
id: 123,
roles: ["admin", "editor"],
settings: {
theme: "dark",
notifications: true
}
}}
```
Arrays can be defined as well:
```meld
```
## Variable Interpolation in Data
Data structures can contain variable references in both keys and values:
```meld
{{keyName}}: {{name}}, # Dynamic key name
id: 123,
active: true
}}
```
## Referencing Data Variables
Data variables are referenced using the `{{identifier}}` syntax:
```meld
```
You can access nested fields using dot notation:
```meld
app: {
name: "MyApp",
version: "1.0.0"
}
}}
```
### Accessing Array Elements
Use dot notation to access array elements:
```meld
```
Note: Currently, only dot notation is supported for array access. Bracket notation (`fruits[0]`) is not supported.
## JSON String Format
Values can also be provided as JSON strings which are parsed:
```meld
```
## Examples
Basic data variable:
```meld
darkMode: true,
fontSize: 16
}}
```
Using variables in data:
```meld
username: {{name}},
active: true
}}
```
Using command output as data:
```meld
```
## Error Handling
The following errors are possible with data directives:
- JSON parsing errors (if value is invalid JSON)
- Variable resolution errors
- Execution errors when using @run or @embed as sources
## Notes
- Field access is only available for data variables
- Data variables can be formatted with the `>>` operator
- Simple data values are automatically converted to text when used in string contexts
- Entire objects/arrays are converted to JSON strings when used in text contexts
- Schema validation is planned but not yet implemented