Skip to content

Commit

Permalink
Merge pull request #412 from getodk/ktuite/strip-orx
Browse files Browse the repository at this point in the history
Strip xml prefix (e.g. orx:) from field names in diffs
  • Loading branch information
ktuite authored Oct 11, 2021
2 parents 61b1848 + 8a27688 commit 2ec6ce2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/data/submission.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ const hparser = require('htmlparser2');
const fmdiff = require('fast-myers-diff');
const { SchemaStack } = require('./schema');
const { noop } = require('../util/util');
const { stripNamespacesFromPath } = require('../util/xml');
const { union, last, pluck } = require('ramda');



// reads submission xml with the streaming parser, and outputs a stream of every
// field in the submission. does no processing at all to localize repeat groups,
// etc. it's just a stream of found data values.
Expand Down Expand Up @@ -210,14 +212,14 @@ const _diffObj = (a, b, subpath) => {
for (const key of union(a[keys], b[keys])) {
const av = a[key];
if (!Object.prototype.hasOwnProperty.call(a, key)) { // null -> b
results.push({ new: b[key], path: subpath.concat([ key ]) });
results.push({ new: b[key], path: subpath.concat([ stripNamespacesFromPath(key) ]) });
} else if (!Object.prototype.hasOwnProperty.call(b, key)) { // a -> null
results.push({ old: av, path: subpath.concat([ key ]) });
results.push({ old: av, path: subpath.concat([ stripNamespacesFromPath(key) ]) });
} else if (av[subhash] == null) { // primitive
if (av !== b[key]) // a -> b
results.push({ old: av, new: b[key], path: subpath.concat([ key ]) });
results.push({ old: av, new: b[key], path: subpath.concat([ stripNamespacesFromPath(key) ]) });
} else if (av[subhash] !== b[key][subhash]) { // structural a -> b
results.push(..._recurseDiff(av, b[key], subpath, key));
results.push(..._recurseDiff(av, b[key], subpath, stripNamespacesFromPath(key)));
}
}
return results;
Expand Down
15 changes: 14 additions & 1 deletion test/unit/data/submission.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ describe('diffing', () => {
{ instanceId: 'three', xml: testData.instances.withrepeat.three }
]);
const expected = [
{ old: 'rthree', new: 'rtwo', path: [ 'orx:meta', 'orx:instanceID' ] },
{ old: 'rthree', new: 'rtwo', path: [ 'meta', 'instanceID' ] },
{ old: 'Chelsea', new: 'Bob', path: [ 'name' ] },
{ old: '38', new: '34', path: [ 'age' ] },
{ old: 'Candace', new: 'Billy', path: [ 'children', [ 'child', 0 ], 'name' ] },
Expand Down Expand Up @@ -587,6 +587,19 @@ describe('diffing', () => {
});
});
});

it('should strip XML prefix from field names', () =>
structuralFieldsFor(testData.forms.withrepeat).then((fields) => {
const newer = '<data id="withrepeat" version="1.0"><orx:meta><orx:instanceID>john</orx:instanceID></orx:meta><name>Bob</name><age>34</age><children><child><name>Billy</name><age>4</age></child><child><name>Blaine</name><age>6</age></child></children></data>';
const older = '<data id="withrepeat" version="1.0"><orx:meta><orx:instanceID>jon</orx:instanceID></orx:meta><name>Bob</name><age>34</age><children><child><name>Billy</name><age>4</age></child><child><name>Blaine</name><age>6</age></child></children></data>';

diffSubmissions(fields, [
{ instanceId: 'john', xml: newer },
{ instanceId: 'jon', xml: older }
]).should.eql({
'john': [{ old: 'jon', new: 'john', path: [ 'meta', 'instanceID'] }]
});
}));
});
});

0 comments on commit 2ec6ce2

Please sign in to comment.