Skip to content

Commit

Permalink
Type mapping for multiple data type fields in FE and RE
Browse files Browse the repository at this point in the history
  • Loading branch information
pdesmarets committed Sep 16, 2019
1 parent cfd553d commit 9c32ec5
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 20 deletions.
2 changes: 1 addition & 1 deletion jsonSchemaProperties.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"unneededFieldProps": ["collectionName", "name", "users", "indexes", "collectionUsers", "additionalProperties"],
"unneededFieldProps": ["collectionName", "name", "users", "indexes", "collectionUsers", "logicalType", "mode"],
"removeIfPropsNegative": ["partitionKey", "sortKey"]
}
73 changes: 70 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Avro",
"version": "0.1.25",
"versionDate": "2019-09-10",
"version": "0.1.26",
"versionDate": "2019-09-16",
"author": "hackolade",
"engines": {
"hackolade": "3.3.2",
Expand All @@ -18,7 +18,74 @@
"enableReverseEngineering": true,
"enableJsonType": false,
"enableArrayItemName": true,
"validateMultipleDefaultByFirstType": true
"validateMultipleDefaultByFirstType": true,
"forwardEngineering": {
"jsonSchema": {
"keepParentType": {
"bytes": {
"dependencies": [{
"dependency": {
"key": "logicalType",
"value": "decimal"
},
"value": "number"
}],
"defaultValue": "string"
},
"number": {
"dependencies": [
{
"dependency": {
"type": "and",
"values": [{
"key": "mode",
"value": "int"
}, {
"key": "logicalType",
"value": "date"
}]
},
"value": {
"type": "string",
"format": "date"
}
},{
"dependency": {
"type": "and",
"values": [{
"key": "mode",
"value": "int"
},{
"key": "logicalType",
"value": "time-millis"
}]
},
"value": {
"type": "string",
"format": "time"
}
},{
"dependency": {
"type": "and",
"values":[{
"key": "mode",
"value": "long"
},{
"key": "logicalType",
"value": "timestamp-millis"
}]
},
"value": {
"type": "string",
"format": "date-time"
}
}
],
"defaultValue": "integer"
}
}
}
}
}
},
"description": "Hackolade plugin for Apache Avro Schema",
Expand Down
2 changes: 0 additions & 2 deletions properties_pane/field_level/fieldLevelConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ making sure that you maintain a proper JSON format.
"propertyName": "Default",
"propertyKeyword": "default",
"shouldValidate": true,
"nullable": true,
"propertyType": "text",
"validation": {
"enum": [null, "null", ""]
Expand Down Expand Up @@ -773,7 +772,6 @@ making sure that you maintain a proper JSON format.
"propertyType": "details",
"template": "textarea",
"markdown": false,
"nullable": true,
"dependency": [{
"key": "required",
"value": false
Expand Down
35 changes: 22 additions & 13 deletions reverse_engineering/helpers/adaptJsonSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,23 @@ const handleStringFormat = field => {
};

const adaptMultiple = field => {
const { fieldData, types } = field.type.reduce(({ fieldData, types }, type) => {
const { fieldData, types } = field.type.reduce(({ fieldData, types }, type, index) => {
const typeField = Object.assign({}, fieldData, { type });
const updatedData = adaptType(typeField);
const updatedTypes = types.map(initialType => {
if (initialType === type) {
return updatedData.type;
}
return initialType;
});
types[index] = updatedData.type;

return {
fieldData: updatedData,
types: _.uniq(updatedTypes)
types
};
}, { fieldData: field, types: field.type });

if (types.length === 1) {
const uniqTypes = _.uniq(types);
if (uniqTypes.length === 1) {
return fieldData;
}

return Object.assign({}, fieldData, {type: types});
return Object.assign({}, fieldData, {type: uniqTypes});
};

const handleEmptyDefault = field => {
Expand Down Expand Up @@ -150,11 +146,24 @@ const adaptType = field => {
return field;
};

const populateDefaultNullValuesForMultiple = field => {
if (!_.isArray(field.type)) {
return field;
}
if (_.first(field.type) !== 'null') {
return field;
}

return Object.assign({}, field, { default: null });
};

const adaptJsonSchema = jsonSchema => {
return mapJsonSchema(jsonSchema, jsonSchemaItem => {
const handledTypesField = adaptType(jsonSchemaItem);

return handleEmptyDefaultInProperties(handledTypesField);
return _.flow([
adaptType,
populateDefaultNullValuesForMultiple,
handleEmptyDefaultInProperties
])(jsonSchemaItem);
});
};

Expand Down
2 changes: 1 addition & 1 deletion types/null.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"dtdAbbreviation": "{null}",
"useSample": false,
"defaultValues": {
"default": "null",
"default": null,
"type": "null",
"primaryKey": false,
"relationshipType": "",
Expand Down

0 comments on commit 9c32ec5

Please sign in to comment.