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 @internal namespace for auto-assigned node IDs. Fixes #31 #32

Merged
merged 2 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 4 additions & 3 deletions src/common/JSONImporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ define([
constructor(core, rootNode) {
this.core = core;
this.rootNode = rootNode;
this._nodeIDCounter = 1;
}

async _metaDictToGuids(meta_dict){
Expand Down Expand Up @@ -283,7 +284,7 @@ define([

async createNode(parent, state={}, base) {
if (!state.id) {
state.id = `@id:${Date.now() + Math.floor(100*Math.random())}`;
state.id = `@internal:${this._nodeIDCounter++}`;
}
const idString = state.id;
const fco = await this.core.loadByPath(this.rootNode, '/1');
Expand Down Expand Up @@ -776,7 +777,7 @@ define([
.find(child => core.getAttribute(child, attr) === value);
}

if (this.tag === '@id') {
if (this.tag === '@id' || this.tag === '@internal') {
return null;
}

Expand Down Expand Up @@ -877,7 +878,7 @@ define([

isAbsolute() {
return this.tag === '@meta' || this.tag === '@path' ||
this.tag === '@id' || this.tag === '@guid';
this.tag === '@id' || this.tag === '@guid' || this.tag === '@internal';
}
}

Expand Down
11 changes: 11 additions & 0 deletions test/common/JSONImporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,17 @@ describe('JSONImporter', function () {
);
});

it('should not have collisions for auto-gen IDs', async function() {
const fco = await core.loadByPath(root, '/1');
const container = core.createNode({base: fco, parent: root});
const childCount = 1000;
const schema = {
children: [...new Array(childCount)].map(() => ({})),
};
await importer.apply(container, schema);
assert.equal(childCount, core.getChildrenPaths(container).length);
});

describe('prepare', function() {
it('should add @meta node to META', async function() {
const selector = new Importer.NodeSelector('@meta:TestMeta');
Expand Down