Skip to content

Commit

Permalink
Introducing logical types
Browse files Browse the repository at this point in the history
  • Loading branch information
pdesmarets committed Nov 30, 2018
1 parent 97a677a commit 7891891
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 2 deletions.
40 changes: 40 additions & 0 deletions forward_engineering/api.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -70,6 +76,7 @@ const handleRecursiveSchema = (schema, avroSchema, parentSchema = {}, key) => {
handleSchemaName(avroSchema, parentSchema);
avroSchema = reorderName(avroSchema);
handleEmptyNestedObjects(avroSchema);
handleTargetProperties(schema, avroSchema);
return;
};

Expand Down Expand Up @@ -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;
};

Expand Down Expand Up @@ -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];
});
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Avro",
"version": "0.1.2",
"version": "0.1.3",
"versionDate": "2018-11-06",
"author": "hackolade",
"engines": {
Expand Down
108 changes: 108 additions & 0 deletions properties_pane/field_level/fieldLevelConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion types/number.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"foreignCollection": "",
"foreignField": [],
"enum": [],
"mode": "",
"mode": "int",
"sample": ""
}
}

0 comments on commit 7891891

Please sign in to comment.