Giter VIP home page Giter VIP logo

Comments (7)

zonyitoo avatar zonyitoo commented on July 29, 2024

cc @kyeah

from bson-rust.

greenpdx avatar greenpdx commented on July 29, 2024

I worked out a way that is a little cleaner

` let mut buf = Vec::new();
sess.serialize(&mut rmp_serde::Serializer::new(&mut buf)).unwrap();
let src = (BinarySubtype::UserDefined(129), buf.clone());

  coll.insert_one(doc!{"sess" => src, "addr" => addr, "ts" => ts, "prekey" => fast, "nonce" => hello, "role" => 0, "salt" => Null }, None).unwrap();

Now I define 129 and session, I am still working on decoding.
`

from bson-rust.

greenpdx avatar greenpdx commented on July 29, 2024

I hope I get and answer to this question. to want to convert a struct to document. I want to pass a value of a field to the document. I have tried to do something like this.
But it does not like tl.FIELD, How do I pass a field to the doc! macro?

struct TmpLogin {
  sess: String,  // client session base64 encoded sodiumoxide PublicKey
  addr: String,  // remote address for hacking issues
  start: i64,   // session start time
  last: i64,   // last access
  prekey: PrecomputedKey,
  nonce: String,  // session nonce base64 encoded
  salt: Option<String>,  // The ID of the user, only decode during verification
  role: i32,
}

impl From<TmpLogin> for Document {
  fn from(tl: TmpLogin) -> Document {
    let last = DateTime::timestamp(&Utc::now());
    let doc = doc! {
      "sess" => tl.sess,
      "addr" => tl.addr,
      "start" => tl.start,
      "last" => last,
      "prekey" =>   BINARY FIELD,
      "nonce" => tl.nonce,
      "salt" =>  tl.salt,
      "role" => tl.role,
    };
    doc
  }
}

from bson-rust.

saghm avatar saghm commented on July 29, 2024

Can you be more specific than "does not like"? Is it a parser error due to the macro not yielding valid code? Is it a type error due to the field not being implicitly converted to BSON? Does it compile, but not give the result you expect when run?

from bson-rust.

kyeah avatar kyeah commented on July 29, 2024

hey @greenpdx, IIRC the doc macro doesn't support .s in open value patterns at the moment; you may want to try wrapping your values in parens.

    let doc = doc! {
      "sess" => (tl.sess),
      "addr" => (tl.addr),
      "start" => (tl.start),
      "last" => last,
      "prekey" =>   BINARY FIELD,
      "nonce" => (tl.nonce),
      "salt" =>  (tl.salt),
      "role" => (tl.role),
    };

from bson-rust.

kyeah avatar kyeah commented on July 29, 2024

note that if PrecomputedKey implements serde::ser::Serialize and serde::de::Deserialize, you can derive the traits for TmpLogin and utilize our to_bson/from_bson helpers:

#[derive(Serialize, Deserialize)]
struct TmpLogin {
  sess: String,  // client session base64 encoded sodiumoxide PublicKey
  addr: String,  // remote address for hacking issues
  start: i64,   // session start time
  last: i64,   // last access
  prekey: PrecomputedKey,
  nonce: String,  // session nonce base64 encoded
  salt: Option<String>,  // The ID of the user, only decode during verification
  role: i32,
}

impl From<TmpLogin> for Document {
  fn from(tl: TmpLogin) -> Document {
    let last = DateTime::timestamp(&Utc::now());

    bson::to_bson(&tl)?
      .as_document()
      .map(|doc| {
        doc.insert("last", last);
        doc
      })
      .unwrap()
  }
}

from bson-rust.

greenpdx avatar greenpdx commented on July 29, 2024

Thank you the () works now. so now I have
pub fn to_bson(&self) -> Document { let doc = doc! { "email" => (&self.email), "fname" => (&self.fname), "lname" => (&self.lname), "salt" => (&self.salt), "pass" => (&self.pass), "age" => (self.age as i32), "wealth" => (self.wealth as i32), "social" => (self.social as i32), "fiscal" => (self.fiscal as i32), "id" => (&self.id), "first" => (self.first), "last" => (self.last) }; doc }

Thanks

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.