Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions oap-formats/oap-template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ A compile-time template engine for the OAP framework. Each unique template strin
- [Delimiters](#delimiters)
- [Custom expression delimiters](#custom-expression-delimiters)
- [Whitespace trimming](#whitespace-trimming)
- [Literal value expressions](#literal-value-expressions)
- [Field access](#field-access)
- [Null safety](#null-safety)
- [Default values (`??`)](#default-values-)
Expand Down Expand Up @@ -163,6 +164,24 @@ line1

Renders as `line1content` when `flag` is `true` (the `\n` after `line1` is stripped).

### Literal value expressions

A template expression can contain a bare literal — no field access required:

| Template | Output |
|---|---|
| `{{ 1 }}` | `1` |
| `{{ 1.2 }}` | `1.2` |
| `{{ -1.2 }}` | `-1.2` |
| `{{ 'text' }}` | `text` |

Useful for static values in otherwise-dynamic templates, or as branch values in if-then-else:

```
price={{ 0.0 }}, label={{ 'unknown' }}
{{ if premium then user.discount else 0 end }}
```

### Field access

```
Expand Down Expand Up @@ -616,6 +635,14 @@ All range forms accept an optional `{{% else %}}` branch. It renders when the co
{{ obj ; toJson() }}
```

A default value (`??`) can follow the function call:

```
{{ byteField ; toString() ?? 'n/a' }}
```

This renders `'n/a'` when `byteField` is null.

### Cast types

`${ <java.lang.Double>field ?? 0.0 }` — forces the expression result to be interpreted as the given type. Useful when the field is typed as `Object` (e.g., in `Map<String, Object>`) but the actual runtime type is known.
Expand Down Expand Up @@ -651,6 +678,7 @@ Registered automatically from `META-INF/oap-template-macros.list` on the classpa
| `toLowerCase()` | `(String src)` | Converts to lower case; null-safe |
| `format(pattern)` | `(DateTime dt, String pattern)` | Formats a Joda `DateTime`; predefined patterns: `SIMPLE`, `MILLIS`, `SIMPLE_CLEAN`, `DATE` |
| `toJson()` | `(Object obj)` | Serialises the value to JSON |
| `toString()` | `(Byte\|Short\|Integer\|Long\|Float\|Double src)` | Converts a numeric wrapper to its string representation; returns `null` for `null` input (so `?? default` applies) |
| `default(fallback)` | `(Object in, Object fallback)` | Returns fallback if in is null or empty |

---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import oap.util.Lists;
}

expression returns [Expression ret]
: (BLOCK_COMMENT)? (CAST_TYPE)? (ifCode | pipeCode | withCode | exprsCode) defaultValue? function? (IF ifCondition)? {
: (BLOCK_COMMENT)? (CAST_TYPE)? (ifCode | pipeCode | withCode | exprsCode) function? defaultValue? (IF ifCondition)? {
$ret = new Expression(
$BLOCK_COMMENT.text,
$CAST_TYPE.text != null ? $CAST_TYPE.text.substring( 1, $CAST_TYPE.text.length() - 1 ) : null,
Expand Down Expand Up @@ -106,6 +106,24 @@ exprsCode returns [ArrayList<Exprs> ret = new ArrayList<>()]
| exprs {
$ret.add( $exprs.ret );
}
| SSTRING {
Exprs e = new Exprs(); e.concatenation = new Concatenation( List.of( sdStringToString( $SSTRING.text ) ) ); $ret.add( e );
}
| DSTRING {
Exprs e = new Exprs(); e.concatenation = new Concatenation( List.of( sdStringToString( $DSTRING.text ) ) ); $ret.add( e );
}
| DECDIGITS {
Exprs e = new Exprs(); e.concatenation = new Concatenation( List.of( new NumericLiteral( $DECDIGITS.text ) ) ); $ret.add( e );
}
| MINUS DECDIGITS {
Exprs e = new Exprs(); e.concatenation = new Concatenation( List.of( new NumericLiteral( "-" + $DECDIGITS.text ) ) ); $ret.add( e );
}
| FLOAT {
Exprs e = new Exprs(); e.concatenation = new Concatenation( List.of( new NumericLiteral( $FLOAT.text ) ) ); $ret.add( e );
}
| MINUS FLOAT {
Exprs e = new Exprs(); e.concatenation = new Concatenation( List.of( new NumericLiteral( "-" + $FLOAT.text ) ) ); $ret.add( e );
}
;

ifCondition returns [ConditionExpr ret]
Expand Down

Large diffs are not rendered by default.

Loading
Loading