Data Conversion
The following set of functions are used to convert values from one type to another.
to_s
Syntax
to_s($value)
Description
Convert value to string. The semantic for each type is summarized in the following table:
| Type | Result | Example |
|---|---|---|
| string | no change | to_s("foo") == "foo" |
| number | string representation of number | to_s(1) == "1" |
| boolean | string representation of boolean | to_s(true) == "true" |
| datetime | string representation of datetime | to_s(d"1/1/2012 6:59 PM") == "d\"2012/01/01 18:59\"" |
| null | string “null” | to_s(null) == "null" |
| array | JSON representation of an array | to_s([1, 2, "3"]) == "[1,2,\"3\"]" |
| hash | JSON representation of a hash | to_s({ "one": 1, "two": 2, "three": 3 }) == "{\"one\":1,\"two\":2,\"three\":3}" |
to_n
Syntax
to_n($value)
Description
Convert a value to a number. The only types that can be converted to numbers are strings, booleans, and datetimes. Attempting to convert a value of a different type (e.g. an array) will result in an error.
| Type | Result | Example |
|---|---|---|
| string | corresponding number or 0 if string does not represent a number | to_n("1.23123") == 1.23123 to_n("foo") == 0 |
| number | no change | to_n(1) == 1 |
| boolean | 1 for true, 0 for false | to_n(true) == 1 |
| datetime | number of seconds since the epoch | to_n(d"2012/01/26 1:49:35") == 1327542575 |
| null | 0 | to_n(null) == 0 |
to_b
Syntax
to_b($value)
Description
Convert value to boolean. The only types that can be converted to booleans are strings and numbers. Attempting to convert a value of a different type (e.g. an array) will result in an error.
| Type | Result | Example |
|---|---|---|
| string | true if string is “true”, false otherwise | to_b("true") == true to_b("foo") == false |
| number | true if non 0, false otherwise | to_b(42) == true to_b(0) == false |
| boolean | no change | to_b(true) == true |
to_d
Syntax
to_d($value)
Description
Convert value to datetime. The only types that can be converted to datetimes are strings and numbers. Attempting to convert a value of a different type (e.g., an array) will result in an error. The accepted syntax for strings representing datetime is:
year/month/day [h:m[:s]] [AM|PM]
Trying to coerce a string that does not match this syntax to a datetime value results in an error.
| Type | Result | Example |
|---|---|---|
| string | corresponding datetime if syntax is correct, error otherwise | to_d("1/1/2012") == d"1/1/2012" |
| number | datetime with corresponding unix timestamp | to_d(42) == d"1/1/1970 00:00:42" |
| datetime | no change |
to_a
Syntax
to_a($value)
Description
Convert hash to array of pairs or a range to an array. Converting a value of type other than hash or range will result in an error.
| Type | Result | Example |
|---|---|---|
| hash | Corresponding array of pairs, ordering is random | to_a({ "one": 1, "two": 2, "three": 3 }) == [ ["two", 2], ["one", 1], ["three", 3] ] |
| range | Array of the given range | to_a([1..3]) == [1, 2, 3] |
to_object
Syntax
to_object(@declaration or @collection)
Description
Convert given resource declaration or resource collection into a JSON object. Especially useful to convert a declaration into an object, manipulate that object and assign it back to a declaration so that for example, provision() may be called on it.
| Type | Result | Example |
|---|---|---|
| Collection -OR- Declaration | JSON object containing declaration or collection fields Objects created from declarations may be assigned back to a reference. | $data = to_object(@servers) |
to_json
Syntax
to_json($value)