#
k8s_openapi/v1_35/apimachinery/pkg/apis/meta/v1/
label_selector.rs

    1// Generated from definition io.k8s.apimachinery.pkg.apis.meta.v1.LabelSelector
    2
    3/// A label selector is a label query over a set of resources. The result of matchLabels and matchExpressions are ANDed. An empty label selector matches all objects. A null label selector matches no objects.
    4#[derive(Clone, Debug, Default, PartialEq)]
    5pub struct LabelSelector {
    6    /// matchExpressions is a list of label selector requirements. The requirements are ANDed.
    7    pub match_expressions: Option<std::vec::Vec<crate::apimachinery::pkg::apis::meta::v1::LabelSelectorRequirement>>,
    8
    9    /// matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed.
    10    pub match_labels: Option<std::collections::BTreeMap<std::string::String, std::string::String>>,
    11}
    12
    13impl crate::DeepMerge for LabelSelector {
    14    fn merge_from(&mut self, other: Self) {
    15        crate::merge_strategies::list::atomic(&mut self.match_expressions, other.match_expressions);
    16        crate::merge_strategies::map::granular(&mut self.match_labels, other.match_labels, |current_item, other_item| {
    17            crate::DeepMerge::merge_from(current_item, other_item);
    18        });
    19    }
    20}
    21
    22impl<'de> crate::serde::Deserialize<'de> for LabelSelector {
    23    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: crate::serde::Deserializer<'de> {
    24        #[allow(non_camel_case_types)]
    25        enum Field {
    26            Key_match_expressions,
    27            Key_match_labels,
    28            Other,
    29        }
    30
    31        impl<'de> crate::serde::Deserialize<'de> for Field {
    32            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: crate::serde::Deserializer<'de> {
    33                struct Visitor;
    34
    35                impl crate::serde::de::Visitor<'_> for Visitor {
    36                    type Value = Field;
    37
    38                    fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    39                        f.write_str("field identifier")
    40                    }
    41
    42                    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where E: crate::serde::de::Error {
    43                        Ok(match v {
    44                            "matchExpressions" => Field::Key_match_expressions,
    45                            "matchLabels" => Field::Key_match_labels,
    46                            _ => Field::Other,
    47                        })
    48                    }
    49                }
    50
    51                deserializer.deserialize_identifier(Visitor)
    52            }
    53        }
    54
    55        struct Visitor;
    56
    57        impl<'de> crate::serde::de::Visitor<'de> for Visitor {
    58            type Value = LabelSelector;
    59
    60            fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    61                f.write_str("LabelSelector")
    62            }
    63
    64            fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> where A: crate::serde::de::MapAccess<'de> {
    65                let mut value_match_expressions: Option<std::vec::Vec<crate::apimachinery::pkg::apis::meta::v1::LabelSelectorRequirement>> = None;
    66                let mut value_match_labels: Option<std::collections::BTreeMap<std::string::String, std::string::String>> = None;
    67
    68                while let Some(key) = crate::serde::de::MapAccess::next_key::<Field>(&mut map)? {
    69                    match key {
    70                        Field::Key_match_expressions => value_match_expressions = crate::serde::de::MapAccess::next_value(&mut map)?,
    71                        Field::Key_match_labels => value_match_labels = crate::serde::de::MapAccess::next_value(&mut map)?,
    72                        Field::Other => { let _: crate::serde::de::IgnoredAny = crate::serde::de::MapAccess::next_value(&mut map)?; },
    73                    }
    74                }
    75
    76                Ok(LabelSelector {
    77                    match_expressions: value_match_expressions,
    78                    match_labels: value_match_labels,
    79                })
    80            }
    81        }
    82
    83        deserializer.deserialize_struct(
    84            "LabelSelector",
    85            &[
    86                "matchExpressions",
    87                "matchLabels",
    88            ],
    89            Visitor,
    90        )
    91    }
    92}
    93
    94impl crate::serde::Serialize for LabelSelector {
    95    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: crate::serde::Serializer {
    96        let mut state = serializer.serialize_struct(
    97            "LabelSelector",
    98            self.match_expressions.as_ref().map_or(0, |_| 1) +
    99            self.match_labels.as_ref().map_or(0, |_| 1),
    100        )?;
    101        if let Some(value) = &self.match_expressions {
    102            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "matchExpressions", value)?;
    103        }
    104        if let Some(value) = &self.match_labels {
    105            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "matchLabels", value)?;
    106        }
    107        crate::serde::ser::SerializeStruct::end(state)
    108    }
    109}
    110
    111#[cfg(feature = "schemars")]
    112impl crate::schemars::JsonSchema for LabelSelector {
    113    fn schema_name() -> std::borrow::Cow<'static, str> {
    114        "io.k8s.apimachinery.pkg.apis.meta.v1.LabelSelector".into()
    115    }
    116
    117    fn json_schema(__gen: &mut crate::schemars::SchemaGenerator) -> crate::schemars::Schema {
    118        crate::schemars::json_schema!({
    119            "description": "A label selector is a label query over a set of resources. The result of matchLabels and matchExpressions are ANDed. An empty label selector matches all objects. A null label selector matches no objects.",
    120            "type": "object",
    121            "properties": {
    122                "matchExpressions": {
    123                    "description": "matchExpressions is a list of label selector requirements. The requirements are ANDed.",
    124                    "type": "array",
    125                    "items": (__gen.subschema_for::<crate::apimachinery::pkg::apis::meta::v1::LabelSelectorRequirement>()),
    126                },
    127                "matchLabels": {
    128                    "description": "matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is \"key\", the operator is \"In\", and the values array contains only \"value\". The requirements are ANDed.",
    129                    "type": "object",
    130                    "additionalProperties": {
    131                        "type": "string",
    132                    },
    133                },
    134            },
    135        })
    136    }
    137}