-
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
Changes from 6 commits
1b7b47c
9b27547
1efc5df
74633c2
2cd00a2
c7548ba
703b7fe
abd2592
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import { Doc } from './Doc'; | |
import { Model, RootModel } from './Model'; | ||
import { JSONObject } from 'sharedb/lib/sharedb'; | ||
import type { Path, ReadonlyDeep, ShallowCopiedValue, Segments } from '../types'; | ||
|
||
var LocalDoc = require('./LocalDoc'); | ||
var util = require('../util'); | ||
|
||
|
@@ -75,6 +76,22 @@ declare module './Model' { | |
getOrCreateCollection(name: string): Collection; | ||
getOrCreateDoc(collectionName: string, id: string, data: any); | ||
|
||
/** | ||
* Gets value at the path if not nullish, otherwise returns provided default value | ||
* | ||
* @param subpath | ||
* @param defaultValue value to return if no value at subpath | ||
*/ | ||
getOrDefault<S>(subpath: Path, defaultValue: S): ReadonlyDeep<S>; | ||
|
||
/** | ||
* Gets the value located at this model's path or a relative subpath. | ||
* | ||
* If no value exists at the path, or the value is nullish (null or undefined), this will throw an error. | ||
* @param subpath | ||
*/ | ||
getOrThrow<S>(subpath: Path): ReadonlyDeep<S>; | ||
|
||
_get(segments: Segments): any; | ||
_getCopy(segments: Segments): any; | ||
_getDeepCopy(segments: Segments): any; | ||
|
@@ -152,6 +169,19 @@ Model.prototype.getOrCreateDoc = function(collectionName, id, data) { | |
return collection.getOrCreateDoc(id, data); | ||
}; | ||
|
||
Model.prototype.getOrDefault = function<S>(subpath: Path, defaultValue: S) { | ||
return this.get(subpath) ?? defaultValue as ReadonlyDeep<S>; | ||
}; | ||
|
||
Model.prototype.getOrThrow = function<S>(subpath?: Path) { | ||
const value = this.get(subpath); | ||
if (value == null) { | ||
const fullpath = this.path(subpath); | ||
throw new Error(`No value at path ${fullpath}`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. The full path will already include the collection name - |
||
} | ||
return value; | ||
}; | ||
|
||
/** | ||
* @param {String} subpath | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const {expect} = require('../util'); | ||
const {RootModel} = require('../../lib'); | ||
|
||
describe('collections', () => { | ||
describe('getOrDefault', () => { | ||
it('returns value if defined', () => { | ||
const model = new RootModel(); | ||
model.add('_test_doc', {name: 'foo'}); | ||
const value = model.getOrDefault('_test_doc', {name: 'bar'}); | ||
expect(value).not.to.be.undefined; | ||
}); | ||
|
||
it('returns defuault value if undefined', () => { | ||
const model = new RootModel(); | ||
const defaultValue = {name: 'bar'}; | ||
const value = model.getOrDefault('_test_doc', defaultValue); | ||
expect(value).not.to.be.undefined; | ||
expect(value.name).to.equal('bar'); | ||
expect(value).to.eql(defaultValue); | ||
}); | ||
|
||
it('returns defult value if null', () => { | ||
craigbeck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const model = new RootModel(); | ||
const id = model.add('_test_doc', {name: null}); | ||
const defaultValue = 'bar'; | ||
const value = model.getOrDefault(`_test_doc.${id}.name`, defaultValue); | ||
expect(value).not.to.be.null; | ||
expect(value).to.equal('bar'); | ||
expect(value).to.eql(defaultValue); | ||
}); | ||
}); | ||
|
||
describe('getOrThrow', () => { | ||
it('returns value if defined', () => { | ||
const model = new RootModel(); | ||
model.add('_test_doc', {name: 'foo'}); | ||
const value = model.getOrThrow('_test_doc'); | ||
expect(value).not.to.be.undefined; | ||
}); | ||
|
||
it('throws if value undefined', () => { | ||
const model = new RootModel(); | ||
expect(() => model.getOrThrow('_test_doc')).to.throw(`No value at path _test_doc`); | ||
expect(() => model.scope('_test').getOrThrow('doc.1')).to.throw(`No value at path _test.doc.1`); | ||
}); | ||
|
||
it('throws if value null', () => { | ||
const model = new RootModel(); | ||
const id = model.add('_test_doc', {name: null}); | ||
expect(model.getOrThrow(`_test_doc.${id}`)).to.eql({id, name: null}); | ||
expect(() => model.getOrThrow(`_test_doc.${id}.name`)).to.throw(`No value at path _test_doc`); | ||
}); | ||
}); | ||
}); |
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 doinggetOrDefault('', 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.