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 support for mixins. Closes #15 #22

Merged
merged 3 commits into from
Mar 16, 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 package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 29 additions & 1 deletion src/common/JSONImporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ define([
attribute_meta: {},
pointers: {},
pointer_meta: {},
mixins: [],
registry: {},
sets: {},
member_attributes: {},
Expand Down Expand Up @@ -61,6 +62,7 @@ define([
}));
const baseNode = this.core.getBase(node);
json.pointers.base = baseNode && this.core.getGuid(baseNode);
json.mixins = Object.values(this.core.getMixinNodes(node)).map(node => this.core.getGuid(node));

asyncTasks.push(...this.core.getOwnValidPointerNames(node).map(async name => {
const ptr_meta = this.core.getPointerMeta(node, name);
Expand All @@ -71,7 +73,9 @@ define([
json.registry[name] = this.core.getRegistry(node, name);
});

asyncTasks.push(...this.core.getOwnSetNames(node).map(async name => {
asyncTasks.push(...this.core.getOwnSetNames(node)
.filter(name => name !== '_mixins')
.map(async name => {
const paths = this.core.getMemberPaths(node, name);
const members = await Promise.all(paths.map(path => this.core.loadByPath(this.rootNode, path)));
const memberGuids = members.map(member => this.core.getGuid(member));
Expand Down Expand Up @@ -136,6 +140,7 @@ define([
'children_meta',
'pointer_meta',
'pointers',
'mixins',
'sets',
'member_attributes',
'member_registry',
Expand Down Expand Up @@ -319,6 +324,23 @@ define([
}
}

Importer.prototype._put.mixins = async function(node, change, resolvedSelectors) {
const [, index] = change.key;
const oldMixinPath = this.core.getMixinPaths(node)[index];
if (oldMixinPath) {
this.core.delMixin(node, oldMixinPath);
}

const mixinId = change.value;
const mixinPath = await this.getNodeId(node, mixinId, resolvedSelectors);
const canSet = this.core.canSetAsMixin(node, mixinPath);
if (canSet.isOk) {
this.core.addMixin(node, mixinPath);
} else {
throw new Error(`Cannot set ${mixinId} as mixin for ${this.core.getPath(node)}: ${canSet.reason}`);
}
};

Importer.prototype._put.attributes = function(node, change) {
assert(
change.key.length === 2,
Expand Down Expand Up @@ -439,6 +461,12 @@ define([
}
};

Importer.prototype._delete.mixins = async function(node, change, resolvedSelectors) {
const [, index] = change.key;
const mixinPath = this.core.getMixinPaths(node)[index];
this.core.delMixin(node, mixinPath);
};

Importer.prototype._put.children_meta = async function(node, change, resolvedSelectors) {
const [/*"children_meta"*/, idOrUndef] = change.key;
const isAddingContainment = !idOrUndef;
Expand Down
1 change: 1 addition & 0 deletions src/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ An example JSON is as follows:
name: idOrPath,
base: idOrPath,
},
mixins: [idOrPath, ...],
pointer_meta: {
name: {
idOrPath: {min=-1, max=1}, // max=-1 if it defines a set
Expand Down
60 changes: 60 additions & 0 deletions test/common/JSONImporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,66 @@ describe('JSONImporter', function () {
});
});

describe('mixins', function() {
let node4;

beforeEach(() => {
const base = node;
const parent = root;
node4 = core.createNode({base, parent});
core.setAttribute(node4, 'name', 'Node4');
core.addMember(root, 'MetaAspectSet', node4);

});

it('should add mixin', async function() {
const nodeId = core.getPath(node4);
original2.mixins.push(nodeId);
await importer.apply(node2, original2);
const mixins = core.getMixinPaths(node2);
assert(mixins.includes(nodeId));
assert.equal(mixins.length, 1);
});

it('should remove mixin', async function() {
const nodeId = core.getPath(node4);
core.addMixin(node2, nodeId);
await importer.apply(node2, original2);
const mixins = core.getMixinPaths(node2);
assert.equal(mixins.length, 0);
});

it('should change mixin', async function() {
const node5 = core.createNode({base: node, parent: root});
core.setAttribute(node5, 'name', 'Node5');
core.addMember(root, 'MetaAspectSet', node5);

const nodeId = core.getPath(node4);
core.addMixin(node2, nodeId);
original2.mixins.push(core.getPath(node5));
await importer.apply(node2, original2);
const mixins = core.getMixinPaths(node2);
assert.deepEqual(mixins, original2.mixins);
});

it('should add mixin using ID', async function() {
original2.mixins.push('@meta:Node4');
await importer.apply(node2, original2);
const mixins = core.getMixinPaths(node2);
const nodeId = core.getPath(node4);
assert(mixins.includes(nodeId));
assert.equal(mixins.length, 1);
});

it('should throw error on invalid mixin', async function() {
original2.mixins.push('@meta:FCO');
await assert.rejects(
() => importer.apply(node2, original2),
/Cannot set .* as mixin/
);
});
});

describe('sets', function() {
const setName = 'someSet';
let node4;
Expand Down