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

Explicit extension namespaces #45

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
37 changes: 26 additions & 11 deletions lib/descriptor-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,20 @@ DescriptorBuilder.prototype.build = function() {
* @param {Object} p
* @param {Number} [idx]
* @param {Boolean} [validate=true]
* @param {Boolean} [inherited=false]
*/
DescriptorBuilder.prototype.addProperty = function(p, idx, validate) {
DescriptorBuilder.prototype.addProperty = function(p, idx, validate, inherited) {

if (typeof idx === 'boolean') {
validate = idx;
idx = undefined;
}

this.addNamedProperty(p, validate !== false);
this.addNamedProperty(p, validate !== false, inherited);

var properties = this.properties;

if (idx !== undefined) {
if (typeof idx === 'number') {
properties.splice(idx, 0, p);
} else {
properties.push(p);
Expand Down Expand Up @@ -105,10 +106,16 @@ DescriptorBuilder.prototype.replaceProperty = function(oldProperty, newProperty,
// * validate only if this is a "rename" operation
// * add at specific index unless we "replace"
//
this.addProperty(newProperty, replace ? undefined : idx, rename);
this.addProperty(newProperty, replace ? undefined : idx, rename, oldProperty.inherited);

if (oldProperty.inherited) {

// make new property available under old localName
propertiesByName[oldNameNs.localName] = newProperty;
}

// make new property available under old name
propertiesByName[oldNameNs.name] = propertiesByName[oldNameNs.localName] = newProperty;
propertiesByName[oldNameNs.name] = newProperty;
};


Expand All @@ -130,16 +137,23 @@ DescriptorBuilder.prototype.redefineProperty = function(p, targetPropertyName, r
delete p.redefines;
};

DescriptorBuilder.prototype.addNamedProperty = function(p, validate) {
DescriptorBuilder.prototype.addNamedProperty = function(p, validate, inherited) {
var ns = p.ns,
propsByName = this.propertiesByName;

if (validate) {
if (inherited) {
this.assertNotDefined(p, ns.localName);
}

this.assertNotDefined(p, ns.name);
this.assertNotDefined(p, ns.localName);
}

propsByName[ns.name] = propsByName[ns.localName] = p;
if (inherited) {
propsByName[ns.localName] = p;
}

propsByName[ns.name] = p;
};

DescriptorBuilder.prototype.removeNamedProperty = function(p) {
Expand Down Expand Up @@ -218,8 +232,8 @@ DescriptorBuilder.prototype.addTrait = function(t, inherited) {

// clone property to allow extensions
p = assign({}, p, {
name: p.ns.localName,
inherited: inherited
name: inherited ? p.ns.localName : p.ns.name,
inherited
});

Object.defineProperty(p, 'definedBy', {
Expand All @@ -239,7 +253,8 @@ DescriptorBuilder.prototype.addTrait = function(t, inherited) {
if (p.isId) {
this.setIdProperty(p);
}
this.addProperty(p);

this.addProperty(p, null, true, inherited);
}
}, this));

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "moddle",
"version": "6.2.1",
"version": "7.0.0-exp.3",
"description": "A library for importing meta-model based file formats into JS",
"scripts": {
"all": "run-s lint test distro",
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/model/extension/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{
"name": "Root",
"properties": [
{ "name": "id", "type": "String" },
{ "name": "own", "type": "Own" },
{ "name": "ownAttr", "type": "String", "isAttr": true },
{ "name": "generic", "type": "Element" },
Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/model/extension/custom.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
"superClass": [ "Base" ],
"extends": [ "b:Root" ],
"properties": [
{ "name": "own", "type": "b:Own" },
{ "name": "customAttr", "type": "Integer", "isAttr": true },
{ "name": "generic", "type": "CustomGeneric", "redefines": "b:Root#generic" }
{ "name": "generic", "type": "CustomGeneric", "redefines": "b:Root#generic" },
{ "name": "id", "type": "String", "replaces": "b:Root#id", "isAttr": true, "isId": true }
]
},
{
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/model/multiple-inheritance/base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "Multiple Inheritance Base",
"uri": "http://multiple-inheritance-base",
"prefix": "b",
"types": [
{
"name": "Element",
"properties": [
{ "name": "ownedElement", "type": "b:Element", "isMany": true }
]
},
{
"name": "PackageableElement",
"superClass": [ "b:Element" ]
}
]
}
16 changes: 16 additions & 0 deletions test/fixtures/model/multiple-inheritance/glue.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "Multiple Inheritance Glue",
"uri": "http://multiple-inheritance-glue",
"prefix": "g",
"types": [
{
"name": "Diagram",
"superClass": [
"b:PackageableElement"
],
"extends": [
"o:Diagram"
]
}
]
}
19 changes: 19 additions & 0 deletions test/fixtures/model/multiple-inheritance/other.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "Multiple Inheritance Other",
"uri": "http://multiple-inheritance-other",
"prefix": "o",
"types": [
{
"name": "DiagramElement",
"properties": [
{ "name": "ownedElement", "type": "DiagramElement", "isMany": true }
]
},
{
"name": "Diagram",
"superClass": [
"DiagramElement"
]
}
]
}
17 changes: 17 additions & 0 deletions test/fixtures/model/redefines/custom.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "Redefines Custom",
"uri": "http://redefines-custom",
"prefix": "c",
"types": [
{
"name": "Base",
"extends": [
"b:Base"
],
"properties": [
{ "name": "value", "type": "String" },
{ "name": "id", "type": "Integer", "redefines": "b:Base#id", "isId": true }
]
}
]
}
17 changes: 17 additions & 0 deletions test/fixtures/model/redefines/extension.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "Redefines Extension",
"uri": "http://redefines-extension",
"prefix": "e",
"types": [
{
"name": "Base",
"superClass": [
"b:Base"
],
"properties": [
{ "name": "value", "type": "String" },
{ "name": "id", "type": "Integer", "redefines": "b:Base#id", "isId": true }
]
}
]
}
17 changes: 17 additions & 0 deletions test/fixtures/model/replaces/custom.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "Replaces Extension",
"uri": "http://replace-extension",
"prefix": "c",
"types": [
{
"name": "Base",
"extends": [
"b:Base"
],
"properties": [
{ "name": "value", "type": "String" },
{ "name": "id", "type": "Integer", "replaces": "b:Base#id", "isId": true }
]
}
]
}
17 changes: 17 additions & 0 deletions test/fixtures/model/replaces/extension.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "Replaces Extension",
"uri": "http://replace-extension",
"prefix": "e",
"types": [
{
"name": "Base",
"superClass": [
"b:Base"
],
"properties": [
{ "name": "value", "type": "String" },
{ "name": "id", "type": "Integer", "replaces": "b:Base#id", "isId": true }
]
}
]
}
Loading