Expression language

Description

OnSphere provides a simple expression language as a way to easily express conditions and transformations.

Usage

Syntax

Literals

  • true or false for booleans

  • 42, 31.4 or -1 for numbers

  • 'hello world' for text

  • variable-name for literal variables

Functions

  • function(argA) for unary functions

  • function(argA, argB) for binary functions

  • function(argA, argB, ..., argN) for n-ary functions

Functions and literals can be combined to express complex expressions

  • concat('abc', 'def', 'ghi')

  • cond(and(true, true), 'abcdef', '')

  • len(substr('test', 0, sub(len('test'), 1)))

Available functions

Name

Signature

Description

Examples

not

not(argA: boolean): boolean

logical NOT

  • not(true) ==> false

  • not(false) ==> true

and

and(argA: boolean, argB: boolean, ...argN: boolean): boolean

logical AND between 1 to N booleans

  • and(true, true) ==> true

  • and(true, true, false) ==> false

or

or(argA: boolean, argB: boolean, ...argN: boolean): boolean

logical OR between 1 to N booleans

  • or(false, false) ==> false

  • or(true, true, false) ==> true

add

add(argA: number, argB: number, ...argN: number): number

addition between 1 to N booleans

  • add(1, 2) ==> 3

  • add(1, -2, 3) ==> 2

sub

sub(argA: number, argB: number, ...argN: number): number

subtraction between 1 to N numbers

  • sub(1, 2) ==> -1

  • sub(3, 1) ==> 2

mul

mul(argA: number, argB: number, ...argN: number): number

multiplication between 1 to N numbers

  • mul(1, 2) ==> 2

  • mul(1, -2, 3) ==> -6

div

div(argA: number, argB: number): number

division between 2 numbers

  • div(9, 3) ==> 3

  • div(9, 2) ==> 4.5

mod

mod(argA: number, argB: number): number

modulus division between 2 numbers

  • mod(9, 3) ==> 0

  • mod(9, 2) ==> 1

starts

starts(str: text, prefix: text): boolean

textual starts with check

  • starts('abcdef', 'a') ==> true

  • starts('abcdef', 'ab') ==> true

  • starts('abcdef', 'f') ==> false

ends

ends(str: text, suffix: text): boolean

textual ends with check

  • ends('abcdef', 'a') ==> false

  • ends('abcdef', 'ab') ==> false

  • ends('abcdef', 'f') ==> true

in

in(str: text, content: text): boolean

textual contains check

  • in('abcdef', 'a') ==> true

  • in('abcdef', 'ab') ==> true

  • in('abcdef', 'cd') ==> true

  • in('abcdef', 'z') ==> false

substr

substr(str: text, begin: number, end: number): text

textual sub-string extraction based on indexes

  • substr('abcdef', 1, 5) ==> 'bcde'

  • substr('abcdef', 0, -1) ==> abcdef

  • substr('abcdef', 1, -2) ==> bcde

substrl

substrl(str: text, begin: number, end: number): text

textual sub-string extraction based on index and length

  • substrl('abcdef', 1, 5) ==> 'bcdef'

  • substrl('abcdef', -3, 2) ==> 'ef'

concat

concat(str: text, append: text): text

textual concatenation

  • concat('abc', 'def') ==> 'abcdef'

len

len(str: text): number

textual length

  • len('abcdef') ==> 6

  • len('abc') ==> 3

eq

eq(argA: any, argB: any): boolean :

equality check

  • eq('abcdef', 'abcdef') ==> true

  • eq(1, 1) ==> true

  • eq(1, 2) ==> false

neq

neq(argA: any, argB: any): boolean

inequality check

  • neq('abcdef', 'abcdef') ==> false, neq(1, 1) ==> false

  • neq(1, 2) ==> true

lt

lt(argA: number, argB: number): boolean

less than check

  • lt(1, 2) ==> true

  • lt(2, 2) ==> false

  • lt(3, 2) ==> false

lte

lte(argA: number, argB: number): boolean

less than or equal to check

  • lte(1, 2) ==> true

  • lte(2, 2) ==> true

  • lte(3, 2) ==> false

gt

gt(argA: number, argB: number): boolean

greater than check

  • gt(1, 2) ==> false

  • gt(2, 2) ==> false

  • gt(3, 2) ==> true

gte

gte(argA: number, argB: number): boolean

greater than or equal to check

  • gte(1, 2) ==> false

  • gte(2, 2) ==> true

  • gte(3, 2) ==> true

min

min(argA: number, argB: number, ...argN: number)

minimum of given numbers

  • min(0, 1) ==> 0

  • min(0, -1, -5) ==> -5

max

max(argA: number, argB: number, ...argN: number)

maximum of given numbers

  • max(0, 1) ==> 1

  • max(0, -1, 5)``==> ``5

var

var(variable: text): any

accesses the content of a variable

  • var('variable-boolean') ==> true with variable-boolean set to true

  • var('variable-text') ==> 'abcdef' with variable-text set to 'abcdef'

cond

cond(condition: boolean, then: any, else: any): any

conditional selection between two expressions

  • cond(true, 1, 2) ==> 1

  • cond(false, 1, 2) ==> 2

  • cond(false, 'a', 'b') ==> 'b'

debug

debug(debugged: any, label: text): any

reports the result of the execution as a labeled debug layer and transparently forwards the result to the caller

  • debug(true, 'dbg1') ==> true + reports dbg1 : true to the context

  • debug(cond(true, 1, 2), 'dbg-cond') ==> 1 + reports dbg-cond : 1 to the context

  • debug(cond(debug(and(true, true), 'dbg-and'), 'abcdef', ''), 'dbg-cond') ==> 'abcdef' + reports dbg-and : true and dbg-cond : abcdef to the context

asBoolean

asBoolean(converted: any)

converts the expression to a boolean.

  • asBoolean('true') ==> true. String are not case sensitive. Anything that is not true or false is false.

  • asBoolean(1) ==> true. The value is false if it is equal to 0.

  • asBoolean(1.0) ==> true. The value is false if it is equal to 0.0.

asInteger

asInteger(converted: any)

converts the expression to a integer.

  • asInteger('10') ==> 10

  • asInteger(true) ==> 1

  • asInteger(1.0) ==> 1

asDecimal

asDecimal(converted: any)

converts the expression to a decimal.

  • asDecimal('10.0') ==> 10.0

  • asDecimal(true) ==> 1.0

  • asDecimal(1) ==> 1.0

asText

asText(converted: any)

converts the expression to text.

  • concat('My message with a value = ', asText(value)) ==> My message with a value = 1 if the value was 1.

Accessing variables

Variables can either be accessed using var('variable-name') function or by using literal variable like variable-name. For example and(var('variable-name'), true) and and(variable-name, true) are equivalent.

Warning

Literal variables must start with a letter and only allow the characters a-z, A-Z, 0-9, ., _ and - when used without '.

However, accessing a dynamically named variable (i.e. a variable whose name is built from sub-expressions) can only be accessed using var() function. For example var(concat('constant-prefix.', variable-part)).