Policy Functions

These functions are available for all policies.


ipairs(tbl)

Same as Lua ipairs. From the Lua Reference Manual:

Returns three values: an iterator function, the table tbl, and 0, so that the construction

for i,v in ipairs(tbl) do body end

will iterate over the pairs (1,tbl[1]), (2,tbl[2]), ..., up to the first integer key absent from the table.


pairs(tbl)

Same as Lua pairs. From the Lua Reference Manual:

Returns three values: the next function, the table tbl, and nil, so that the construction

 for k,v in pairs(tbl) do body end

will iterate over all key–value pairs of table tbl.


sub(str, tbl)

_(str)

Simple string interpolation.

Given a table tbl, interpolate the string str by replacing corresponding field names with the respective value.

tbl = { "field" = "value" }
str = [[ this is the ${field} ]]

If passed with these arguments, a new string is returned as

'this is the value'

format(str, ...)

Same as Lua string.format. From the Lua Reference Manual:

Returns a formatted version of its variable number of arguments following the description given in its first argument (which must be a string). The format string follows the same rules as the ANSI C function sprintf. The only differences are that the options/modifiers *, h, L, l, n, and p are not supported and that there is an extra option, q. The q option formats a string between double quotes, using escape sequences when necessary to ensure that it can safely be read back by the Lua interpreter. For instance, the call

format('%q', 'a string with "quotes" and \n new line')

may produce the string:

"a string with \"quotes\" and \
  new line"

Options A and a (when available), E, e, f, G, and g all expect a number as argument. Options c, d, i, o, u, X, and x also expect a number, but the range of that number may be limited by the underlying C implementation. For options o, u, X, and x, the number cannot be negative. Option q expects a string; option s expects a string without embedded zeros. If the argument to option s is not a string, it is converted to one following the same rules of tostring.


each(tbl, func)

Run a function func against each item from table tbl.

The following:

t = {
  ["test.xxx"] = { comment = "test" },
  ["test.yyy"] = { comment = "test" }
}

each(t, file.absent)

is equivalent to

file.absent"test.xxx"{
  comment = "test"
}

file.absent"test.yyy"{
  comment = "test"
}