Giter VIP home page Giter VIP logo

Comments (4)

zonyitoo avatar zonyitoo commented on July 29, 2024

Hmm. I don't quite understand what you are about to do....

You are going to create an OrderedDocument from a bson::Bson, what is the actual subtype of that bson::Bson? Bson::Document?

from bson-rust.

lrlna avatar lrlna commented on July 29, 2024

ah sorry I wasn't very clear; a bit new to rust as well so can't formulate types that well yet :O

It is a Bson::Document, yes. And I want to be able to create an OrderedDocument from it. Preferably with just the doc! macro. But I can't figure out if there is a way to do that without supplying the key.

from bson-rust.

kyeah avatar kyeah commented on July 29, 2024

hi! A Bson::Document is a wrapper around an OrderedDocument, so you can convert by pattern-matching and pulling out the inner OrderedDocument (if it is a Bson::Document.)

To move the document out of the Bson enum (value cannot be used after this):

let inner_doc = match value {
  Bson::Document(doc) => doc,
   _ => panic!("expected a Bson::Document")
};

conditionally:

if let Bson::Document(inner_doc) = value {
  generate_schema_from_doc(inner_doc)
}

If you need to maintain the original Bson::Document, you can get a reference to the document:

// provided helper function from https://github.com/zonyitoo/bson-rs/blob/master/src/bson.rs#L500
// alternatively: value.as_document().expect('expected a Bson::Document');
let inner_doc_ref = value.as_document()?;

conditionally:

if let Bson::Document(ref inner_doc) = value {
  generate_schema_from_doc(inner_doc)
}

hope that makes sense!

from bson-rust.

lrlna avatar lrlna commented on July 29, 2024

hi! thank you so much for your help! I think doing something like the pattern matching you are describing will work. I am doing something like that at another point anyways o/

from bson-rust.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.