Comments (7)
cc @kyeah
from bson-rust.
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.
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.
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.
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.
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.
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)
- Deserialization implementation for Document doesn't handle documents with extjson keys HOT 1
- Feature Request: Consider CompactString for Document keys HOT 1
- Number type (u16) field of struct throwing "invalid type: floating point `21010`, expected u16" HOT 2
- Use RawDocument/Buf in doc! HOT 3
- serde_with `SerializeAs` & `DeserializeAs` support for time crate HOT 2
- Add Option<ObjectId> serialize_object_id_as_hex_string support HOT 3
- Serialising and deserialising a BSON document with Regex can change the document HOT 3
- Better Details for BSON Deserialization Errors HOT 5
- use simd to optimize uft8 validation. HOT 3
- Bson fails to serialize AND deserialize IPAddr keys in hashmaps HOT 3
- `js-sys` should not be used when targeting `wasm32-wasi` HOT 3
- add ObjectId deserialization from sequence to support messagepack HOT 8
- Inconsistency between documentation and code implementation HOT 3
- RUST-1892 incorrect serialization/deserializer on big endian machines ( s390x ) HOT 5
- Serialization issue with Uuid HOT 3
- RUST-1899 Deserialization with UUID HOT 5
- How to deserialize ISO formatted json into Bson::DateTime HOT 1
- Time's "large-dates" feature should be optional HOT 1
- document has bytes remaining that were not visited HOT 4
- Operating on BSON Decimal128 type HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bson-rust.