Struct grafana_plugin_sdk::data::Field
source · pub struct Field {
pub name: String,
pub labels: BTreeMap<String, String>,
pub config: Option<FieldConfig>,
/* private fields */
}
Expand description
A typed column within a Frame
.
The underlying data for this field can be read using the Field::values
method,
and updated using the Field::set_values
and Field::set_values_opt
methods.
Fields§
§name: String
The name of this field.
Fields within a Frame
are not required to have unique names, but
the combination of name
and labels
should be unique within a frame
to ensure proper behaviour in all situations.
labels: BTreeMap<String, String>
An optional set of key-value pairs that, combined with the name, should uniquely identify a field within a Frame
.
config: Option<FieldConfig>
Optional display configuration used by Grafana.
Implementations§
source§impl Field
impl Field
sourcepub fn with_name(self, name: impl Into<String>) -> Self
pub fn with_name(self, name: impl Into<String>) -> Self
Return a new field with the given name.
Example
use grafana_plugin_sdk::prelude::*;
let field = ["a", "b", "c"]
.into_field("x")
.with_name("other name");
assert_eq!(&field.name, "other name");
sourcepub fn with_labels(self, labels: BTreeMap<String, String>) -> Self
pub fn with_labels(self, labels: BTreeMap<String, String>) -> Self
Return a new field with the given labels.
Example
use std::collections::BTreeMap;
use grafana_plugin_sdk::prelude::*;
let mut labels = BTreeMap::default();
labels.insert("some".to_string(), "value".to_string());
let field = ["a", "b", "c"]
.into_field("x")
.with_labels(labels);
assert_eq!(field.labels["some"], "value");
sourcepub fn with_config(self, config: impl Into<Option<FieldConfig>>) -> Self
pub fn with_config(self, config: impl Into<Option<FieldConfig>>) -> Self
Return a new field with the given config.
Example
use grafana_plugin_sdk::{data::FieldConfig, prelude::*};
let mut config = FieldConfig::default();
config.display_name_from_ds = Some("X".to_string());
let field = ["a", "b", "c"]
.into_field("x")
.with_config(config);
assert_eq!(&field.config.unwrap().display_name_from_ds.unwrap(), "X");
sourcepub fn set_values<T, U, V>(&mut self, values: T) -> Result<(), Error>where
T: IntoIterator<Item = U>,
U: IntoFieldType<ElementType = V>,
V: FieldType,
V::Array: Array + FromIterator<Option<V>> + 'static,
pub fn set_values<T, U, V>(&mut self, values: T) -> Result<(), Error>where
T: IntoIterator<Item = U>,
U: IntoFieldType<ElementType = V>,
V: FieldType,
V::Array: Array + FromIterator<Option<V>> + 'static,
Set the values of this field using an iterator of values.
Errors
Returns an Error::DataTypeMismatch
if the types of the new data
do not match the types of the existing data.
use arrow2::array::Utf8Array;
use grafana_plugin_sdk::prelude::*;
let mut field = ["a", "b", "c"]
.into_field("x");
assert!(field.set_values(["d", "e", "f", "g"]).is_ok());
assert_eq!(
field
.values()
.as_any()
.downcast_ref::<Utf8Array<i32>>()
.unwrap()
.iter()
.collect::<Vec<_>>(),
vec![Some("d"), Some("e"), Some("f"), Some("g")],
);
assert!(field.set_values([1u32, 2, 3]).is_err());
sourcepub fn set_values_opt<T, U, V>(&mut self, values: T) -> Result<(), Error>where
T: IntoIterator<Item = Option<U>>,
U: IntoFieldType<ElementType = V>,
V: FieldType,
V::Array: Array + FromIterator<Option<V>> + 'static,
pub fn set_values_opt<T, U, V>(&mut self, values: T) -> Result<(), Error>where
T: IntoIterator<Item = Option<U>>,
U: IntoFieldType<ElementType = V>,
V: FieldType,
V::Array: Array + FromIterator<Option<V>> + 'static,
Set the values of this field using an iterator of optional values.
Errors
Returns an Error::DataTypeMismatch
if the types of the new data
do not match the types of the existing data.
use arrow2::array::Utf8Array;
use grafana_plugin_sdk::prelude::*;
let mut field = ["a", "b", "c"]
.into_field("x");
assert!(field.set_values_opt([Some("d"), Some("e"), None, None]).is_ok());
assert_eq!(
field
.values()
.as_any()
.downcast_ref::<Utf8Array<i32>>()
.unwrap()
.iter()
.collect::<Vec<_>>(),
vec![Some("d"), Some("e"), None, None],
);
assert!(field.set_values([Some(1u32), Some(2), None]).is_err());
sourcepub fn set_values_array<T>(&mut self, values: T) -> Result<(), Error>where
T: Array + 'static,
pub fn set_values_array<T>(&mut self, values: T) -> Result<(), Error>where
T: Array + 'static,
Set the values of this field using an Array
.
Errors
Returns an Error::DataTypeMismatch
if the types of the new data
do not match the types of the existing data.
use arrow2::array::{PrimitiveArray, Utf8Array};
use grafana_plugin_sdk::prelude::*;
let mut field = ["a", "b", "c"]
.into_field("x");
let new_values = Utf8Array::<i32>::from(["d", "e", "f"].map(Some));
assert!(field.set_values_array(new_values).is_ok());
assert_eq!(
field
.values()
.as_any()
.downcast_ref::<Utf8Array<i32>>()
.unwrap()
.iter()
.collect::<Vec<_>>(),
vec![Some("d"), Some("e"), Some("f")],
);
let bad_values = PrimitiveArray::<u32>::from([1, 2, 3].map(Some));
assert!(field.set_values_array(bad_values).is_err());
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Field
impl Send for Field
impl Sync for Field
impl Unpin for Field
impl !UnwindSafe for Field
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request