Skip to content

Commit 6d9afcb

Browse files
authored
Merge pull request share#507 from share/normalize-lm
🥅 Normalize `json0` `lm`
2 parents dbb5748 + 2ac114c commit 6d9afcb

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

lib/backend.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ Backend.prototype._fetchSnapshotByTimestamp = function(collection, id, timestamp
836836

837837
Backend.prototype._buildSnapshotFromOps = function(id, startingSnapshot, ops, callback) {
838838
var snapshot = startingSnapshot || new Snapshot(id, 0, null, undefined, null);
839-
var error = ot.applyOps(snapshot, ops, {_normalizeJson0Paths: true});
839+
var error = ot.applyOps(snapshot, ops, {_normalizeLegacyJson0Ops: true});
840840
callback(error, snapshot);
841841
};
842842

lib/ot.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ exports.applyOps = function(snapshot, ops, options) {
164164
options = options || {};
165165
for (var index = 0; index < ops.length; index++) {
166166
var op = ops[index];
167-
if (options._normalizeJson0Paths) normalizeJson0Paths(snapshot, op);
167+
if (options._normalizeLegacyJson0Ops) normalizeLegacyJson0Ops(snapshot, op);
168168
snapshot.v = op.v;
169169
var error = exports.apply(snapshot, op);
170170
if (error) return error;
@@ -205,15 +205,17 @@ exports.transformPresence = function(presence, op, isOwnOp) {
205205
* json0 had a breaking change in https://github.com/ottypes/json0/pull/40
206206
* The change added stricter type checking, which breaks fetchSnapshot()
207207
* when trying to rebuild a snapshot from old, committed ops that didn't
208-
* have this stricter validation. This method fixes up the op paths to
208+
* have this stricter validation. This method fixes up legacy ops to
209209
* pass the stricter validation
210210
*/
211-
function normalizeJson0Paths(snapshot, json0Op) {
211+
function normalizeLegacyJson0Ops(snapshot, json0Op) {
212212
if (snapshot.type !== types.defaultType.uri) return;
213213
var components = json0Op.op;
214214
if (!components) return;
215215
for (var i = 0; i < components.length; i++) {
216-
var path = components[i].p;
216+
var component = components[i];
217+
if (typeof component.lm === 'string') component.lm = +component.lm;
218+
var path = component.p;
217219
var element = snapshot.data;
218220
for (var j = 0; j < path.length; j++) {
219221
var key = path[j];

test/client/snapshot-version-request.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -441,16 +441,15 @@ describe('SnapshotVersionRequest', function() {
441441
});
442442
});
443443

444-
describe('invalid json0 path', function() {
444+
describe('invalid json0v2 path', function() {
445445
beforeEach(function(done) {
446446
var doc = backend.connect().get('series', 'his-dark-materials');
447-
doc.create([{title: 'Golden Compass'}], function(error) {
448-
if (error) return done(error);
449-
doc.submitOp({p: ['0', 'title'], od: 'Golden Compass', oi: 'Northern Lights'}, function(error) {
450-
if (error) return done(error);
451-
doc.submitOp({p: ['1'], li: {title: 'Subtle Knife'}}, done);
452-
});
453-
});
447+
async.series([
448+
doc.create.bind(doc, [{title: 'Golden Compass'}]),
449+
doc.submitOp.bind(doc, {p: ['0', 'title'], od: 'Golden Compass', oi: 'Northern Lights'}),
450+
doc.submitOp.bind(doc, {p: ['1'], li: {title: 'Subtle Knife'}}),
451+
doc.submitOp.bind(doc, {p: ['1'], lm: '0'})
452+
], done);
454453
});
455454

456455
describe('json0v1', function() {
@@ -477,13 +476,24 @@ describe('SnapshotVersionRequest', function() {
477476
types.register(defaultType);
478477
});
479478

480-
it('fetches v2 with json0v2', function(done) {
479+
it('fetches a string-indexed list insertion with json0v2', function(done) {
481480
backend.connect().fetchSnapshot('series', 'his-dark-materials', 2, function(error, snapshot) {
482481
if (error) return done(error);
483482
expect(snapshot.data).to.eql([{title: 'Northern Lights'}]);
484483
done();
485484
});
486485
});
486+
487+
it('fetches a list move using a string target', function(done) {
488+
backend.connect().fetchSnapshot('series', 'his-dark-materials', 4, function(error, snapshot) {
489+
if (error) return done(error);
490+
expect(snapshot.data).to.eql([
491+
{title: 'Subtle Knife'},
492+
{title: 'Northern Lights'}
493+
]);
494+
done();
495+
});
496+
});
487497
});
488498
});
489499
});

0 commit comments

Comments
 (0)