diff options
author | Alex Auvolat <alex@adnab.me> | 2023-06-13 11:35:30 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2023-06-13 11:35:30 +0200 |
commit | 2f968a585df2b560a2270f3323f578750bc15a67 (patch) | |
tree | 1b8bf384884ba0283c3da48928aba908fac56cfb | |
parent | 45e12c3bcd1198bdfce3a4f7f000ef4177067a5c (diff) | |
download | df-consul-2f968a585df2b560a2270f3323f578750bc15a67.tar.gz df-consul-2f968a585df2b560a2270f3323f578750bc15a67.zip |
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/catalog.rs | 17 |
2 files changed, 17 insertions, 2 deletions
@@ -2,7 +2,7 @@ name = "df-consul" description = "Deuxfleurs' async Rust bindings for (a subset of) the Consul HTTP API" authors = [ "Alex Auvolat <alex@adnab.me>" ] -version = "0.3.4" +version = "0.3.5" edition = "2021" license = "MIT" repository = "https://git.deuxfleurs.fr/Deuxfleurs/df-consul" diff --git a/src/catalog.rs b/src/catalog.rs index fe82c5a..87c7775 100644 --- a/src/catalog.rs +++ b/src/catalog.rs @@ -13,7 +13,7 @@ use futures::future::BoxFuture; use futures::stream::futures_unordered::FuturesUnordered; use futures::{FutureExt, StreamExt, TryFutureExt}; use log::*; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Deserializer, Serialize}; use tokio::select; use tokio::sync::watch; @@ -26,6 +26,7 @@ use crate::{Consul, WithIndex}; pub struct Node { pub node: String, pub address: String, + #[serde(default, deserialize_with = "deserialize_null_default")] pub meta: HashMap<String, String>, } @@ -37,6 +38,8 @@ pub struct Service { pub address: String, pub port: u16, pub tags: Vec<String>, + #[serde(default, deserialize_with = "deserialize_null_default")] + pub meta: HashMap<String, String>, } /// Full node info, as specified in response to "retrieve map of services for a node" API call in @@ -45,6 +48,7 @@ pub struct Service { #[serde(rename_all = "PascalCase")] pub struct CatalogNode { pub node: Node, + #[serde(default, deserialize_with = "deserialize_null_default")] pub services: HashMap<String, Service>, } @@ -59,11 +63,14 @@ pub type ServiceList = HashMap<String, Vec<String>>; pub struct ServiceNode { pub node: String, pub address: String, + #[serde(default, deserialize_with = "deserialize_null_default")] pub node_meta: HashMap<String, String>, pub service_name: String, pub service_tags: Vec<String>, pub service_address: String, pub service_port: u16, + #[serde(default, deserialize_with = "deserialize_null_default")] + pub service_meta: HashMap<String, String>, } /// Node serving a service with health info, @@ -273,3 +280,11 @@ fn retry_to_time(retries: usize, max_time: Duration) -> Duration { Duration::from_secs_f64(2.0f64 * 1.5f64.powf(retries as f64)), ) } + +fn deserialize_null_default<'de, D, T>(deserializer: D) -> Result<T, D::Error> +where + T: Default + Deserialize<'de>, + D: Deserializer<'de>, +{ + Option::deserialize(deserializer).map(Option::unwrap_or_default) +} |