Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
timotheecour opened this issue Nov 16, 2018 · 0 comments

Comments

@timotheecour
Copy link
Contributor

timotheecour commented Nov 16, 2018

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,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant