diff --git a/forward_engineering/api.js b/forward_engineering/api.js index 4f513b3..e488ebf 100644 --- a/forward_engineering/api.js +++ b/forward_engineering/api.js @@ -1,10 +1,16 @@ 'use strict' +const fs = require('fs'); +const path = require('path'); const ADDITIONAL_PROPS = ['name', 'arrayItemName', 'doc', 'order', 'aliases', 'symbols', 'namespace', 'size', 'default']; const DEFAULT_TYPE = 'string'; const DEFAULT_NAME = 'New_field'; const VALID_FULL_NAME_REGEX = /[^A-Za-z0-9_]/g; const VALID_FIRST_NAME_LETTER_REGEX = /^[0-9]/; +const readConfig = (pathToConfig) => { + return JSON.parse(fs.readFileSync(path.join(__dirname, pathToConfig)).toString().replace(/\/\*[.\s\S]*?\*\//ig, "")); +}; +const fieldLevelConfig = readConfig('../properties_pane/field_level/fieldLevelConfig.json'); let nameIndex = 0; module.exports = { @@ -70,6 +76,7 @@ const handleRecursiveSchema = (schema, avroSchema, parentSchema = {}, key) => { handleSchemaName(avroSchema, parentSchema); avroSchema = reorderName(avroSchema); handleEmptyNestedObjects(avroSchema); + handleTargetProperties(schema, avroSchema); return; }; @@ -232,6 +239,12 @@ const handleSchemaName = (avroSchema, parentSchema) => { avroSchema.name = avroSchema.name.replace(VALID_FULL_NAME_REGEX, '_') .replace(VALID_FIRST_NAME_LETTER_REGEX, '_'); } + + if(avroSchema.type && avroSchema.type.name) { + avroSchema.type.name = avroSchema.type.name.replace(VALID_FULL_NAME_REGEX, '_') + .replace(VALID_FIRST_NAME_LETTER_REGEX, '_'); + } + delete avroSchema.arrayItemName; }; @@ -269,4 +282,31 @@ const handleEmptyNestedObjects = (avroSchema) => { } else if (avroSchema.type && avroSchema.type === 'array') { avroSchema.items = (avroSchema.items) ? avroSchema.items : DEFAULT_TYPE; } +}; + +const getTargetFieldLevelPropertyNames = (type, data) => { + if (!fieldLevelConfig.structure[type]) { + return []; + } + + return fieldLevelConfig.structure[type].filter(property => { + if (typeof property === 'object' && property.isTargetProperty) { + if (property.dependency) { + return (data[property.dependency.key] == property.dependency.value); + } else { + return true; + } + } + + return false; + }).map(property => property.propertyKeyword); +}; + +const handleTargetProperties = (schema, avroSchema) => { + if (schema.type) { + const targetProperties = getTargetFieldLevelPropertyNames(schema.type, schema); + targetProperties.forEach(prop => { + avroSchema[prop] = schema[prop]; + }); + } }; \ No newline at end of file diff --git a/package.json b/package.json index cf33c1f..c37f24e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "Avro", - "version": "0.1.2", + "version": "0.1.3", "versionDate": "2018-11-06", "author": "hackolade", "engines": { diff --git a/properties_pane/field_level/fieldLevelConfig.json b/properties_pane/field_level/fieldLevelConfig.json index 36693b7..72ba8ca 100644 --- a/properties_pane/field_level/fieldLevelConfig.json +++ b/properties_pane/field_level/fieldLevelConfig.json @@ -155,6 +155,43 @@ making sure that you maintain a proper JSON format. "name", "schemaId", "type", + { + "propertyName": "Logical type", + "propertyKeyword": "logicalType", + "shouldValidate": false, + "propertyType": "select", + "options": [ + "", + "decimal" + ], + "isTargetProperty": true + }, + { + "propertyName": "Precision", + "propertyKeyword": "precision", + "propertyValidate": true, + "propertyType": "numeric", + "valueType": "number", + "allowNegative": false, + "dependency": { + "key": "logicalType", + "value": "decimal" + }, + "isTargetProperty": true + }, + { + "propertyName": "Scale", + "propertyKeyword": "scale", + "propertyValidate": true, + "propertyType": "numeric", + "valueType": "number", + "allowNegative": false, + "dependency": { + "key": "logicalType", + "value": "decimal" + }, + "isTargetProperty": true + }, { "propertyName": "Doc", "propertyKeyword": "doc", @@ -203,6 +240,39 @@ making sure that you maintain a proper JSON format. "data": "options", "valueType": "string" }, + { + "propertyName": "Logical type", + "propertyKeyword": "logicalType", + "shouldValidate": false, + "propertyType": "select", + "options": [ + "", + "date", + "time-millis" + ], + "dependency": { + "key": "mode", + "value": "int" + }, + "isTargetProperty": true + }, + { + "propertyName": "Logical type", + "propertyKeyword": "logicalType", + "shouldValidate": false, + "propertyType": "select", + "options": [ + "", + "time-micros", + "timestamp-millis", + "timestamp-micros" + ], + "dependency": { + "key": "mode", + "value": "long" + }, + "isTargetProperty": true + }, { "propertyName": "Doc", "propertyKeyword": "doc", @@ -558,6 +628,44 @@ making sure that you maintain a proper JSON format. "name", "schemaId", "type", + { + "propertyName": "Logical type", + "propertyKeyword": "logicalType", + "shouldValidate": false, + "propertyType": "select", + "options": [ + "", + "decimal", + "duration" + ], + "isTargetProperty": true + }, + { + "propertyName": "Precision", + "propertyKeyword": "precision", + "propertyValidate": true, + "propertyType": "numeric", + "valueType": "number", + "allowNegative": false, + "dependency": { + "key": "logicalType", + "value": "decimal" + }, + "isTargetProperty": true + }, + { + "propertyName": "Scale", + "propertyKeyword": "scale", + "propertyValidate": true, + "propertyType": "numeric", + "valueType": "number", + "allowNegative": false, + "dependency": { + "key": "logicalType", + "value": "decimal" + }, + "isTargetProperty": true + }, { "propertyName": "Namespace", "propertyKeyword": "namespace", diff --git a/types/number.json b/types/number.json index 222de1e..da47b8b 100644 --- a/types/number.json +++ b/types/number.json @@ -22,7 +22,7 @@ "foreignCollection": "", "foreignField": [], "enum": [], - "mode": "", + "mode": "int", "sample": "" } }