Derive Macro falco_plugin::TableValues

source ·
#[derive(TableValues)]
{
    // Attributes available to this derive:
    #[static_only]
    #[dynamic]
    #[readonly]
    #[hidden]
}
Expand description

Mark a struct type as a table value

Tables in Falco plugins are effectively maps from a key to a (possibly dynamic) struct of values.

The default implementation for tables (tables::DynamicFieldValues) uses dynamic fields only, but with this macro you can also define structs containing static (predefined) fields that are accessible to your plugin without going through the Falco plugin API.

A table can be fully static (no dynamic fields allowed). In this case, it must be tagged with a #[static_only] attribute (to prevent accidental omission of the dynamic field values, which would only get caught at runtime, possibly much later).

Alternatively, it can mark a single field as #[dynamic]. That field needs to implement tables::TableValues and it will generally be of type tables::DynamicFieldValues.

Fields tagged as #[readonly] won’t be writable via the Falco API and fields tagged as #[hidden] won’t be exposed to the API at all. This is useful if you want to store data that’s incompatible with the Falco plugin API in your table.

§Example

use std::ffi::CString;
use falco_plugin::tables::DynamicFieldValues;
use falco_plugin::TableValues;

#[derive(TableValues, Default)]     // all table structs must implement Default
#[static_only]                      // no dynamic fields in this one
struct TableWithStaticFieldsOnly {
    #[readonly]
    int_field: u64,                 // this field cannot be modified with the Falco API
    string_field: CString,

    #[hidden]
    secret: Vec<u8>,                // this field is not visible via the Falco API
}

#[derive(TableValues, Default)]
struct AnotherTable {
    #[readonly]
    int_field: u64,                 // this field cannot be modified with the Falco API
    string_field: CString,

    #[hidden]
    secret: Vec<u8>,                // this field is not visible via the Falco API

    #[dynamic]
    dynamic_fields: DynamicFieldValues, // dynamically added fields have their values here
}