use std::collections::HashSet;
use serde::ser::SerializeMap;
use sonic_rs::{JsonValueTrait, Value};
use super::super::api::plugin::{RequestContextPluginRead, RequestContextPluginWrite};
use super::super::deser::RequestContextValueExt;
use super::RequestContextDomain;
use super::RequestContextError;
use crate::executor::hooks;
pub trait CanWriteProgressiveOverride {}
impl CanWriteProgressiveOverride for hooks::OnQueryPlan {}
impl CanWriteProgressiveOverride for hooks::OnHttpRequest {}
impl CanWriteProgressiveOverride for hooks::OnGraphqlParams {}
impl CanWriteProgressiveOverride for hooks::OnGraphqlParse {}
impl CanWriteProgressiveOverride for hooks::OnGraphqlValidation {}
pub(crate) const UNRESOLVED_LABELS_KEY: &str = "hive::progressive_override::unresolved_labels";
pub(crate) const LABELS_TO_OVERRIDE_KEY: &str = "hive::progressive_override::labels_to_override";
#[derive(Debug, Clone, Default)]
pub struct ProgressiveOverrideContext {
pub unresolved_labels: Option<HashSet<String>>,
pub labels_to_override: Option<HashSet<String>>,
}
impl ProgressiveOverrideContext {
fn set_labels_to_override_value(&mut self, value: Value) -> Result<(), RequestContextError> {
if value.is_null() {
self.labels_to_override = None;
return Ok(());
}
let array = value.expect_array(LABELS_TO_OVERRIDE_KEY, "array of strings or null")?;
let mut labels = HashSet::with_capacity(array.len());
for item in array {
let label = item.expect_str(LABELS_TO_OVERRIDE_KEY, "array of strings or null")?;
labels.insert(label.to_string());
}
self.labels_to_override = Some(labels);
Ok(())
}
}
pub struct RequestContextProgressiveOverrideRead<'a> {
context: &'a ProgressiveOverrideContext,
}
impl RequestContextProgressiveOverrideRead<'_> {
pub fn unresolved_labels(&self) -> Option<&HashSet<String>> {
self.context.unresolved_labels.as_ref()
}
pub fn labels_to_override(&self) -> Option<&HashSet<String>> {
self.context.labels_to_override.as_ref()
}
}
pub struct RequestContextProgressiveOverrideWrite<'a> {
context: &'a mut ProgressiveOverrideContext,
}
impl RequestContextProgressiveOverrideWrite<'_> {
pub fn set_labels_to_override(&mut self, labels: Option<HashSet<String>>) -> &mut Self {
self.context.labels_to_override = labels;
self
}
}
impl<Hook> RequestContextPluginRead<Hook> {
pub fn progressive_override(&self) -> RequestContextProgressiveOverrideRead<'_> {
RequestContextProgressiveOverrideRead {
context: &self.snapshot.progressive_override,
}
}
}
impl<Hook: CanWriteProgressiveOverride> RequestContextPluginWrite<'_, Hook> {
pub fn progressive_override(&mut self) -> RequestContextProgressiveOverrideWrite<'_> {
RequestContextProgressiveOverrideWrite {
context: &mut self.context.progressive_override,
}
}
}
impl RequestContextDomain for ProgressiveOverrideContext {
const DOMAIN_PREFIX: &'static str = "hive::progressive_override::";
fn set_key_value(&mut self, key: &str, value: Value) -> Result<(), RequestContextError> {
match key {
UNRESOLVED_LABELS_KEY => self.forbidden_mutation(key),
LABELS_TO_OVERRIDE_KEY => self.set_labels_to_override_value(value),
_ => self.unknown_key(key),
}
}
super::impl_domain_serde!(
UNRESOLVED_LABELS_KEY => unresolved_labels,
LABELS_TO_OVERRIDE_KEY => labels_to_override,
);
}