Skip to content

Commit

Permalink
Java Metavalue
Browse files Browse the repository at this point in the history
  • Loading branch information
pdesmarets committed Dec 19, 2019
1 parent a68ca3c commit 95101a2
Show file tree
Hide file tree
Showing 14 changed files with 862 additions and 30 deletions.
83 changes: 83 additions & 0 deletions adapter/0.1.35.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Copyright © 2016-2019 by IntegrIT S.A. dba Hackolade. All rights reserved.
*
* The copyright to the computer software herein is the property of IntegrIT S.A.
* The software may be used and/or copied only with the written permission of
* IntegrIT S.A. or in accordance with the terms and conditions stipulated in
* the agreement/contract under which the software has been supplied.
*
* {
* "add": {
* "entity": [<names of new property>],
* "container": [<names of new property>],
* "model": [<names of new property>],
* "view": [<names of new property>],
* "field": {
* "<type>": [<names of new property>]
* }
* },
* "delete": {
* "entity": [<names of new property>],
* "container": [<names of new property>],
* "model": [<names of new property>],
* "view": [<names of new property>],
* "field": {
* "<type>": [<names of new property>]
* }
* },
* "modify": {
* "entity": [
* {
* "from": { <properties that identify record> },
* "to": { <properties that need to be changed> }
* }
* ],
* "container": [],
* "model": [],
* "view": [],
* "field": []
* },
* }
*/
{
"add": {
},
"modify": {
"field": [
{
"from": {
"type": "string",
"avro.java.string": "String"
},
"to": {
"metaProps": [{
"metaKey": "avro.java.string",
"metaValueString": "String"
}]
}
}
],
"multipleTypes": [
{
"from": {
"type": "string",
"avro.java.string": "String"
},
"to": {
"metaProps": [{
"metaKey": "avro.java.string",
"metaValueString": "String"
}]
}
}
]
},
"delete": {
"field": [
"avro.java.string"
],
"multipleTypes": [
"avro.java.string"
]
}
}
86 changes: 76 additions & 10 deletions forward_engineering/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const _ = require('lodash');
const validationHelper = require('./validationHelper');
const mapJsonSchema = require('../reverse_engineering/helpers/mapJsonSchema');

const ADDITIONAL_PROPS = ['doc', 'order', 'aliases', 'avro.java.string', 'symbols', 'namespace', 'size', 'durationSize', 'default', 'precision', 'scale'];
const ADDITIONAL_PROPS = ['doc', 'order', 'aliases', 'symbols', 'namespace', 'size', 'durationSize', 'default', 'precision', 'scale'];
const ADDITIONAL_CHOICE_META_PROPS = ADDITIONAL_PROPS.concat('index');
const PRIMITIVE_FIELD_ATTRIBUTES = ['order', 'logicalType', 'precision', 'scale', 'aliases'];
const DEFAULT_TYPE = 'string';
Expand Down Expand Up @@ -194,6 +194,8 @@ const handleRecursiveSchema = (schema, avroSchema, parentSchema = {}, udt) => {

handleRequired(parentSchema, avroSchema, schema);

addMetaPropertiesToType(avroSchema, schema);

return;
};

Expand Down Expand Up @@ -315,13 +317,18 @@ const handleChoice = (schema, choice, udt) => {
multipleField.type = [multipleField.type];
}

let newField = {};

handleRecursiveSchema(field, newField, {}, udt);

if (isComplexType(filedType)) {
let newField = {};
handleRecursiveSchema(field, newField, {}, udt);
newField.name = newField.name || field.name || fieldName;
newField.type.name = newField.type.name || field.name || fieldName;
newField.type = reorderName(newField.type);
multipleField.type.push(newField);
} else if (Object(newField.type) === newField.type) {
newField.name = newField.name || field.name || fieldName;
multipleField.type = multipleField.type.concat([newField]);
} else if (Array.isArray(filedType)) {
multipleField.type = multipleField.type.concat(filedType);
} else {
Expand Down Expand Up @@ -621,10 +628,10 @@ const handleItems = (schema, avroSchema, udt) => {
schemaItem.type = schemaItem.type || getTypeFromReference(schemaItem);

handleRecursiveSchema(schemaItem, avroSchema.items, avroSchema, udt);
}

if (avroSchema.items.type && typeof avroSchema.items.type === 'object') {
avroSchema.items = avroSchema.items.type;
}
if (avroSchema.items.type && typeof avroSchema.items.type === 'object') {
avroSchema.items = Object.assign({}, avroSchema.items, avroSchema.items.type);
}

if (schemaItemName) {
Expand All @@ -646,10 +653,6 @@ const handleDefault = (schema, avroSchema) => {
};

const handleOtherProps = (schema, prop, avroSchema) => {
if (!ADDITIONAL_PROPS.includes(prop)) {
return;
}

const allowedProperties = getAllowedPropertyNames(schema.type, schema);
if (!allowedProperties.includes(prop)) {
return;
Expand Down Expand Up @@ -785,8 +788,21 @@ const getAllowedPropertyNames = (type, data) => {
if (!fieldLevelConfig.structure[type]) {
return [];
}
const isAllowed = (property) => {
if (typeof property === 'string') {
return ADDITIONAL_PROPS.includes(property)
} else if (Object(property) === property) {
return ADDITIONAL_PROPS.includes(property.propertyKeyword) || property.isTargetProperty;
} else {
return false;
}
};

return fieldLevelConfig.structure[type].filter(property => {
if (!isAllowed(property)) {
return false;
}

if (typeof property !== 'object') {
return true;
}
Expand Down Expand Up @@ -916,3 +932,53 @@ const mapAvroSchema = (avroSchema, iteratee) => {

return avroSchema;
};

const getMetaProperties = (metaProperties) => {
const metaValueKeyMap = {
'avro.java.string': 'metaValueString',
'java-element': 'metaValueElement',
'java-element-class': 'metaValueElementClass',
'java-class': 'metaValueClass',
'java-key-class': 'metaValueKeyClass'
};

return metaProperties.reduce((props, property) => {
const metaValueKey = _.get(metaValueKeyMap, property.metaKey, 'metaValue');

return Object.assign(props, { [property.metaKey]: property[metaValueKey] });
}, {});
};

const getTypeWithMeta = (type, meta) => {
if (typeof type !== 'string') {
return Object.assign({}, type, meta);
} else {
return Object.assign({ type }, meta);
}
};

const getMultipleTypeWithMeta = (types, meta) => {
return types.map(type => {
if (type === 'null') {
return type;
}

return getTypeWithMeta(type, meta);
});
};

const addMetaPropertiesToType = (avroSchema, jsonSchema) => {
if (!Array.isArray(jsonSchema.metaProps) || !jsonSchema.metaProps.length) {
return avroSchema;
}

const meta = getMetaProperties(jsonSchema.metaProps);

if (Array.isArray(avroSchema.type)) {
avroSchema.type = getMultipleTypeWithMeta(avroSchema.type, meta);
} else {
avroSchema.type = getTypeWithMeta(avroSchema.type, meta);
}

return avroSchema;
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Avro",
"version": "0.1.34",
"versionDate": "2019-12-04",
"version": "0.1.35",
"versionDate": "2019-12-19",
"author": "hackolade",
"engines": {
"hackolade": "3.3.2",
Expand Down
Loading

0 comments on commit 95101a2

Please sign in to comment.