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

Reduce Code Complexity #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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: 1 addition & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ rules:
dot-location: 0
dot-notation: 0
eqeqeq: 2
guard-for-in: 2
guard-for-in: 0
no-alert: 2
no-caller: 2
no-case-declarations: 2
no-div-regex: 2
no-else-return: 0
no-empty-label: 2
no-empty-pattern: 2
no-eq-null: 2
no-eval: 2
Expand Down
23 changes: 9 additions & 14 deletions lib/Action.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
const ParserCommon = require('./ParserCommon');
const AnnotatedObject = require('./AnnotatedObject');

class Action extends ParserCommon {
constructor(element) {
class Action extends AnnotatedObject {
constructor() {
super();
this.Annotations = {};
this.Parameters = {};
this.ReturnType = null;

this.validElements = {
'Annotation': {parent: this.Annotations, nameProp: 'Term'},
'Parameter': {parent: this.Parameters, nameProp: 'Name'},
'ReturnType': {name: 'ReturnType'}
};
this.validAttributes = {
'IsBound': {bool: true},
'Name': {alreadyHandeled: true}
};
this.addElementHandler('Parameter', this.addElementToObj, {parent: this.Parameters, nameProp: 'Name'});
this.addElementHandler('ReturnType', this.addElementToObj, {name: 'ReturnType'});

this.init(element, 'Name');
this.addAttributeHandler('IsBound', this.addBoolAttributeToObj, {});
this.addAttributeHandler('Name', null, {});

this.nameAttr = 'Name';
}
}

Expand Down
20 changes: 7 additions & 13 deletions lib/ActionImport.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
const ParserCommon = require('./ParserCommon');
const AnnotatedObject = require('./AnnotatedObject');

class ActionImport extends ParserCommon {
constructor(element) {
class ActionImport extends AnnotatedObject {
constructor() {
super();
this.Annotations = {};
this.addAttributeHandler('Action', this.addAttributeToObj, {});
this.addAttributeHandler('EntitySet', this.addAttributeToObj, {});
this.addAttributeHandler('Name', null, {});

this.validElements = {
'Annotation': {parent: this.Annotations, nameProp: 'Term'}
};
this.validAttributes = {
'Action': {},
'EntitySet': {},
'Name': {alreadyHandeled: true}
};
this.init(element, 'Name');
this.nameAttr = 'Name';
}
}

Expand Down
13 changes: 13 additions & 0 deletions lib/AnnotatedObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const ParserCommon = require('./ParserCommon');

class AnnotatedObject extends ParserCommon {
constructor() {
super();
this.Annotations = {};

this.addElementHandler('Annotation', this.addElementToObj, {parent: this.Annotations, nameProp: 'Term'});
}
}

module.exports = AnnotatedObject;
/* vim: set tabstop=2 shiftwidth=2 expandtab: */
27 changes: 12 additions & 15 deletions lib/Annotation.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
const ParserCommon = require('./ParserCommon');

class Annotation extends ParserCommon {
constructor(element) {
constructor() {
super();
this.validElements = {
'Collection': {parent: this, name: 'Collection'},
'Record': {parent: this, name: 'Record'},
'String': {parent: this, name: 'String', getText: true}
};
this.validAttributes = {
'Term': {alreadyHandeled: true},
'Qualifier': {},
'String': {},
'EnumMember': {},
'Bool': {bool: true},
'Int': {integer: true}
};
this.init(element, 'Term');
this.addElementHandler('Collection', this.addElementToObj, {name: 'Collection'});
this.addElementHandler('Record', this.addElementToObj, {name: 'Record'});
this.addElementHandler('String', this.addStringToObj, {name: 'String'});

this.addAttributeHandler('Term', null, {});
this.addAttributeHandler('Qualifier', this.addAttributeToObj, {});
this.addAttributeHandler('String', this.addAttributeToObj, {});
this.addAttributeHandler('EnumMember', this.addAttributeToObj, {});
this.addAttributeHandler('Bool', this.addBoolAttributeToObj, {});
this.addAttributeHandler('Int', this.addIntAttributeToObj, {});
this.nameAttr = 'Term';
}
}

Expand Down
131 changes: 76 additions & 55 deletions lib/CSDLSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
function searchAnnotation(collection, elementName) {
var ret = [];
if(elementName === undefined) {
for(var name in collection) {
if(name[0] === '_') {
continue;
}
ret.push(collection[name]);
}
return getAllValidElementsInCollection(collection);
}
else {
if(collection[elementName] !== undefined) {
Expand All @@ -17,6 +12,17 @@ function searchAnnotation(collection, elementName) {
return ret;
}

function getAllValidElementsInCollection(collection) {
let ret = [];
for(var name in collection) {
if(name[0] === '_') {
continue;
}
ret.push(collection[name]);
}
return ret;
}

function getElementIfNameEqual(element, elementName) {
if(elementName === undefined) {
return element;
Expand All @@ -43,37 +49,49 @@ function searchCSDL(root, elementType, elementName, includeReferences) {
if(key[0] === '_') {
continue;
}
else if(key === 'Annotations') {
if(elementType === 'Annotation') {
ret = ret.concat(searchAnnotation(root.Annotations, elementName));
}
continue;
}
else if(key === 'References' && includeReferences !== true) {
continue;
}
var element = root[key];
if(elementInvalid(element)) {
continue;
}
if(elementType !== undefined) {
if(elementType === element.constructor.name) {
pushIfNotUndefined(ret, getElementIfNameEqual(element, elementName));
}
else {
var tmp = searchCSDL(element, elementType, elementName);
if(tmp.length > 0) {
ret = ret.concat(tmp);
}
}
}
else {
pushIfNotUndefined(ret, getElementIfNameEqual(element, elementName));
}
ret = ret.concat(searchElementPart(key, root[key], elementType, elementName, includeReferences));
}
return ret;
}

function searchElementPart(key, element, elementType, elementName, includeReferences) {
let ret = [];
switch(key) {
case 'Annotations':
return processAnnotationsKey(element, elementType, elementName);
case 'References':
if(includeReferences !== true) {
return ret;
}
//Deliberate fallthrough...
default:
return processDefaultKey(element, elementType, elementName);
}
}

function processAnnotationsKey(element, elementType, elementName) {
if(elementType === 'Annotation') {
return searchAnnotation(element, elementName);
}
return [];
}

function processDefaultKey(element, elementType, elementName) {
let ret = [];
if(elementInvalid(element)) {
return ret;
}
if(elementType === undefined) {
pushIfNotUndefined(ret, getElementIfNameEqual(element, elementName));
return ret;
}
if(elementType === element.constructor.name) {
pushIfNotUndefined(ret, getElementIfNameEqual(element, elementName));
return ret;
}
return searchCSDL(element, elementType, elementName);
}

function remapNamespace(references, namespace) {
for(var i = 0; i < references.length; i++) {
if(references[i].Includes[namespace] !== undefined) {
Expand All @@ -95,34 +113,37 @@ function findByType(metadata, typeName) {
var justType = typeName.substring(index+1);
var schema = metadata[namespace];
if(schema === undefined) {
let schemas = metadata._options.cache.getSchemas(namespace);
if(schemas.length === 0) {
if(metadata.References === undefined) {
return null;
}
namespace = remapNamespace(metadata.References, namespace);
if(namespace !== undefined) {
schema = metadata._options.cache.getSchema(namespace);
}
else {
return null;
}
}
else if(schemas.length === 1) {
schema = schemas[0];
}
else {
for(let i = 0; i < schemas.length; i++) {
if(schemas[i][justType] !== undefined) {
return schemas[i][justType];
}
}
schema = findSchema(metadata, namespace, justType);
if(schema === undefined) {
return null;
}
}
return schema[justType];
}

function findSchema(metadata, namespace, justType) {
let schemas = metadata._options.cache.getSchemas(namespace);
if(schemas.length === 0) {
return findSchemaWithReferences(metadata, namespace, justType);
}
for(let i = 0; i < schemas.length; i++) {
if(schemas[i][justType] !== undefined) {
return schemas[i];
}
}
return undefined;
}

function findSchemaWithReferences(metadata, namespace, justType) {
if(metadata.References !== undefined) {
namespace = remapNamespace(metadata.References, namespace);
if(namespace !== undefined) {
return findSchema(metadata, namespace, justType);
}
}
return undefined;
}

module.exports.search = searchCSDL;
module.exports.findByType = findByType;
/* vim: set tabstop=2 shiftwidth=2 expandtab: */
14 changes: 6 additions & 8 deletions lib/Collection.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
const ParserCommon = require('./ParserCommon');

class Collection extends ParserCommon {
constructor(element) {
constructor() {
super();
this.PropertyPaths = [];
this.Records = [];
this.Strings = [];

this.validElements = {
'PropertyPath': {parent: this.PropertyPaths, getText: true},
'Record': {parent: this.Records},
'String': {parent: this.Strings, passthru: true}
};

this.init(element);
this.addElementHandler('PropertyPath', this.addStringToArray, {parent: this.PropertyPaths});
this.addElementHandler('String', this.addStringToArray, {parent: this.Strings});
this.addElementHandler('Record', this.addElementToArray, {parent: this.Records});
}

postInitCleanup() {
if(this.PropertyPaths.length === 0) {
delete this.PropertyPaths;
}
Expand Down
27 changes: 11 additions & 16 deletions lib/ComplexType.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
const ParserCommon = require('./ParserCommon');
const AnnotatedObject = require('./AnnotatedObject');

class ComplexType extends ParserCommon {
constructor(element) {
class ComplexType extends AnnotatedObject {
constructor() {
super();

this.Properties = {};
this.Annotations = {};

this.validElements = {
'Annotation': {parent: this.Annotations, nameProp: 'Term'},
'Property': {parent: this.Properties, nameProp: 'Name'},
'NavigationProperty': {parent: this.Properties, nameProp: 'Name'}
};
this.validAttributes = {
'Abstract': {bool: true},
'OpenType': {bool: true},
'BaseType': {},
'Name': {alreadyHandeled: true}
};
this.addElementHandler('Property', this.addElementToObj, {parent: this.Properties, nameProp: 'Name'});
this.addElementHandler('NavigationProperty', this.addElementToObj, {parent: this.Properties, nameProp: 'Name'});

this.init(element, 'Name');
this.addAttributeHandler('Abstract', this.addBoolAttributeToObj, {});
this.addAttributeHandler('OpenType', this.addBoolAttributeToObj, {});
this.addAttributeHandler('Name', null, {});
this.addAttributeHandler('BaseType', this.addAttributeToObj, {});

this.nameAttr = 'Name';
}
}

Expand Down
21 changes: 8 additions & 13 deletions lib/EntityContainer.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
const ParserCommon = require('./ParserCommon');

class EntityContainer extends ParserCommon {
constructor(element) {
constructor() {
super();
this.addElementHandler('EntitySet', this.addElementToObj, {nameProp: 'Name'});
this.addElementHandler('Singleton', this.addElementToObj, {nameProp: 'Name'});
this.addElementHandler('ActionImport', this.addElementToObj, {nameProp: 'Name'});
this.addElementHandler('FunctionImport', this.addElementToObj, {nameProp: 'Name'});

this.validElements = {
'EntitySet': {parent: this, nameProp: 'Name'},
'Singleton': {parent: this, nameProp: 'Name'},
'ActionImport': {parent: this, nameProp: 'Name'},
'FunctionImport': {parent: this, nameProp: 'Name'}
};
this.validAttributes = {
'Extends': {},
'Name': {alreadyHandeled: true}
};

this.init(element, 'Name');
this.addAttributeHandler('Name', null, {});
this.addAttributeHandler('Extends', this.addAttributeToObj, {});
this.nameAttr = 'Name';
}
}

Expand Down
Loading