|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. |
| 2 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 3 | +// Copyright 2019-Present Datadog, Inc. |
| 4 | +use serde::de::{Error, MapAccess, Visitor}; |
| 5 | +use serde::{Deserialize, Deserializer, Serialize}; |
| 6 | +use serde_with::skip_serializing_none; |
| 7 | +use std::fmt::{self, Formatter}; |
| 8 | + |
| 9 | +/// A metric SLI specification, composed of three parts: the good events formula, the total events formula, |
| 10 | +/// and the involved queries. |
| 11 | +#[non_exhaustive] |
| 12 | +#[skip_serializing_none] |
| 13 | +#[derive(Clone, Debug, PartialEq, Serialize)] |
| 14 | +pub struct SLOCountCondition { |
| 15 | + /// A formula that specifies how to combine the results of multiple queries. |
| 16 | + #[serde(rename = "good_events_formula")] |
| 17 | + pub good_events_formula: crate::datadogV1::model::SLOFormula, |
| 18 | + #[serde(rename = "queries")] |
| 19 | + pub queries: Vec<crate::datadogV1::model::SLODataSourceQueryDefinition>, |
| 20 | + /// A formula that specifies how to combine the results of multiple queries. |
| 21 | + #[serde(rename = "total_events_formula")] |
| 22 | + pub total_events_formula: crate::datadogV1::model::SLOFormula, |
| 23 | + #[serde(flatten)] |
| 24 | + pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>, |
| 25 | + #[serde(skip)] |
| 26 | + #[serde(default)] |
| 27 | + pub(crate) _unparsed: bool, |
| 28 | +} |
| 29 | + |
| 30 | +impl SLOCountCondition { |
| 31 | + pub fn new( |
| 32 | + good_events_formula: crate::datadogV1::model::SLOFormula, |
| 33 | + queries: Vec<crate::datadogV1::model::SLODataSourceQueryDefinition>, |
| 34 | + total_events_formula: crate::datadogV1::model::SLOFormula, |
| 35 | + ) -> SLOCountCondition { |
| 36 | + SLOCountCondition { |
| 37 | + good_events_formula, |
| 38 | + queries, |
| 39 | + total_events_formula, |
| 40 | + additional_properties: std::collections::BTreeMap::new(), |
| 41 | + _unparsed: false, |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + pub fn additional_properties( |
| 46 | + mut self, |
| 47 | + value: std::collections::BTreeMap<String, serde_json::Value>, |
| 48 | + ) -> Self { |
| 49 | + self.additional_properties = value; |
| 50 | + self |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl<'de> Deserialize<'de> for SLOCountCondition { |
| 55 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 56 | + where |
| 57 | + D: Deserializer<'de>, |
| 58 | + { |
| 59 | + struct SLOCountConditionVisitor; |
| 60 | + impl<'a> Visitor<'a> for SLOCountConditionVisitor { |
| 61 | + type Value = SLOCountCondition; |
| 62 | + |
| 63 | + fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 64 | + f.write_str("a mapping") |
| 65 | + } |
| 66 | + |
| 67 | + fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error> |
| 68 | + where |
| 69 | + M: MapAccess<'a>, |
| 70 | + { |
| 71 | + let mut good_events_formula: Option<crate::datadogV1::model::SLOFormula> = None; |
| 72 | + let mut queries: Option< |
| 73 | + Vec<crate::datadogV1::model::SLODataSourceQueryDefinition>, |
| 74 | + > = None; |
| 75 | + let mut total_events_formula: Option<crate::datadogV1::model::SLOFormula> = None; |
| 76 | + let mut additional_properties: std::collections::BTreeMap< |
| 77 | + String, |
| 78 | + serde_json::Value, |
| 79 | + > = std::collections::BTreeMap::new(); |
| 80 | + let mut _unparsed = false; |
| 81 | + |
| 82 | + while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? { |
| 83 | + match k.as_str() { |
| 84 | + "good_events_formula" => { |
| 85 | + good_events_formula = |
| 86 | + Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 87 | + } |
| 88 | + "queries" => { |
| 89 | + queries = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 90 | + } |
| 91 | + "total_events_formula" => { |
| 92 | + total_events_formula = |
| 93 | + Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 94 | + } |
| 95 | + &_ => { |
| 96 | + if let Ok(value) = serde_json::from_value(v.clone()) { |
| 97 | + additional_properties.insert(k, value); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + let good_events_formula = good_events_formula |
| 103 | + .ok_or_else(|| M::Error::missing_field("good_events_formula"))?; |
| 104 | + let queries = queries.ok_or_else(|| M::Error::missing_field("queries"))?; |
| 105 | + let total_events_formula = total_events_formula |
| 106 | + .ok_or_else(|| M::Error::missing_field("total_events_formula"))?; |
| 107 | + |
| 108 | + let content = SLOCountCondition { |
| 109 | + good_events_formula, |
| 110 | + queries, |
| 111 | + total_events_formula, |
| 112 | + additional_properties, |
| 113 | + _unparsed, |
| 114 | + }; |
| 115 | + |
| 116 | + Ok(content) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + deserializer.deserialize_any(SLOCountConditionVisitor) |
| 121 | + } |
| 122 | +} |
0 commit comments