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

crow::json::wvalue lacks an 'object' initialization and assignment. #189

Closed
lcsdavid opened this issue Aug 6, 2021 · 2 comments · Fixed by #190
Closed

crow::json::wvalue lacks an 'object' initialization and assignment. #189

lcsdavid opened this issue Aug 6, 2021 · 2 comments · Fixed by #190
Labels
feature Code based project improvement

Comments

@lcsdavid
Copy link
Contributor

lcsdavid commented Aug 6, 2021

crow::json::wvalue lacks an explicit object initialization and assignment.

Currently to initialize a crow::json:wvalue we need to default construct the object and then populate values, hence some trivial operations are needlessly more wordy, or can be more straightforward and might be even more efficient (in-place construction optimisation, ...).

Examples

Classic object initialization
/* let's suppose that mentionned variables exist in this scope. */
crow::json::wvalue result;
result["username"] = username;
result["email"] = email;
result["firstname"] = firstname;
result["lastname"] = lastname;
return result;
Empty object initialization ({})
crow::json::wvalue result = crow::json::load("{}");

Proposals

By adding those proposals, object constructor can be more explicit and semantically accurate regarding to syntax.

std::unordered_map<std::string, wvalue> or std::map<std::string, wvalue>, const& and &&, constructors and assignements.
/* let's assume that mentionned variables exist in this scope. */
std::unordered_map<std::string, crow::json::wvalue> map =  {
/* might need to explicitly use std::pair here. */
  {"username", username},
  {"email", email},
  {"firstname", firstname},
  {"lastname", lastname}
};

crow::json::wvalue result = map;
crow::json::wvalue result =  std::move(map); 

/* let's ignore that map as been moved. */
result = map;
result = std::move(map);

/* can now create an empty object like this. */
/* rvalue constructor is called here so map construction is certainly in-placed if well done. */
crow::json::wvalue result = std::unordered_map<std::string, crow::json::walue>(); 
std::initializer_list<std::pair, wvalue> contructor and assignment.
/* let's assume that mentionned variables exist in this scope. */
crow::json::wvalue result =  {
/* might need to explicitly use std::pair here. */
  {"username", username},
  {"email", email},
  {"firstname", firstname},
  {"lastname", lastname}
};

result = {
/* might need to explicitly use std::pair here. */
  {"username", username},
  {"email", email},
  {"firstname", firstname},
  {"lastname", lastname}
};

Is there an interest into those proposals ? May I PR sometime soon ?

@The-EDev The-EDev added the feature Code based project improvement label Aug 6, 2021
@The-EDev
Copy link
Member

The-EDev commented Aug 6, 2021

Hi @lcsdavid, There absolutely is interest in every new addition or improvement. Thanks for pointing this out and proposing a solution. A PR would be great, and if you want to go the extra mile you can add to the existing examples and unit tests. Though it's not strictly necessary.

Thanks again for pointing out both the problem and a solution.

@lcsdavid
Copy link
Contributor Author

lcsdavid commented Aug 6, 2021

I'll do a PR probably this week-end so. Thanks for your feedback.

@lcsdavid lcsdavid mentioned this issue Aug 6, 2021
@The-EDev The-EDev linked a pull request Aug 6, 2021 that will close this issue
lcsdavid added a commit to lcsdavid/Crow that referenced this issue Aug 9, 2021
lcsdavid added a commit to lcsdavid/Crow that referenced this issue Aug 11, 2021
The-EDev added a commit to lcsdavid/Crow that referenced this issue Aug 14, 2021
The-EDev added a commit to lcsdavid/Crow that referenced this issue Aug 14, 2021
lcsdavid added a commit to lcsdavid/Crow that referenced this issue Aug 16, 2021
The-EDev added a commit that referenced this issue Aug 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Code based project improvement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants