public inbox for pdm-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Gabriel Goller <g.goller@proxmox.com>
To: pdm-devel@lists.proxmox.com
Subject: [pdm-devel] [PATCH proxmox v2 3/3] section-config: add lookup and convert_to_typed_array helpers
Date: Wed, 23 Apr 2025 12:20:44 +0200	[thread overview]
Message-ID: <20250423102045.368629-4-g.goller@proxmox.com> (raw)
In-Reply-To: <20250423102045.368629-1-g.goller@proxmox.com>

These helpers exist on the untyped SectionConfigData struct, but not on
the typed one. To facilitate the migration to the typed
SectionConfigData<T> add these as macros.

These can be used to simply lookup an item with a specific type by
passing the type, e.g.:

    lookup!(config, "root", UserSectionConfig::User)

or retrieve a vector with the sections that correspond to the passed
type, e.g.:

    convert_to_typed_array!(config, UserSectionConfig::ApiToken)

Also add some simple unit-tests to test them.

Signed-off-by: Gabriel Goller <g.goller@proxmox.com>
---
 proxmox-section-config/Cargo.toml   |   3 +
 proxmox-section-config/src/typed.rs | 135 ++++++++++++++++++++++++++++
 2 files changed, 138 insertions(+)

diff --git a/proxmox-section-config/Cargo.toml b/proxmox-section-config/Cargo.toml
index 4796a6bff5e6..eb456a3bb3e6 100644
--- a/proxmox-section-config/Cargo.toml
+++ b/proxmox-section-config/Cargo.toml
@@ -19,3 +19,6 @@ serde_json.workspace = true
 proxmox-schema.workspace = true
 # FIXME: remove!
 proxmox-lang.workspace = true
+
+[dev-dependencies]
+serde = { workspace = true, features = ["derive"] }
diff --git a/proxmox-section-config/src/typed.rs b/proxmox-section-config/src/typed.rs
index 0152eeeb8a21..c05eab21b653 100644
--- a/proxmox-section-config/src/typed.rs
+++ b/proxmox-section-config/src/typed.rs
@@ -211,6 +211,35 @@ impl<T> SectionConfigData<T> {
     }
 }
 
