public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Wolfgang Bumiller <w.bumiller@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox 10/18] schema: ExtractValueDeserializer
Date: Fri, 18 Dec 2020 12:25:58 +0100	[thread overview]
Message-ID: <20201218112608.6845-11-w.bumiller@proxmox.com> (raw)
In-Reply-To: <20201218112608.6845-1-w.bumiller@proxmox.com>

A deserializer which takes an `&mut Value` and an object
schema reference and deserializes by extracting (removing)
the values from the references serde Value.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
---
 proxmox/src/api/de.rs  | 270 +++++++++++++++++++++++++++++++++++++++++
 proxmox/src/api/mod.rs |   2 +
 2 files changed, 272 insertions(+)
 create mode 100644 proxmox/src/api/de.rs

diff --git a/proxmox/src/api/de.rs b/proxmox/src/api/de.rs
new file mode 100644
index 0000000..b48fd85
--- /dev/null
+++ b/proxmox/src/api/de.rs
@@ -0,0 +1,270 @@
+//! Partial object deserialization by extracting object portions from a Value using an api schema.
+
+use std::fmt;
+
+use serde::de::{self, IntoDeserializer, Visitor};
+use serde_json::Value;
+
+use crate::api::schema::{ObjectSchema, ObjectSchemaType, Schema};
+
+pub struct Error {
+    inner: anyhow::Error,
+}
+
+impl fmt::Debug for Error {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        fmt::Debug::fmt(&self.inner, f)
+    }
+}
+
+impl fmt::Display for Error {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        fmt::Display::fmt(&self.inner, f)
+    }
+}
+
+impl std::error::Error for Error {}
+
+impl serde::de::Error for Error {
+    fn custom<T: fmt::Display>(msg: T) -> Self {
+        Self {
+            inner: anyhow::format_err!("{}", msg),
+        }
+    }
+}
+
+impl From<serde_json::Error> for Error {
+    fn from(inner: serde_json::Error) -> Self {
+        Error {
+            inner: inner.into(),
+        }
+    }
+}
+
+pub struct ExtractValueDeserializer<'o> {
+    object: &'o mut serde_json::Map<String, Value>,
+    schema: &'static ObjectSchema,
+}
+
+impl<'o> ExtractValueDeserializer<'o> {
+    pub fn try_new(
+        object: &'o mut serde_json::Map<String, Value>,
+        schema: &'static Schema,
+    ) -> Option<Self> {
+        match schema {
+            Schema::Object(schema) => Some(Self { object, schema }),
+            _ => None,
+        }
+    }
+}
+
+macro_rules! deserialize_non_object {
+    ($name:ident) => {
+        fn $name<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            Err(de::Error::custom(
+                "deserializing partial object into type which is not an object",
+            ))
+        }
+    };
+    ($name:ident ( $($args:tt)* )) => {
+        fn $name<V>(self, $($args)*, _visitor: V) -> Result<V::Value, Self::Error>
+        where
+            V: Visitor<'de>,
+        {
+            Err(de::Error::custom(
+                "deserializing partial object into type which is not an object",
+            ))
+        }
+    };
+}
+
+impl<'de> de::Deserializer<'de> for ExtractValueDeserializer<'de> {
+    type Error = Error;
+
+    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
+    where
+        V: Visitor<'de>,
+    {
+        self.deserialize_map(visitor)
+    }
+
+    fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Error>
+    where
+        V: Visitor<'de>,
+    {
+        visitor.visit_map(MapAccess::<'de>::new(
+            self.object,
+            self.schema.properties().map(|(name, _, _)| *name),
+        ))
+    }
+
+    fn deserialize_struct<V>(
+        self,
+        _name: &'static str,
+        _fields: &'static [&'static str],
+        visitor: V,
+    ) -> Result<V::Value, Error>
+    where
+        V: Visitor<'de>,
+    {
+        visitor.visit_map(MapAccess::<'de>::new(
+            self.object,
+            self.schema.properties().map(|(name, _, _)| *name),
+        ))
+    }
+
+    deserialize_non_object!(deserialize_i8);
+    deserialize_non_object!(deserialize_i16);
+    deserialize_non_object!(deserialize_i32);
+    deserialize_non_object!(deserialize_i64);
+    deserialize_non_object!(deserialize_u8);
+    deserialize_non_object!(deserialize_u16);
+    deserialize_non_object!(deserialize_u32);
+    deserialize_non_object!(deserialize_u64);
+    deserialize_non_object!(deserialize_f32);
+    deserialize_non_object!(deserialize_f64);
+    deserialize_non_object!(deserialize_char);
+    deserialize_non_object!(deserialize_bool);
+    deserialize_non_object!(deserialize_str);
+    deserialize_non_object!(deserialize_string);
+    deserialize_non_object!(deserialize_bytes);
+    deserialize_non_object!(deserialize_byte_buf);
+    deserialize_non_object!(deserialize_option);
+    deserialize_non_object!(deserialize_seq);
+    deserialize_non_object!(deserialize_unit);
+    deserialize_non_object!(deserialize_identifier);
+    deserialize_non_object!(deserialize_unit_struct(_: &'static str));
+    deserialize_non_object!(deserialize_newtype_struct(_: &'static str));
+    deserialize_non_object!(deserialize_tuple(_: usize));
+    deserialize_non_object!(deserialize_tuple_struct(_: &'static str, _: usize));
+    deserialize_non_object!(deserialize_enum(
+        _: &'static str,
+        _: &'static [&'static str]
+    ));
+
+    fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+    where
+        V: Visitor<'de>,
+    {
+        visitor.visit_unit()
+    }
+}
+
+struct MapAccess<'o, I> {
+    object: &'o mut serde_json::Map<String, Value>,
+    iter: I,
+    value: Option<Value>,
+}
+
+impl<'o, I> MapAccess<'o, I>
+where
+    I: Iterator<Item = &'static str>,
+{
+    fn new(object: &'o mut serde_json::Map<String, Value>, iter: I) -> Self {
+        Self {
+            object,
+            iter,
+            value: None,
+        }
+    }
+}
+
+impl<'de, I> de::MapAccess<'de> for MapAccess<'de, I>
+where
+    I: Iterator<Item = &'static str>,
+{
+    type Error = Error;
+
+    fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
+    where
+        K: de::DeserializeSeed<'de>,
+    {
+        loop {
+            return match self.iter.next() {
+                Some(key) => match self.object.remove(key) {
+                    Some(value) => {
+                        self.value = Some(value);
+                        seed.deserialize(key.into_deserializer()).map(Some)
+                    }
+                    None => continue,
+                },
+                None => Ok(None),
+            };
+        }
+    }
+
+    fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
+    where
+        V: de::DeserializeSeed<'de>,
+    {
+        match self.value.take() {
+            Some(value) => seed.deserialize(value).map_err(Error::from),
+            None => Err(de::Error::custom("value is missing")),
+        }
+    }
+}
+
+#[test]
+fn test_extraction() {
+    use serde::Deserialize;
+
+    use crate::api::schema::StringSchema;
+
+    #[derive(Deserialize)]
+    struct Foo {
+        foo1: String,
+        foo2: String,
+    }
+
+    const SIMPLE_STRING: Schema = StringSchema::new("simple").schema();
+    const FOO_SCHEMA: Schema = ObjectSchema::new(
+        "A Foo",
+        &[
+            ("foo1", false, &SIMPLE_STRING),
+            ("foo2", false, &SIMPLE_STRING),
+        ],
+    )
+    .schema();
+
+    #[derive(Deserialize)]
+    struct Bar {
+        bar1: String,
+        bar2: String,
+    }
+
+    const BAR_SCHEMA: Schema = ObjectSchema::new(
+        "A Bar",
+        &[
+            ("bar1", false, &SIMPLE_STRING),
+            ("bar2", false, &SIMPLE_STRING),
+        ],
+    )
+    .schema();
+
+    let mut data = serde_json::json!({
+        "foo1": "hey1",
+        "foo2": "hey2",
+        "bar1": "there1",
+        "bar2": "there2",
+    });
+
+    let data = data.as_object_mut().unwrap();
+
+    let foo: Foo =
+        Foo::deserialize(ExtractValueDeserializer::try_new(data, &FOO_SCHEMA).unwrap()).unwrap();
+
+    assert!(data.remove("foo1").is_none());
+    assert!(data.remove("foo2").is_none());
+    assert_eq!(foo.foo1, "hey1");
+    assert_eq!(foo.foo2, "hey2");
+
+    let bar =
+        Bar::deserialize(ExtractValueDeserializer::try_new(data, &BAR_SCHEMA).unwrap()).unwrap();
+
+    assert!(data.is_empty());
+    assert_eq!(bar.bar1, "there1");
+    assert_eq!(bar.bar2, "there2");
+}
diff --git a/proxmox/src/api/mod.rs b/proxmox/src/api/mod.rs
index b0b8333..8c6f597 100644
--- a/proxmox/src/api/mod.rs
+++ b/proxmox/src/api/mod.rs
@@ -48,3 +48,5 @@ pub use router::{
 
 #[cfg(feature = "cli")]
 pub mod cli;
+
+pub mod de;
-- 
2.20.1





  parent reply	other threads:[~2020-12-18 11:37 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-18 11:25 [pbs-devel] [PATCH proxmox 00/18] Optional Return Types and AllOf schema Wolfgang Bumiller
2020-12-18 11:25 ` [pbs-devel] [PATCH proxmox 01/18] formatting fixup Wolfgang Bumiller
2020-12-18 11:25 ` [pbs-devel] [PATCH proxmox 02/18] schema: support optional return values Wolfgang Bumiller
2020-12-18 11:25 ` [pbs-devel] [PATCH proxmox 03/18] api-macro: " Wolfgang Bumiller
2020-12-18 11:25 ` [pbs-devel] [PATCH proxmox 04/18] schema: support AllOf schemas Wolfgang Bumiller
2020-12-18 11:25 ` [pbs-devel] [PATCH proxmox 05/18] schema: allow AllOf schema as method parameter Wolfgang Bumiller
2020-12-18 11:25 ` [pbs-devel] [PATCH proxmox 06/18] api-macro: add 'flatten' to SerdeAttrib Wolfgang Bumiller
2020-12-18 11:25 ` [pbs-devel] [PATCH proxmox 07/18] api-macro: forbid flattened fields Wolfgang Bumiller
2020-12-18 11:25 ` [pbs-devel] [PATCH proxmox 08/18] api-macro: add more standard Maybe methods Wolfgang Bumiller
2020-12-18 11:25 ` [pbs-devel] [PATCH proxmox 09/18] api-macro: suport AllOf on structs Wolfgang Bumiller
2020-12-18 11:25 ` Wolfgang Bumiller [this message]
2020-12-18 11:25 ` [pbs-devel] [PATCH proxmox 11/18] api-macro: object schema entry tuple -> struct Wolfgang Bumiller
2020-12-18 11:26 ` [pbs-devel] [PATCH proxmox 12/18] api-macro: more tuple refactoring Wolfgang Bumiller
2020-12-18 11:26 ` [pbs-devel] [PATCH proxmox 13/18] api-macro: factor parameter extraction into a function Wolfgang Bumiller
2020-12-18 11:26 ` [pbs-devel] [PATCH proxmox 14/18] api-macro: support flattened parameters Wolfgang Bumiller
2020-12-18 11:26 ` [pbs-devel] [PATCH proxmox 15/18] schema: ParameterSchema at 'api' level Wolfgang Bumiller
2020-12-18 11:26 ` [pbs-devel] [PATCH proxmox 16/18] proxmox: temporary d/changelog update Wolfgang Bumiller
2020-12-18 11:26 ` [pbs-devel] [PATCH proxmox 17/18] macro: " Wolfgang Bumiller
2020-12-18 11:26 ` [pbs-devel] [PATCH proxmox 18/18] proxmox changelog update Wolfgang Bumiller
2020-12-18 11:26 ` [pbs-devel] [PATCH backup 1/2] adaptions for proxmox 0.9 and proxmox-api-macro 0.3 Wolfgang Bumiller
2020-12-18 11:26 ` [pbs-devel] [PATCH backup 2/2] tests: verify-api: check AllOf schemas Wolfgang Bumiller
2020-12-22  6:45 ` [pbs-devel] [PATCH proxmox 00/18] Optional Return Types and AllOf schema Dietmar Maurer
2020-12-22  6:51   ` Dietmar Maurer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201218112608.6845-11-w.bumiller@proxmox.com \
    --to=w.bumiller@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal