pub fn decode_json<T>(payload: &[u8]) -> Result<T, JsonError>where
T: DeserializeOwned,Expand description
Deserializes a JSON payload, for a decode implementation to call.
Payloads are opaque bytes and need not be JSON, so decoding is left to each product. This helper serves the
products whose payloads are. It takes no configuration ID: the client attributes a decode error to the
configuration being decoded.
§Errors
Returns JsonError if payload is not valid JSON or does not match the shape of T.
§Examples
use datadog_agent_remote_config::{decode_json, ConfigId, JsonError, ProductDecoder};
use serde::Deserialize;
#[derive(Deserialize)]
struct Limits {
max_spans: u32,
}
#[derive(Default)]
struct LimitsDecoder {
limits: Option<Limits>,
}
impl ProductDecoder for LimitsDecoder {
const PRODUCT: &'static str = "EXAMPLE_LIMITS";
type Snapshot = Option<Limits>;
type Error = JsonError;
fn decode(&mut self, _id: &ConfigId, payload: &[u8]) -> Result<(), Self::Error> {
self.limits = Some(decode_json(payload)?);
Ok(())
}
fn build(self) -> Result<Self::Snapshot, Self::Error> {
Ok(self.limits)
}
}
let mut decoder = LimitsDecoder::default();
decoder.decode(&ConfigId::new("limits.v1"), br#"{"max_spans": 100}"#).unwrap();
assert!(decoder.decode(&ConfigId::new("limits.v2"), b"{").is_err());
assert_eq!(decoder.build().unwrap().unwrap().max_spans, 100);