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

consider fully qualified name during ID resolution. Closes #19 #20

Merged
merged 2 commits into from
Feb 10, 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
3 changes: 2 additions & 1 deletion config/config.webgme.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var config = require('webgme/config/config.default'),

// The paths can be loaded from the webgme-setup.json
config.plugin.basePaths.push(__dirname + '/../src/plugins');
config.seedProjects.basePaths.push(__dirname + '/../src/seeds/test');



Expand All @@ -23,6 +24,6 @@ config.requirejsPaths = {
};


config.mongo.uri = 'mongodb://127.0.0.1:27017/webgme_json';
config.mongo.uri = 'mongodb://127.0.0.1:27017/webgme_json_importer';
validateConfig(config);
module.exports = config;
27 changes: 24 additions & 3 deletions src/common/JSONImporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,9 +705,30 @@ define([
}

if (this.tag === '@meta') {
const meta = await core.getAllMetaNodes(rootNode);
return Object.values(meta)
.find(child => core.getAttribute(child, 'name') === this.value);
const metanodes = Object.values(core.getAllMetaNodes(rootNode));
const libraries = core.getLibraryNames(rootNode)
.map(name => [
core.getPath(core.getLibraryRoot(rootNode, name)),
name,
]);

function getFullyQualifiedName(node) {
const name = core.getAttribute(node, 'name');
const path = core.getPath(node);
const libraryPair = libraries.find(([rootPath,]) => path.startsWith(rootPath));
if (libraryPair) {
const [,libraryName] = libraryPair;
return libraryName + '.' + name;
}
return name;
}

return metanodes
.find(child => {
const name = core.getAttribute(child, 'name');
const fullName = getFullyQualifiedName(child);
return name === this.value || fullName === this.value;
});
}

if (this.tag === '@attribute') {
Expand Down
Binary file added src/seeds/test/test.webgmex
Binary file not shown.
74 changes: 51 additions & 23 deletions test/common/JSONImporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('JSONImporter', function () {
const assert = require('assert');
const gmeConfig = testFixture.getGmeConfig();
const path = testFixture.path;
const SEED_DIR = path.join(__dirname, '..', '..', 'src', 'seeds');
const Q = testFixture.Q;
const logger = testFixture.logger.fork('JSONImporter');
const projectName = 'testProject';
Expand All @@ -25,7 +26,7 @@ describe('JSONImporter', function () {
storage = testFixture.getMemoryStorage(logger, gmeConfig, gmeAuth);
await storage.openDatabase();
const importParam = {
projectSeed: path.join(testFixture.SEED_DIR, 'EmptyProject.webgmex'),
projectSeed: path.join(SEED_DIR, 'test', 'test.webgmex'),
projectName: projectName,
branchName: 'master',
logger: logger,
Expand Down Expand Up @@ -58,10 +59,12 @@ describe('JSONImporter', function () {
let importer,
node,
original,
root;
root,
fco;

beforeEach(async () => {
root = await getNewRootNode(core);
fco = await core.loadByPath(root, '/1');
importer = new Importer(core, root);
node = (await core.loadChildren(root))[0];
original = await importer.toJSON(node);
Expand All @@ -75,27 +78,33 @@ describe('JSONImporter', function () {
});

it('should set attributes using @name', async function() {
const rootSchema = await importer.toJSON(root);
rootSchema.children = [
const container = core.createNode({base: fco, parent: root});
const child = core.createNode({base: fco, parent: container});
core.setAttribute(child, 'name', 'TEST!');
const schema = await importer.toJSON(container);
schema.children = [
{
id: '@name:FCO',
id: '@name:TEST!',
attributes: {name: 'NewName'},
}
];
await importer.apply(root, rootSchema);
assert.equal(core.getAttribute(node, 'name'), 'NewName');
await importer.apply(container, schema);
assert.equal(core.getAttribute(child, 'name'), 'NewName');
});

it('should set attributes using @attribute:name:FCO', async function() {
const rootSchema = await importer.toJSON(root);
rootSchema.children = [
it('should set attributes using @attribute:name:<name>', async function() {
const container = core.createNode({base: fco, parent: root});
const child = core.createNode({base: fco, parent: container});
core.setAttribute(child, 'name', 'TEST!');
const schema = await importer.toJSON(container);
schema.children = [
{
id: '@attribute:name:FCO',
id: '@attribute:name:TEST!',
attributes: {name: 'NewName'},
}
];
await importer.apply(root, rootSchema);
assert.equal(core.getAttribute(node, 'name'), 'NewName');
await importer.apply(container, schema);
assert.equal(core.getAttribute(child, 'name'), 'NewName');
});

it('should delete attributes', async function() {
Expand Down Expand Up @@ -228,7 +237,6 @@ describe('JSONImporter', function () {
});

it('should preserve children on base change', async function() {
const fco = await core.loadByPath(root, '/1');
const childNode = core.createNode({base: fco, parent: node2});
const childPath = core.getPath(childNode);
core.setAttribute(childNode, 'name', 'ChildNode');
Expand All @@ -246,12 +254,14 @@ describe('JSONImporter', function () {

it('should resolve @meta tag even if renamed during changes', async function() {
const fco = await core.loadByPath(root, '/1');
const node = core.createNode({base: fco, parent: root});
core.setAttribute(node, 'name', 'MetaNode');
core.addMember(root, 'MetaAspectSet', node);
const metanode = core.createNode({base: fco, parent: root});
core.setAttribute(metanode, 'name', 'MetaNode');
core.addMember(root, 'MetaAspectSet', metanode);
const container = core.createNode({base: fco, parent: root});
const node = core.createNode({base: fco, parent: container});
core.setAttribute(node, 'name', 'testNode');

const newJSON = {
attributes: {name: 'root'},
children: [
{
id: '@meta:MetaNode',
Expand All @@ -260,17 +270,35 @@ describe('JSONImporter', function () {
}
},
{
id: '@meta:FCO',
id: '@name:testNode',
pointers: {
base: '@meta:MetaNode',
testPtr: '@meta:MetaNode',
}
},
]
};

await importer.apply(root, newJSON);
assert.equal(core.getAttribute(node, 'name'), 'NewMetaNodeName');
assert.equal(core.getPointerPath(fco, 'testPtr'), core.getPath(node));
await importer.apply(container, newJSON);
assert.equal(core.getAttribute(metanode, 'name'), 'NewMetaNodeName');
assert.equal(core.getPointerPath(node, 'testPtr'), core.getPath(metanode));
});

it('should resolve @meta tag when library prefix used', async function() {
const node = core.createNode({parent: root, base: fco});
core.setAttribute(node, 'name', 'libPrefixTest');
const state = {
pointers: {
base: '@meta:FCO',
ptr: '@meta:myLibrary.A',
}
};
await importer.apply(node, state);
const libraryAPath = '/f/n';
assert.equal(
core.getPointerPath(node, 'ptr'),
libraryAPath
);
});

it('should set base correctly during structural inheritance', async function() {
Expand Down Expand Up @@ -1040,7 +1068,7 @@ describe('JSONImporter', function () {
});

it('should create new node', async function() {
assert.equal(children.length, 2);
assert.equal(children.length, 3);
});

it('should apply changes to new node', async function() {
Expand Down
8 changes: 7 additions & 1 deletion webgme-setup.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@
"src": "src/plugins/ExportToJSON",
"test": "test/plugins/ExportToJSON"
}
},
"seeds": {
"test": {
"src": "src/seeds/test"
}
}
},
"dependencies": {
"plugins": {}
"plugins": {},
"seeds": {}
}
}