Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialize Bucket retention policy from String to u64 #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions google-cloud/src/deserializer/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::fmt;
use serde::de::{self, Unexpected};
use serde::Deserializer;

pub fn deserialize_u64_or_string<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_any(DeserializeU64OrStringVisitor)
}

struct DeserializeU64OrStringVisitor;
impl<'de> de::Visitor<'de> for DeserializeU64OrStringVisitor {
type Value = u64;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("an integer or a string")
}

fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(v)
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
if let Ok(n) = v.parse::<u64>() {
Ok(n)
} else if v.is_empty() {
Ok(0)
} else {
Err(E::invalid_value(Unexpected::Str(v), &self))
}
}
}
2 changes: 2 additions & 0 deletions google-cloud/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ extern crate google_cloud_derive;
pub mod authorize;
/// Error handling utilities.
pub mod error;
/// Complements serde deserializers.
pub mod deserializer;

/// Datastore bindings.
#[cfg(feature = "datastore")]
Expand Down
8 changes: 5 additions & 3 deletions google-cloud/src/storage/api/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};

use crate::storage::api::bucket_acl::BucketAclResource;
use crate::storage::api::object_acl::ObjectAclResource;
use crate::deserializer::deserialize_u64_or_string;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -49,9 +50,10 @@ pub struct BucketResource {
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BucketRetentionPolicy {
pub pubretention_period: u64,
pub pubeffective_time: String,
pub pubis_locked: bool,
#[serde(deserialize_with = "deserialize_u64_or_string")]
pub retention_period: u64,
pub effective_time: String,
pub is_locked: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
Expand Down