pub struct TransformFunctionOptions(/* private fields */);Expand description
The TransformFunctionOptions struct is a wrapper for serde_json::Value, any json data is accepted. Configuration options passed to transform functions when processing inventory entities.
TransformFunctionOptions is a wrapper around a JSON value that provides flexible,
schema-free configuration data for transform functions. It allows passing arbitrary
structured data to transforms without requiring predefined types or schemas.
The wrapper implements Deref and DerefMut to provide direct access to the underlying
serde_json::Value, enabling use of all JSON value methods for accessing and manipulating
the configuration data.
§Usage in Transforms
Transform functions receive an Option<&TransformFunctionOptions> parameter that can be
used to access configuration data. The options are typically set on the Inventory using
InventoryBuilder::transform_function_options().
§JSON Structure
The underlying JSON value can be any valid JSON structure:
- Object:
{"key": "value", "nested": {"data": 123}} - Array:
["item1", "item2"] - Primitive:
"string",42,true,null
§Examples
§Creating Options
// Simple key-value options
let options = TransformFunctionOptions::new(serde_json::json!({
"default_port": 2222,
"environment": "production"
}));
// Nested configuration
let options = TransformFunctionOptions::new(serde_json::json!({
"ssh": {
"port": 22,
"timeout": 30
},
"netconf": {
"port": 830,
"timeout": 60
}
}));§Accessing Options in Transforms
struct PortTransform;
impl Transform for PortTransform {
fn transform_host(&self, host: &Host, options: Option<&TransformFunctionOptions>) -> Host {
// Access options using JSON value methods
if let Some(opts) = options {
if let Some(port) = opts.get("default_port").and_then(|v| v.as_u64()) {
if host.port().is_none() {
return host.to_builder().port(port as u16).build();
}
}
}
host.clone()
}
}§Using with Inventory
let transform = TransformFunction::new(|host, options| {
if let Some(opts) = options {
if let Some(prefix) = opts.get("hostname_prefix").and_then(|v| v.as_str()) {
if let Some(hostname) = host.hostname() {
return host.to_builder()
.hostname(format!("{}{}", prefix, hostname))
.build();
}
}
}
host.clone()
});
let options = TransformFunctionOptions::new(serde_json::json!({
"hostname_prefix": "prod-"
}));
let mut hosts = Hosts::new();
hosts.add_host("router1", Host::builder().hostname("router1").build());
let inventory = Inventory::builder()
.hosts(hosts)
.transform_function(transform)
.transform_function_options(options)
.build();Implementations§
Source§impl TransformFunctionOptions
impl TransformFunctionOptions
Sourcepub fn new(options: Value) -> Self
pub fn new(options: Value) -> Self
Creates a new TransformFunctionOptions instance from a JSON value.
This constructor wraps any valid JSON value in a TransformFunctionOptions struct,
providing a flexible way to pass configuration data to transform functions. The JSON
value can be of any type: object, array, string, number, boolean, or null.
The options are typically accessed within transform function implementations using
the Deref trait to access the underlying serde_json::Value methods like get(),
as_str(), as_object(), etc.
§Parameters
options- Aserde_json::Valuecontaining the configuration data to be passed to transform functions. This can be any valid JSON structure created using theserde_json::json!macro or parsed from JSON text.
§Returns
Returns a new TransformFunctionOptions instance wrapping the provided JSON value.
§Examples
// Create options with an object
let options = TransformFunctionOptions::new(serde_json::json!({
"default_port": 2222,
"environment": "production"
}));
// Create options with an array
let options = TransformFunctionOptions::new(serde_json::json!(["item1", "item2"]));
// Create options with a primitive value
let options = TransformFunctionOptions::new(serde_json::json!("simple_string"));Trait Implementations§
Source§impl Clone for TransformFunctionOptions
impl Clone for TransformFunctionOptions
Source§fn clone(&self) -> TransformFunctionOptions
fn clone(&self) -> TransformFunctionOptions
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for TransformFunctionOptions
impl Debug for TransformFunctionOptions
Source§impl Deref for TransformFunctionOptions
impl Deref for TransformFunctionOptions
Source§type Target = <TransformFunctionOptions as DerefTarget>::Target
type Target = <TransformFunctionOptions as DerefTarget>::Target
Source§impl DerefMut for TransformFunctionOptions
impl DerefMut for TransformFunctionOptions
Source§impl<'de> Deserialize<'de> for TransformFunctionOptions
impl<'de> Deserialize<'de> for TransformFunctionOptions
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl JsonSchema for TransformFunctionOptions
impl JsonSchema for TransformFunctionOptions
Source§fn schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
Source§fn json_schema(generator: &mut SchemaGenerator) -> Schema
fn json_schema(generator: &mut SchemaGenerator) -> Schema
Source§fn inline_schema() -> bool
fn inline_schema() -> bool
$ref keyword. Read moreSource§impl PartialEq for TransformFunctionOptions
impl PartialEq for TransformFunctionOptions
Source§fn eq(&self, other: &TransformFunctionOptions) -> bool
fn eq(&self, other: &TransformFunctionOptions) -> bool
self and other values to be equal, and is used by ==.