SSPkrolik / nimongo

Pure Nim lang MongoDB driver
http://sspkrolik.github.io/nimongo
MIT License
101 stars 20 forks source link

support simplified syntax (as in mongodb cmdline client) for BSON fields: `{foo: "bar"}` (to mean: `{"foo": "bar"}`) #59

Open timotheecour opened 5 years ago

timotheecour commented 5 years ago

mongodb command line client supports specifying fields without quotes:

{foo: "bar"} can be used instead of: {"foo": "bar"}

I'm wondering whether this idea could be used in nimongo/bson to simplify BSON construction, using an appropriate macro that would understand untyped params

simplified example

import macros

macro foo(bar: untyped): untyped=
  let temp = repr(bar)
  result = quote do:
    var ret = ""
    ret.addQuoted `temp`
    ret

echo foo(hello_world) # prints: "hello_world"
echo foo(helloWorld) # prints: "helloWorld"

note

obviously, this would only be used for fields that are valid symbols

let doc = toBsonMagicFields{
  foo1: "bar1",
  "foo2-special", "bar2", # here, we can't use foo2-special, "bar2"
}

note

note that toBsonMagicFields would ignore variables in scope for the "key" positions of each field definition, eg:

let foo1 = "bar"
let doc = toBsonMagicFields{
  foo1: foo1,
}
# transforms to:
let foo1 = "bar"
let doc = toBson{
  "foo1": foo1,
}