Skip to content

Commit

Permalink
v0.1.11 additional properties
Browse files Browse the repository at this point in the history
  • Loading branch information
pdesmarets committed Oct 9, 2018
1 parent 7068091 commit 1c37c8f
Show file tree
Hide file tree
Showing 10 changed files with 494 additions and 56 deletions.
60 changes: 49 additions & 11 deletions forward_engineering/api.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
const helper = require('../helper/helper.js');
const schemaHelper = require('../helper/schemaHelper.js');

module.exports = {
generateScript(data, logger, cb) {
const { jsonSchema, modelData, containerData, entityData, isUpdateScript } = data;
let result = "";
let fieldsSchema = this.getFieldsSchema(JSON.parse(jsonSchema));
let fieldsSchema = this.getFieldsSchema({
jsonSchema: JSON.parse(jsonSchema),
internalDefinitions: JSON.parse(data.internalDefinitions),
modelDefinitions: JSON.parse(data.modelDefinitions),
externalDefinitions: JSON.parse(data.externalDefinitions)
});
let typeSchema = this.getTypeSchema(entityData, fieldsSchema);
let mappingScript = this.getMappingScript(containerData, typeSchema);

Expand All @@ -31,31 +37,34 @@ module.exports = {
return `PUT /${indexName.toLowerCase()}\n${JSON.stringify(mapping, null, 4)}`;
},

getFieldsSchema(jsonSchema) {
getFieldsSchema(data) {
const {
jsonSchema
} = data;
let schema = {};

if (!(jsonSchema.properties && jsonSchema.properties._source && jsonSchema.properties._source.properties)) {
return schema;
}

schema = this.getSchemaByItem(jsonSchema.properties._source.properties)
schema = this.getSchemaByItem(jsonSchema.properties._source.properties, data)

return schema;
},

getSchemaByItem(properties) {
getSchemaByItem(properties, data) {
let schema = {};

for (let fieldName in properties) {
let field = properties[fieldName];

schema[fieldName] = this.getField(field);
schema[fieldName] = this.getField(field, data);
}

return schema;
},

getField(field) {
getField(field, data) {
let schema = {};
const fieldProperties = helper.getFieldProperties(field.type, field, {});
let type = this.getFieldType(field);
Expand All @@ -68,20 +77,20 @@ module.exports = {
schema.properties = {};
}

this.setProperties(schema, fieldProperties);
this.setProperties(schema, fieldProperties, data);

if (type === 'geo_shape' || type === 'geo_point') {
return schema;
} else if (field.properties) {
schema.properties = this.getSchemaByItem(field.properties);
schema.properties = this.getSchemaByItem(field.properties, data);
} else if (field.items) {
let arrData = field.items;

if (Array.isArray(field.items)) {
arrData = field.items[0];
}

schema = Object.assign(schema, this.getField(arrData));
schema = Object.assign(schema, this.getField(arrData, data));
}

return schema;
Expand All @@ -106,13 +115,26 @@ module.exports = {
}
},

setProperties(schema, properties) {
setProperties(schema, properties, data) {
for (let propName in properties) {
if (propName === 'stringfields') {
try {
schema['fields'] = JSON.parse(properties[propName]);
} catch (e) {
}
} else if (this.isFieldList(properties[propName])) {
const names = schemaHelper.getNamesByIds(
properties[propName].map(item => item.keyId),
[
data.jsonSchema,
data.internalDefinitions,
data.modelDefinitions,
data.externalDefinitions
]
);
if (names.length) {
schema[propName] = names.length === 1 ? names[0] : names;
}
} else {
schema[propName] = properties[propName];
}
Expand Down Expand Up @@ -203,5 +225,21 @@ module.exports = {
});

return aliases;
},

isFieldList(property) {
if (!Array.isArray(property)) {
return false;
}

if (!property[0]) {
return false;
}

if (property[0].keyId) {
return true;
}

return false;
}
};
};
2 changes: 1 addition & 1 deletion helper/helper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs');
const path = require('path');
const readConfig = (pathToConfig) => {
return JSON.parse(fs.readFileSync(path.join(__dirname, pathToConfig)).toString().replace(/\/\*[.\s\S]*\*\//ig, ""));
return JSON.parse(fs.readFileSync(path.join(__dirname, pathToConfig)).toString().replace(/\/\*[.\s\S]*?\*\//ig, ""));
};
const fieldLevelConfig = readConfig('../properties_pane/field_level/fieldLevelConfig.json');
const containerLevelConfig = readConfig('../properties_pane/container_level/containerLevelConfig.json');
Expand Down
107 changes: 107 additions & 0 deletions helper/schemaHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
'use strict'

const getPathById = (schema, id, path) => {
if (schema.GUID === id) {
return path;
}

if (schema.properties) {
return Object.keys(schema.properties).reduce((newPath, propertyName) => {
if (newPath) {
return newPath;
} else {
return getPathById(schema.properties[propertyName], id, [...path, schema.properties[propertyName].GUID]);
}
}, undefined);
} else if (schema.items) {
if (Array.isArray(schema.items)) {
return schema.items.reduce((newPath, item) => {
if (newPath) {
return newPath;
} else {
return getPathById(item, id, [...path, item.GUID]);
}
}, undefined);
} else {
return getPathById(schema.items, id, [...path, schema.items.GUID]);
}
}
};

const getNameByPath = (schema, path, parentName) => {
if (schema.properties) {
return Object.keys(schema.properties).reduce((foundedName, propertyName) => {
if (foundedName !== "") {
return foundedName;
}

const property = schema.properties[propertyName];

if (property.GUID !== path[0]) {
return foundedName;
}

if (path.length === 1) {
return propertyName;
}

return getNameByPath(property, path.slice(1), propertyName);
}, "");
} else if (Array.isArray(schema.items)) {
return schema.items.reduce((foundedName, property, i) => {
if (foundedName !== "") {
return foundedName;
}

if (property.GUID !== path[0]) {
return foundedName;
}

if (path.length === 1) {
return parentName + '[' + i + ']';
}

return getNameByPath(property, path.slice(1), parentName + '[' + i + ']');
}, "");
} else if (Object(schema.items) === schema.items) {
const property = schema.items;

if (property.GUID !== path[0]) {
return "";
}

if (path.length === 1) {
return parentName + '[0]';
}

return getNameByPath(property, path.slice(1), parentName + '[0]');
}
};

const findFieldNameById = (id, source) => {
let path = getPathById(source, id, []);

if (path) {
return getNameByPath(source, path, "");
} else {
return "";
}
};

const getNamesByIds = (ids, sources) => {
return ids.reduce((names, id) => {
for (let i = 0; i < sources.length; i++) {
const name = findFieldNameById(id, sources[i]);

if (name) {
return [...names, name];
}
}

return names;
}, []);
};

module.exports = {
getNamesByIds
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Elasticsearch",
"version": "0.1.10",
"versionDate": "2018-09-27",
"version": "0.1.11",
"versionDate": "2018-10-09",
"author": "hackolade",
"engines": {
"hackolade": "1.12.7",
Expand Down
3 changes: 2 additions & 1 deletion properties_pane/defaultData.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"dynamic": "true"
},
"field": {
"name": "New field"
"name": "New field",
"index_options": "positions"
},
"patternField": {
"name": "^New Pattern Field$"
Expand Down
8 changes: 7 additions & 1 deletion properties_pane/entity_level/entityLevelConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ making sure that you maintain a proper JSON format.
"lowerTab": "Details",
"structure": [
{
"propertyName": "Dynamic",
"propertyName": "dynamic",
"propertyKeyword": "dynamic",
"shouldValidate": false,
"propertyTooltip": "dynamic setting value",
Expand All @@ -96,6 +96,12 @@ making sure that you maintain a proper JSON format.
"strict"
]
},
{
"propertyName": "enabled",
"propertyKeyword": "enabled",
"shouldValidate": false,
"propertyType": "checkbox"
},
{
"propertyName": "Comments",
"propertyKeyword": "comments",
Expand Down
Loading

0 comments on commit 1c37c8f

Please sign in to comment.