Language: Values
Values are the data that Sentinel policies operate on. You can create this
data yourself, for example by just typing the number 42
, or you may access
this data from an external source to make policy decisions.
Values have a type, which determines what kind of operations can be performed on the data. Example types are booleans (true and false), numbers, and strings (text).
This page documents all the available value types. You can also convert between types.
Boolean
A boolean is a value that is either true or false.
A boolean value is created literally with true
or false
.
As a policy language, booleans are central to the behavior of Sentinel. Booleans are used as conditions in if statements, are the result of rules, and more. The ultimate result of a Sentinel policy is true or false.
Integer
An integer is a whole number.
An integer is a 64-bit value. This means it can represent numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,808.
Integers are created by typing them out literally with no
separators (such as a comma). An optional prefix can set a non-decimal base:
0
for octal, and 0x
for hexadecimal. In hexadecimal literals, letters
a-f
and A-F
represent values 10 to 15.
Example integers are shown below:
4206000xBadFace170141183460469
Integers are used for math and numerical comparison.
Float
A float is a number with a decimal point. It has an integer part, a decimal point, a fractional part, and optionally an exponent part. Example floats are shown below:
0.72.40072.40 // == 72.402.718281.e+06.67428e-111E6.25.12345E+5
Floats are IEEE-754 64-bit floating point numbers.
String
Strings are text values.
Strings are created by wrapping text in double quotes, such as "hello"
.
Within the quotes, any character may appear except newline and an unescaped
double quote. The text between the quotes is the value.
Because Sentinel policies must be UTF-8 encoded text, strings themselves are
UTF-8 encoded text. This means you can put any value UTF-8 character into
a string, such as "日本語"
.
You can have a string with a literal double-quote by escaping it with
a backslash. For example: "they said \"hello\""
turns into the value
they said "hello"
.
Backslash escapes can be used for much more than only escaping a double quote.
Newlines are \n
, a literal backslash is \\
, and many more. The full list
of available backslash escapes can be found
in the language specification.
Example strings:
`abc` // same as "abc"`\n\n` // same as "\\n\n\\n""\n""\"" // same as `"`"Hello, world!\n""日本語""\u65e5本\U00008a9e""\xff\u00FF""\uD800" // illegal: surrogate half"\U00110000" // illegal: invalid Unicode code point
Strings do not support indexing, however it is possible to achieve a similar result through the combination of the strings standard import and list indexing.
Sentinel Playground
Loading the playground...
Press "Run" to get policy output
Strings support slicing to efficiently create substrings.
List
See Lists.
Map
See Maps.
Rule
See Rules.
Function
See Functions.
Type Conversion
The built-in functions int
, float
, string
, and bool
convert a value to a
value of that type. Some examples are shown below followed by a list of exact
rules for type conversion.
int(42) // 42int("42") // 42int(42.8) // 42int(true) // 1 float(1.2) // 1.2float(1) // 1.0float("4.2") // 4.2float(true) // 1.0 string("foo") // "foo"string(88) // "88"string(0xF) // "15"string(true) // "true" bool("true") // truebool(1) // truebool(-1) // truebool(0.1) // truebool("false") // falsebool(0) // false
For int
:
- Integer values are unchanged
- String values are converted according to the syntax of integer literals
- Float values are rounded down to their nearest integer value
- Boolean values are converted to
1
fortrue
, and0
forfalse
For float
:
- Float values are unchanged
- Integer values are converted to the nearest equivalent floating point value
- String values are converted according to the syntax of float literals
- Boolean values are converted to
1.0
fortrue
, and0.0
forfalse
For string
:
- String values are unchanged
- Integer values are converted to the base 10 string representation
- Float values are converted to a string formatted
xxx.xxx
with a precision of 6. This is equivalent to%f
for C's sprintf. - Boolean values are converted to
"true"
fortrue
, and"false"
forfalse
For bool
:
- The following string values convert to
true
:"1"
,"t"
,"T"
,"TRUE"
,"true"
, and"True"
- The following string values convert to
false
:"0"
,"f"
,"F"
,"FALSE"
,"false"
, and"False"
- Any non-zero integer or float value converts to
true
- Any zero integer or float value converts to
false
For any other unspecified type, the result is the undefined
value.