+#[doc(hidden)]
+#[macro_export]
+macro_rules! lookup {
+    ($map:expr, $key:expr, $variant:path) => {
+        match $map.get($key) {
+            Some($variant(inner)) => Some(inner),
+            _ => None,
+        }
+    };
+}
+
+#[doc(hidden)]
+#[macro_export]
+macro_rules! convert_to_typed_array {
+    ($map:expr, $variant:path) => {
+        $map.values()
+            .filter_map(|value| match value {
+                $variant(inner) => Some(inner),
+                _ => None,
+            })
+            .collect::<Vec<_>>()
+    };
+}
+
+#[doc(inline)]
+pub use convert_to_typed_array;
+#[doc(inline)]
+pub use lookup;
+
 impl<T: ApiSectionDataEntry + DeserializeOwned> TryFrom<RawSectionConfigData>
     for SectionConfigData<T>
 {
@@ -371,6 +400,7 @@ mod test {
     use std::sync::OnceLock;
 
     use proxmox_schema::{ApiStringFormat, EnumEntry, ObjectSchema, Schema, StringSchema};
+    use serde::{Deserialize, Serialize};
 
     use crate::{SectionConfig, SectionConfigPlugin};
 
@@ -512,4 +542,109 @@ mod test {
             .expect("failed to write out section config");
         assert_eq!(written, raw);
     }
+
+    #[derive(Deserialize, Serialize, Debug, PartialEq, Eq, PartialOrd, Ord)]
+    struct User {
+        user_id: String,
+    }
+
+    #[derive(Deserialize, Serialize, Debug, PartialEq, Eq, PartialOrd, Ord)]
+    struct Token {
+        token: String,
+    }
+
+    #[derive(Deserialize, Serialize)]
+    enum UserSectionConfig {
+        #[serde(rename = "user")]
+        User(User),
+        #[serde(rename = "token")]
+        Token(Token),
+    }
+
+    const USER_PROPERTIES: ObjectSchema = ObjectSchema::new(
+        "User",
+        &[("user_id", false, &StringSchema::new("Some id.").schema())],
+    );
+
+    const TOKEN_PROPERTIES: ObjectSchema = ObjectSchema::new(
+        "Token",
+        &[("token", false, &StringSchema::new("Some token.").schema())],
+    );
+
+    impl ApiSectionDataEntry for UserSectionConfig {
+        fn section_config() -> &'static SectionConfig {
+            static SC: OnceLock<SectionConfig> = OnceLock::new();
+
+            SC.get_or_init(|| {
+                let mut config = SectionConfig::new(&ID_SCHEMA);
+                config.register_plugin(SectionConfigPlugin::new(
+                    "user".to_string(),
+                    None,
+                    &USER_PROPERTIES,
+                ));
+                config.register_plugin(SectionConfigPlugin::new(
+                    "token".to_string(),
+                    None,
+                    &TOKEN_PROPERTIES,
+                ));
+                config
+            })
+        }
+
+        fn section_type(&self) -> &'static str {
+            match self {
+                UserSectionConfig::User(_) => "user",
+                UserSectionConfig::Token(_) => "token",
+            }
+        }
+    }
+
+    #[test]
+    fn test_macros() {
+        let filename = "sync.cfg";
+        let raw = "\
+            token: first\n\
+                \ttoken 1\n\
+            \n\
+            user: second\n\
+                \tuser_id 2\n\
+            \n\
+            user: third\n\
+                \tuser_id 4\n\
+        ";
+
+        let parsed = UserSectionConfig::section_config()
+            .parse(filename, raw)
+            .expect("failed to parse");
+        let config: SectionConfigData<UserSectionConfig> =
+            parsed.try_into().expect("failed to convert");
+
+        let token = lookup!(config, "first", UserSectionConfig::Token);
+        assert_eq!(token.unwrap().token, "1");
+        let user = lookup!(config, "second", UserSectionConfig::User);
+        assert_eq!(user.unwrap().user_id, "2");
+
+        let mut tokens = convert_to_typed_array!(config, UserSectionConfig::Token);
+        tokens.sort();
+        assert_eq!(
+            tokens,
+            vec![&Token {
+                token: "1".to_owned()
+            }]
+        );
+
+        let mut users = convert_to_typed_array!(config, UserSectionConfig::User);
+        users.sort();
+        assert_eq!(
+            users,
+            vec![
+                &User {
+                    user_id: "2".to_owned()
+                },
+                &User {
+                    user_id: "4".to_owned()
+                }
+            ]
+        );
+    }
 }
-- 
2.39.5



_______________________________________________
pdm-devel mailing list
pdm-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel


  parent reply	other threads:[~2025-04-23 10:21 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-23 10:20 [pdm-devel] [PATCH proxmox{, -datacenter-manager} v2 0/4] Remove direct access to SectionConfigData<T> sections Gabriel Goller
2025-04-23 10:20 ` [pdm-devel] [PATCH proxmox v2 1/3] section-config: make write_section_config parameter more generic Gabriel Goller
2025-04-23 10:20 ` [pdm-devel] [PATCH proxmox v2 2/3] section-config: remove DerefMut and make underlying HashMap private Gabriel Goller
2025-04-23 10:20 ` Gabriel Goller [this message]
2025-04-23 10:20 ` [pdm-devel] [PATCH proxmox-datacenter-manager v2 1/1] remotes: remove direct access on underlying sections HashMap Gabriel Goller
2025-05-06  9:56 ` [pdm-devel] applied-series: [PATCH proxmox{, -datacenter-manager} v2 0/4] Remove direct access to SectionConfigData<T> sections Wolfgang Bumiller

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=20250423102045.368629-4-g.goller@proxmox.com \
    --to=g.goller@proxmox.com \
    --cc=pdm-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