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

source

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");
source

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");
source

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");
source

pub fn values(&self) -> &dyn Array

Get the values of this field as a [&dyn Array].

source

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());
source

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());
source

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§

source§

impl Debug for Field

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq for Field

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoRequest<T> for T

source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more