|
| 1 | +use crate::cube_bridge::base_query_options::{FilterItem, OrderByItem}; |
| 2 | +use serde::de; |
| 3 | +use serde::{Deserialize, Deserializer}; |
| 4 | + |
| 5 | +#[derive(Debug, Deserialize)] |
| 6 | +pub struct YamlBaseQueryOptions { |
| 7 | + #[serde(default)] |
| 8 | + pub measures: Option<Vec<String>>, |
| 9 | + #[serde(default)] |
| 10 | + pub dimensions: Option<Vec<String>>, |
| 11 | + #[serde(default)] |
| 12 | + pub segments: Option<Vec<String>>, |
| 13 | + #[serde(default)] |
| 14 | + pub order: Option<Vec<YamlOrderByItem>>, |
| 15 | + #[serde(default)] |
| 16 | + pub filters: Option<Vec<YamlFilterItem>>, |
| 17 | + #[serde(default)] |
| 18 | + pub limit: Option<String>, |
| 19 | + #[serde(default)] |
| 20 | + pub row_limit: Option<String>, |
| 21 | + #[serde(default)] |
| 22 | + pub offset: Option<String>, |
| 23 | + #[serde(default)] |
| 24 | + pub ungrouped: Option<bool>, |
| 25 | + #[serde(default)] |
| 26 | + pub export_annotated_sql: Option<bool>, |
| 27 | + #[serde(default)] |
| 28 | + pub pre_aggregation_query: Option<bool>, |
| 29 | + #[serde(default)] |
| 30 | + pub total_query: Option<bool>, |
| 31 | + #[serde(default)] |
| 32 | + pub cubestore_support_multistage: Option<bool>, |
| 33 | + #[serde(default)] |
| 34 | + pub disable_external_pre_aggregations: Option<bool>, |
| 35 | +} |
| 36 | + |
| 37 | +#[derive(Debug, Deserialize)] |
| 38 | +pub struct YamlOrderByItem { |
| 39 | + pub id: String, |
| 40 | + #[serde(default)] |
| 41 | + pub desc: Option<bool>, |
| 42 | +} |
| 43 | + |
| 44 | +impl YamlOrderByItem { |
| 45 | + pub fn into_order_by_item(self) -> OrderByItem { |
| 46 | + OrderByItem { |
| 47 | + id: self.id, |
| 48 | + desc: self.desc, |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#[derive(Debug)] |
| 54 | +pub enum YamlFilterItem { |
| 55 | + Group(YamlFilterGroup), |
| 56 | + Base(YamlBaseFilter), |
| 57 | +} |
| 58 | + |
| 59 | +impl<'de> Deserialize<'de> for YamlFilterItem { |
| 60 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 61 | + where |
| 62 | + D: Deserializer<'de>, |
| 63 | + { |
| 64 | + let value = serde_yaml::Value::deserialize(deserializer)?; |
| 65 | + |
| 66 | + // Check if it has 'or' or 'and' keys - then it's a Group |
| 67 | + if let serde_yaml::Value::Mapping(ref map) = value { |
| 68 | + let has_or = map.contains_key(&serde_yaml::Value::String("or".to_string())); |
| 69 | + let has_and = map.contains_key(&serde_yaml::Value::String("and".to_string())); |
| 70 | + |
| 71 | + if has_or || has_and { |
| 72 | + return serde_yaml::from_value::<YamlFilterGroup>(value) |
| 73 | + .map(YamlFilterItem::Group) |
| 74 | + .map_err(de::Error::custom); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Otherwise it's a Base filter |
| 79 | + serde_yaml::from_value::<YamlBaseFilter>(value) |
| 80 | + .map(YamlFilterItem::Base) |
| 81 | + .map_err(de::Error::custom) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +#[derive(Debug, Deserialize)] |
| 86 | +pub struct YamlFilterGroup { |
| 87 | + #[serde(default)] |
| 88 | + pub or: Option<Vec<YamlFilterItem>>, |
| 89 | + #[serde(default)] |
| 90 | + pub and: Option<Vec<YamlFilterItem>>, |
| 91 | +} |
| 92 | + |
| 93 | +#[derive(Debug, Deserialize)] |
| 94 | +pub struct YamlBaseFilter { |
| 95 | + #[serde(default)] |
| 96 | + pub member: Option<String>, |
| 97 | + #[serde(default)] |
| 98 | + pub dimension: Option<String>, |
| 99 | + #[serde(default)] |
| 100 | + pub operator: Option<String>, |
| 101 | + #[serde(default)] |
| 102 | + pub values: Option<Vec<Option<String>>>, |
| 103 | +} |
| 104 | + |
| 105 | +impl YamlFilterItem { |
| 106 | + pub fn into_filter_item(self) -> FilterItem { |
| 107 | + match self { |
| 108 | + YamlFilterItem::Group(group) => FilterItem { |
| 109 | + or: group.or.map(|items| { |
| 110 | + items |
| 111 | + .into_iter() |
| 112 | + .map(|item| item.into_filter_item()) |
| 113 | + .collect() |
| 114 | + }), |
| 115 | + and: group.and.map(|items| { |
| 116 | + items |
| 117 | + .into_iter() |
| 118 | + .map(|item| item.into_filter_item()) |
| 119 | + .collect() |
| 120 | + }), |
| 121 | + member: None, |
| 122 | + dimension: None, |
| 123 | + operator: None, |
| 124 | + values: None, |
| 125 | + }, |
| 126 | + YamlFilterItem::Base(base) => FilterItem { |
| 127 | + or: None, |
| 128 | + and: None, |
| 129 | + member: base.member, |
| 130 | + dimension: base.dimension, |
| 131 | + operator: base.operator, |
| 132 | + values: base.values, |
| 133 | + }, |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments