|
| 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 single column of a DDSQL tabular query result. |
| 10 | +#[non_exhaustive] |
| 11 | +#[skip_serializing_none] |
| 12 | +#[derive(Clone, Debug, PartialEq, Serialize)] |
| 13 | +pub struct DdsqlTabularQueryColumn { |
| 14 | + /// Name of the column as projected by the SQL statement. |
| 15 | + #[serde(rename = "name")] |
| 16 | + pub name: String, |
| 17 | + /// DDSQL data type of the column's values, for example `VARCHAR`, `BIGINT`, |
| 18 | + /// `DECIMAL`, `BOOLEAN`, `TIMESTAMP`, `JSON`, or an array variant such as |
| 19 | + /// `VARCHAR[]`. See the |
| 20 | + /// [DDSQL data-types reference](<https://docs.datadoghq.com/ddsql_reference/#data-types>) |
| 21 | + /// for the full, up-to-date list. |
| 22 | + #[serde(rename = "type")] |
| 23 | + pub type_: String, |
| 24 | + /// Column values in row order. The element type matches the column's `type`; |
| 25 | + /// for example a `VARCHAR` column carries strings, a `TIMESTAMP` column carries |
| 26 | + /// Unix-millisecond integers. `null` is allowed for missing values. |
| 27 | + #[serde(rename = "values")] |
| 28 | + pub values: Vec<serde_json::Value>, |
| 29 | + #[serde(flatten)] |
| 30 | + pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>, |
| 31 | + #[serde(skip)] |
| 32 | + #[serde(default)] |
| 33 | + pub(crate) _unparsed: bool, |
| 34 | +} |
| 35 | + |
| 36 | +impl DdsqlTabularQueryColumn { |
| 37 | + pub fn new( |
| 38 | + name: String, |
| 39 | + type_: String, |
| 40 | + values: Vec<serde_json::Value>, |
| 41 | + ) -> DdsqlTabularQueryColumn { |
| 42 | + DdsqlTabularQueryColumn { |
| 43 | + name, |
| 44 | + type_, |
| 45 | + values, |
| 46 | + additional_properties: std::collections::BTreeMap::new(), |
| 47 | + _unparsed: false, |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + pub fn additional_properties( |
| 52 | + mut self, |
| 53 | + value: std::collections::BTreeMap<String, serde_json::Value>, |
| 54 | + ) -> Self { |
| 55 | + self.additional_properties = value; |
| 56 | + self |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl<'de> Deserialize<'de> for DdsqlTabularQueryColumn { |
| 61 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 62 | + where |
| 63 | + D: Deserializer<'de>, |
| 64 | + { |
| 65 | + struct DdsqlTabularQueryColumnVisitor; |
| 66 | + impl<'a> Visitor<'a> for DdsqlTabularQueryColumnVisitor { |
| 67 | + type Value = DdsqlTabularQueryColumn; |
| 68 | + |
| 69 | + fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 70 | + f.write_str("a mapping") |
| 71 | + } |
| 72 | + |
| 73 | + fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error> |
| 74 | + where |
| 75 | + M: MapAccess<'a>, |
| 76 | + { |
| 77 | + let mut name: Option<String> = None; |
| 78 | + let mut type_: Option<String> = None; |
| 79 | + let mut values: Option<Vec<serde_json::Value>> = None; |
| 80 | + let mut additional_properties: std::collections::BTreeMap< |
| 81 | + String, |
| 82 | + serde_json::Value, |
| 83 | + > = std::collections::BTreeMap::new(); |
| 84 | + let mut _unparsed = false; |
| 85 | + |
| 86 | + while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? { |
| 87 | + match k.as_str() { |
| 88 | + "name" => { |
| 89 | + name = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 90 | + } |
| 91 | + "type" => { |
| 92 | + type_ = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 93 | + } |
| 94 | + "values" => { |
| 95 | + values = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 96 | + } |
| 97 | + &_ => { |
| 98 | + if let Ok(value) = serde_json::from_value(v.clone()) { |
| 99 | + additional_properties.insert(k, value); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + let name = name.ok_or_else(|| M::Error::missing_field("name"))?; |
| 105 | + let type_ = type_.ok_or_else(|| M::Error::missing_field("type_"))?; |
| 106 | + let values = values.ok_or_else(|| M::Error::missing_field("values"))?; |
| 107 | + |
| 108 | + let content = DdsqlTabularQueryColumn { |
| 109 | + name, |
| 110 | + type_, |
| 111 | + values, |
| 112 | + additional_properties, |
| 113 | + _unparsed, |
| 114 | + }; |
| 115 | + |
| 116 | + Ok(content) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + deserializer.deserialize_any(DdsqlTabularQueryColumnVisitor) |
| 121 | + } |
| 122 | +} |
0 commit comments