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

Add methods getOrDefault and getOrThrow #307

Merged
merged 8 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Model/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -75,6 +76,21 @@ declare module './Model' {
getOrCreateCollection(name: string): Collection;
getOrCreateDoc(collectionName: string, id: string, data: any);

/**
* Get a value that may be undefined but ensure value is returned
craigbeck marked this conversation as resolved.
Show resolved Hide resolved
*
* @param subpath
* @param defaultValue value to return if no value at subpath
*/
getOrDefault<S>(subpath: Path, defaultValue: S): ReadonlyDeep<S>;

/**
* Get a value and throw error if undefined
craigbeck marked this conversation as resolved.
Show resolved Hide resolved
*
* @param subpath
*/
getOrThrow<S>(subpath: Path): ReadonlyDeep<S>;

_get(segments: Segments): any;
_getCopy(segments: Segments): any;
_getDeepCopy(segments: Segments): any;
Expand Down Expand Up @@ -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) {
Copy link
Contributor

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.

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?

Copy link
Contributor Author

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.

return this.get(subpath) ?? defaultValue as ReadonlyDeep<S>;
};

Model.prototype.getOrThrow = function<S>(subpath?: Path) {
const value = this.get(subpath);
if (value === undefined) {
craigbeck marked this conversation as resolved.
Show resolved Hide resolved
const fullpath = [this._at, subpath].filter(Boolean).join('.');
craigbeck marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(`No value at path ${fullpath}`)
Copy link

@colincollerlever colincollerlever May 23, 2024

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.

Copy link
Contributor

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.

}
return value;
};

/**
* @param {String} subpath
*/
Expand Down
37 changes: 37 additions & 0 deletions test/Model/collections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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);
});
});

describe('getOrThrow', () => {
it('returns value if defined', () => {
const model = new RootModel();
model.add('_test_doc', {name: 'foo'});
const value = model.getOrThrow('_test_doc', {name: 'bar'});
expect(value).not.to.be.undefined;
});

it('thows if value undefined', () => {
const model = new RootModel();
expect(() => model.getOrThrow('_test_doc', {name: 'bar'})).to.throw(`No value at path _test_doc`);
expect(() => model.scope('_test').getOrThrow('doc.1', {name: 'bar'})).to.throw(`No value at path _test.doc.1`);
});
});
});
Loading