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

Any reason for not providing Uuid wrapper just like UtcDataTime? #174

Closed
AtsukiTak opened this issue May 25, 2020 · 5 comments
Closed

Any reason for not providing Uuid wrapper just like UtcDataTime? #174

AtsukiTak opened this issue May 25, 2020 · 5 comments
Labels
tracked-in-jira Ticket filed in Mongo's Jira system

Comments

@AtsukiTak
Copy link

AtsukiTak commented May 25, 2020

Hi there! Thanks for all of your great work!

In my project, I want to use Uuid as document field, but it seems not to easy.
When I serialize Uuid into Bson, it always serialized as String because of its original implementation.

I think this situation is pretty similar to DateTime<Utc> whose thin wrapper is provided by this crate.

Is there any reason for not providing Uuid wrapper?

If it is provided, it would be very useful for me.

Sorry if I missed something. I'm pretty new to this crate.

Thanks.

@AtsukiTak
Copy link
Author

Currently I'm using small conversion function like

fn uuid_to_bson(uuid: Uuid) -> Bson {
    Bson::Binary(BinarySubtype::Uuid, uuid.as_bytes().to_vec())
}

fn uuid_from_bson(bson: Bson) -> anyhow::Result<Uuid> {
    match bson {
        Bson::Binary(BinarySubtype::Uuid, bytes) => Ok(Uuid::from_slice(bytes.as_slice())?),
        _ => Err(anyhow::anyhow!("given Bson is not Uuid")),
    }
}

@patrickfreed
Copy link
Contributor

Thanks for filing this issue! I filed RUST-465 on our jira project to track the work for it.

@AtsukiTak
Copy link
Author

@patrickfreed Thank you so much!

@isabelatkinson
Copy link
Contributor

Hi @AtsukiTak! RUST-506 introduces a uuid_as_binary module that simplifies working with the Uuid type. You can add the following attribute to a Uuid field to serialize to and deserialize from BSON Binary:

struct Item {
    #[serde(with = "uuid_as_binary")]
    pub id: Uuid,
    // other fields
}

@guija
Copy link

guija commented Jul 7, 2023

Currently I'm using small conversion function like

fn uuid_to_bson(uuid: Uuid) -> Bson {
    Bson::Binary(BinarySubtype::Uuid, uuid.as_bytes().to_vec())
}

fn uuid_from_bson(bson: Bson) -> anyhow::Result<Uuid> {
    match bson {
        Bson::Binary(BinarySubtype::Uuid, bytes) => Ok(Uuid::from_slice(bytes.as_slice())?),
        _ => Err(anyhow::anyhow!("given Bson is not Uuid")),
    }
}

This conversion is not working for me for Rust version 1.70. So I adapted it (especially the restructuring) and implemented a trait for it like below.
I still need this because at the moment I want to avoid working with concrete structs but rather work with plain Document and Bson.

trait BsonUuidConversion {
    fn as_uuid(&self) -> anyhow::Result<uuid::Uuid>;
}

impl BsonUuidConversion for Bson {
    fn as_uuid(&self) -> anyhow::Result<uuid::Uuid> {
        match self {
            Bson::Binary(binary) => match binary {
                mongodb::bson::binary::Binary {
                    subtype: _BinarySubtype,
                    bytes,
                } => Ok(uuid::Uuid::from_slice(bytes.as_slice())?),
                _ => Err(anyhow::anyhow!("given Bson is not Uuid")),
            },
            _ => Err(anyhow::anyhow!("given Bson is not Uuid")),
        }
    }
}

let bsonUuid : Bson = ...;
let uuid = bsonUuid.as_uuid();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tracked-in-jira Ticket filed in Mongo's Jira system
Projects
None yet
Development

No branches or pull requests

5 participants
@patrickfreed @guija @AtsukiTak @isabelatkinson and others