mashingan / anonimongo

Another Nim pure Mongo DB driver
MIT License
43 stars 5 forks source link

generic to should work instead of non-generic ofInt etc #10

Closed timotheecour closed 4 years ago

timotheecour commented 4 years ago
let a1 = mybson.to(int) # doesn't work but should, for generic code
let a2 = old["time"].ofInt # works but not generic
mashingan commented 4 years ago

The to macro works exclusively for BsonDocument to Type. I could overload to to convert more flat types but refrained to do so.

You can provide your own overloaded to for flat types like:


template to(b: BsonBase, typ: typedesc): untyped =
  var r: typ = b
  r

let b5 = 5.toBson
let i5 = b5.to(int)
doAssert i5 == 5

Note that we can immediately doAssert b5 == 5 above as the ofInt (and all variants of ofType ) is converter.

I don't think will add this to the lib, should this be added as example instead?

timotheecour commented 4 years ago

@mashingan custom operators like !> are usually frowned upon, for good reasons:

operators do make sense in certain situations like std/wrapnils because of technical reasons (operator precedence), I can give more details and point to relevant discussion if needed.

I really like the previous bson name, why was it changed to !> ?

Visually !> can be (forcefully) looked like b.

this is a stretch ;-)

it looks like you did that to avoid the parens in

bson({ insertId: 8})
=>
!>{ insertId: 8}

but the parens don't seem needed even with non operators, eg:

import std/json
template foo(t: untyped): untyped = %* t
let s = foo {"a1": 12}
echo s.pretty
mashingan commented 4 years ago

custom operators like !> are usually frowned upon, for good reasons: it looks like you did that to avoid the parens in

the bson is still working, and many internal implementations are still using it.
Yes, it's added for convenience to avoid the parenthesis, last time I tried to omit the parenthesis, it was failed. IIRC, I tried it like bson{....} and failed. Can't remember whether it's exactly so or not.

this is a stretch ;-)

Haha, maybe I can revert this but I'll see for several releases whether this is really convenient or just my misunderstanding.
It's added to conveniently define inline bson used as argument in several APIs. Well, if it's to disappear, it won't be in for long though.

timotheecour commented 4 years ago

Yes, it's added for convenience to avoid the parenthesis, last time I tried to omit the parenthesis, it was failed. IIRC, I tried it like bson{....} and failed. Can't remember whether it's exactly so or not.

that's because you need a space, this works and is what I'm using:

bson { insertId: 8}

unless I'm missing something, I'd recommend to just use bson and remove or deprecate !> (but optionally, change some examples to use the parenthesis-less version as shown above)

mashingan commented 4 years ago

Reasonable, I don't want to add more aliases and the !> indeed doesn't do much to be included in the lib.
I'll revert/remove the !> in the next tagged version.
I'll update the examples in readme soon as it doesn't need the tagged version.