SSPkrolik / nimongo

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

validation at CT and (de)serialization of Bson against a Nim type #60

Open timotheecour opened 5 years ago

timotheecour commented 5 years ago

validation at CT

to make programs more type safe and prevent errors due to field name or type, the following would be very useful:

type Doc = object
  kind: string
  nimid: int
  # badfield  not present
  badtype: int
  ommittedfield: string  # not all fields need to be present for validate to succeed

let doc = %*{
  "kind": "foo",
  "nimid": 32,
  "badfield": 123,
  "badtype": 12.3, # float instead of int
}

doc.validate(Doc) # CT failure because of badfield and badtype

serialization/deserialization

similar to above but I don't think it can be reduced to a common use case in Bson which is to specify only a subset of fields

var doc: Bson = ... # see above
var doc2: Doc = doc.to(Doc)
var doc3: Bson = doc2.to(Bson)
doAssert doc3 == doc

Note: this is implemented in D in vibe.d

yglukhov commented 5 years ago

Is it in scope of a mongodb driver though, which nimongo is? Could be well implemented separately.

timotheecour commented 5 years ago

yes, it could be a separate nimble package (that would depend on nimongo/bson); but can we keep it open as placeholder