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 6 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
30 changes: 30 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,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;
Expand Down Expand Up @@ -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) {
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 == null) {
const fullpath = this.path(subpath);
throw new Error(`No value at path ${fullpath}`)
Copy link

@ghost ghost 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
54 changes: 54 additions & 0 deletions test/Model/collections.js
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', () => {
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`);
});
});
});
Loading