populateFromBSON

Undocumented in source. Be warned that the author may not have intended to support it.
void
populateFromBSON
(
T
)
(
ref T object
,)

Examples

populateFromBSON - ensure that population works with valid BSON data

import dutils.validation.constraints : ValidateMinimumLength,
  ValidateMaximumLength, ValidateMinimum, ValidateEmail, ValidateRequired;

struct Person {
  @ValidateMinimumLength(2)
  @ValidateMaximumLength(100)
  string name;

  @ValidateMinimum!double(20) double height;

  @ValidateEmail()
  @ValidateRequired()
  string email;

  @ValidateRequired()
  bool member;
}

auto data = BSON([
    "does not exists": BSON(true),
    "name": BSON("Anna"),
    "height": BSON(170.1),
    "email": BSON("anna@example.com"),
    "member": BSON(true)
    ]);

Person person;
populateFromBSON(person, data);

assert(person.name == "Anna", "expected name Anna");
assert(person.height == 170.1, "expected height 170");
assert(person.email == "anna@example.com", "expected email anna@example.com");
assert(person.member == true, "expected member true");

populateFromBSON - ensure that validation errors are thrown with invalid BSON data

import dutils.validation.validate : ValidationError, ValidationErrors;
import dutils.validation.constraints : ValidationErrorTypes, ValidateMinimumLength,
  ValidateMaximumLength, ValidateMinimum, ValidateEmail, ValidateRequired;

struct Person {
  @ValidateMinimumLength(2)
  @ValidateMaximumLength(100)
  string name;

  @ValidateMinimum!double(20) double height;

  @ValidateEmail()
  @ValidateRequired()
  string email;

  @ValidateRequired()
  bool member;
}

auto data = BSON([
    "does not exists": BSON(true),
    "name": BSON("Anna"),
    "height": BSON("not a number")
    ]);

Person person;

auto catched = false;
try {
  populateFromBSON(person, data);
} catch (ValidationErrors validation) {
  import std.conv : to;

  catched = true;
  assert(validation.errors.length == 4,
      "expected 4 errors, got " ~ validation.errors.length.to!string
      ~ " with message: " ~ validation.msg);
  assert(validation.errors[0].type == "type", "expected minimumLength error");
  assert(validation.errors[0].path == "height", "expected error path to be height");
  assert(validation.errors[1].type == "minimum", "expected minimum error");
  assert(validation.errors[1].path == "height", "expected error path to be height");
  assert(validation.errors[2].type == "required", "expected required error");
  assert(validation.errors[2].path == "email", "expected error path to be email");
  assert(validation.errors[3].type == "required", "expected required error");
  assert(validation.errors[3].path == "member", "expected error path to be member");
}

assert(catched == true, "did not catch the expected errors");

populateFromBSON - should populate

import dutils.validation.constraints : ValidateRequired, ValidateEmail;

struct Email {
  @ValidateRequired()
  @ValidateEmail()
  string to;

  @ValidateEmail()
  string from;

  string subject;

  @ValidateRequired()
  string body;
}

auto data = BSON([
    "does not exists": BSON(true),
    "to": BSON("anna@example.com"),
    "body": BSON("Some text")
    ]);

Email email;
populateFromBSON(email, data);

assert(email.to == "anna@example.com", "expected to to be anna@example.com");
assert(email.from == "", "expected from to be \"\"");
assert(email.subject == "", "expected from to be \"\"");
assert(email.body == "Some text", "expected from to be \"Some text\"");

Meta