-
Notifications
You must be signed in to change notification settings - Fork 118
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
Add methods getOrDefault and getOrThrow #307
Conversation
const value = this.get(subpath); | ||
if (value === undefined) { | ||
const fullpath = [this._at, subpath].filter(Boolean).join('.'); | ||
throw new Error(`No value at path ${fullpath}`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way we can include the collection name in the error? I don't know if it's really needed, if we get the call stack, but it might be helpful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The full path will already include the collection name - Model#_at
is the child model's absolute path from the root.
@@ -152,6 +168,19 @@ Model.prototype.getOrCreateDoc = function(collectionName, id, data) { | |||
return collection.getOrCreateDoc(id, data); | |||
}; | |||
|
|||
Model.prototype.getOrDefault = function<S>(subpath: Path, defaultValue: S) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mentioning for discussion - as this is now, it won't support a 1-arg call like getOrDefault(defaultValue)
, though there is the workaround of doing getOrDefault('', defaultValue)
.
We can always add it later if needed and we don't want to add it now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like the intent is as a fallback in case the subpath fails? . which would make sense then to keep the structure... I'd argue that providing blank subpath or defaultValue raise error no? They are both required?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eric and I discussed ofline and agree the no-subpath case is unlikely, so leaving as is and see if any use comes up.
Added methods for common patterns found in production code:
Data we know we should have in app:
using
getOrThrow
Data we may or may not have but want to cleaner code
using
getOrDefault