Skip to content
Open
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
114 changes: 114 additions & 0 deletions components/camel-datasonnet/src/main/docs/datasonnet-language.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,120 @@ xref:ROOT:properties-component.adoc[Properties] component (property placeholders
|cml.variable |the variable name |String |Will return the exchange variable.
|===

=== Null Handling Functions

[width="100%",cols="20%,30%,50%",options="header",]
|===
|Function |Example |Description

|cml.defaultVal(value, fallback) |`cml.defaultVal(body.currency, 'USD')` |Returns `value` if non-null, `fallback` otherwise.

|cml.isEmpty(value) |`cml.isEmpty(body.name)` |Returns `true` if the value is null, an empty string, or an empty array.
|===

=== Type Coercion Functions

[width="100%",cols="20%,30%,50%",options="header",]
|===
|Function |Example |Description

|cml.toInteger(value) |`cml.toInteger('42')` |Converts a string or number to an integer. Returns null for null input.

|cml.toDecimal(value) |`cml.toDecimal('3.14')` |Converts a string to a decimal number. Returns null for null input.

|cml.toBoolean(value) |`cml.toBoolean('true')` |Converts a string (`true`/`false`/`yes`/`no`/`1`/`0`) or number to boolean. Returns null for null input.
|===

=== Date/Time Functions

[width="100%",cols="20%,30%,50%",options="header",]
|===
|Function |Example |Description

|cml.now() |`cml.now()` |Returns the current timestamp as an ISO-8601 string.

|cml.nowFmt(format) |`cml.nowFmt('yyyy-MM-dd')` |Returns the current UTC time formatted with the given pattern.

|cml.formatDate(value, format) |`cml.formatDate('2026-03-20T10:30:00Z', 'dd/MM/yyyy')` |Reformats an ISO-8601 date string using the given pattern. Returns null for null input.

|cml.parseDate(value, format) |`cml.parseDate('20/03/2026', 'dd/MM/yyyy')` |Parses a date string into epoch milliseconds. Returns null for null input.
|===

=== Utility Functions

[width="100%",cols="20%,30%,50%",options="header",]
|===
|Function |Example |Description

|cml.uuid() |`cml.uuid()` |Generates a random UUID string.

|cml.typeOf(value) |`cml.typeOf(body.x)` |Returns the type name: `string`, `number`, `boolean`, `object`, `array`, `null`, or `function`.
|===

=== Camel Standard Library (`camel.libsonnet`)

Camel ships a standard library of helper functions that can be imported in any DataSonnet script:

[source,java]
----
local c = import 'camel.libsonnet';
{
total: c.sumBy(body.items, function(i) i.price * i.qty),
names: c.join(std.map(function(i) i.name, body.items), ', '),
topItem: c.first(c.sortBy(body.items, function(i) -i.price))
}
----

==== String Helpers

[width="100%",cols="30%,70%",options="header",]
|===
|Function |Description

|`c.capitalize(s)` |Capitalizes the first letter.
|`c.trim(s)` |Strips whitespace from both ends.
|`c.split(s, sep)` |Splits a string by separator.
|`c.join(arr, sep)` |Joins an array of strings with separator.
|`c.contains(s, sub)` |Checks if string contains substring.
|`c.startsWith(s, prefix)` |Checks if string starts with prefix.
|`c.endsWith(s, suffix)` |Checks if string ends with suffix.
|`c.replace(s, old, new)` |Replaces all occurrences.
|`c.lower(s)` / `c.upper(s)` |Case conversion.
|===

==== Collection Helpers

[width="100%",cols="30%,70%",options="header",]
|===
|Function |Description

|`c.sum(arr)` |Sums all elements.
|`c.sumBy(arr, f)` |Sums by applying function `f` to each element.
|`c.first(arr)` / `c.last(arr)` |First/last element, or null if empty.
|`c.count(arr)` |Array length.
|`c.distinct(arr)` |Removes duplicates.
|`c.flatMap(arr, f)` |Maps and flattens.
|`c.sortBy(arr, f)` |Sorts by key function.
|`c.groupBy(arr, f)` |Groups into object by key function.
|`c.min(arr)` / `c.max(arr)` |Minimum/maximum element.
|`c.take(arr, n)` / `c.drop(arr, n)` |First/last n elements.
|`c.zip(a, b)` |Zips two arrays into pairs.
|===

==== Object Helpers

[width="100%",cols="30%,70%",options="header",]
|===
|Function |Description

|`c.pick(obj, keys)` |Selects only the listed keys.
|`c.omit(obj, keys)` |Removes the listed keys.
|`c.merge(a, b)` |Merges two objects (b overrides a).
|`c.keys(obj)` / `c.values(obj)` |Object keys/values as arrays.
|`c.entries(obj)` |Converts to `[{key, value}]` array.
|`c.fromEntries(arr)` |Converts `[{key, value}]` array back to object.
|===

Here's an example showing some of these functions in use:

[tabs]
Expand Down
Loading
Loading