Utils
Here you can find various helpers available in the root core namespace.
log
Prints a string message to the /usr/local/coronium/logs/coronium.log file.
core.log( message )
Parameters
Name | Description | Requried |
---|---|---|
message | The message string to log. | Y |
Example
core.log('something to log')
sf (string format)
Format a string based on token replacment. This method shares the same attributes as the Lua string.format method.
core.sf( str_template, values )
Parameters
Name | Description | Requried |
---|---|---|
str_template | The string containing replacment tokens. | Y |
values | A list of values for token replacment. | Y |
Example
local username = "Dave" local formatted_str = core.sf("Hello, %s!", username) -- formatted_str = "Hello, Dave!"
Notes
Some common token types:
Token | Description |
---|---|
%s | Used as a string replacment, any other type will error. |
%d | Used as a number replacment, any other type will error. |
You can mix tokens, as well as have multiples:
local str = core.sf("Ordered %d %s with %s.", 2, 'burgers', 'cheese') -- `str` contains "Ordered 2 burgers with cheese."
Tip
The sf method is useful for creating SQL queries, as well as other type-safe replacments.
trim
Removes empty space from the start and end of a string, if any.
core.trim( string )
Parameters
Name | Description | Requried |
---|---|---|
string | The string to trim. | Y |
Example
local trimmed_str = core.trim(" I could use a trim ") -- trimmed_str = "I could use a trim"
split
Converts a delimited string into a Lua table array.
core.split( string[, delimiter ] )
Parameters
Name | Description | Default | Requried |
---|---|---|---|
string | The delimited string to split. | None | Y |
delimiter | The delimiter to split the string with. | Comma ( , ) | N |
Example
local tbl_array = core.split("Red,Green,Blue") -- `tbl_array` contains {"Red","Green","Blue"}
Using a custom delimiter:
local tbl_array = core.split("User:2001:Storage", ":") -- `tbl_array` contains {"User","2001","Storage"}
json
JSON encoding and decoding.
local json = core.json
Encode
local str = json.encode(tbl)
Decode
local tbl = json.decode(str)
json_safe
JSON encoding and decoding with error handling.
local json = core.json_safe
Encode safe
local str, err = json.encode(tbl)
Decode safe
local tbl, err = json.decode(str)
uuid
Generates a universally unique id.
Example
local uuid = core.uuid() -- `uuid` will contain something like "1f0af2fa-8b06-4605-bace-e13a85aa36d5"
md5
Generate a hexadecimal MD5 digest from a given string.
local md5 = core.md5("somestringtomd5") -- `md5` will contain something like "9a5c03309269f0274506259626dc725c"
sleep
Pause processing for a certain amount of seconds. Useful in looping situations.
core.sleep(seconds)
Parameters
Name | Description | Default | Requried |
---|---|---|---|
seconds | The amount of seconds to sleep. | Number | Y |
Example
for i=1, 20 do --do some work core.sleep(2) --wait 2 seconds end
countryCode
Two letter country code based on the request IP. Can only be called in a core.api method.
Example
local code = core.countryCode() -- `code` will contain something like "US" or "EU"
callApi
Call a method on a custom api endpoint built using the core.api. This method allows you to call other project api methods from within a different project api.
core.callApi(project, action, data_params)
Parameters
Name | Description | Type | Required |
---|---|---|---|
project | The name of the api project. | String | Y |
action | The name of the api method to call. | String | Y |
data_params | Values to pass to the api method. | Table | Y |
Example
local result, err = core.callApi("default", "test", {greet="Hello!"})