Skip to content

Commit

Permalink
Add strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
Kostya committed May 7, 2021
1 parent 3f78352 commit 796d0e1
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 5 deletions.
34 changes: 31 additions & 3 deletions forward_engineering/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const LOGICAL_TYPES_MAP = {
fixed: ['decimal', 'duration']
};

const RecordNameStrategy = 'RecordNameStrategy';
const TopicRecordNameStrategy = 'TopicRecordNameStrategy';
module.exports = {
generateModelScript(data, logger, cb) {
logger.clear();
Expand Down Expand Up @@ -107,8 +109,9 @@ const getCommonEntitiesData = (data) => {
},
additionalOptions: data.options.additionalOptions
};
const modelData = _.get(data, 'modelData[0]', {})

return { options, modelDefinitions, externalDefinitions }
return { options, modelDefinitions, externalDefinitions, modelData }
}

const getEntityData = (container, entityId) => {
Expand All @@ -121,6 +124,31 @@ const getEntityData = (container, entityId) => {
return { containerData, jsonSchema, jsonData, entityData, internalDefinitions }
}

const getConfluentPostQuery = ({ data, schema }) => {
const getName = ()=>{
const name = getRecordName(data);
const typePostfix = _.has(data, 'entityData.schemaType') ? `-${data.entityData.schemaType}` : '';
const containerPrefix = _.has(data, 'containerData.name') ? `${data.containerData.name}.`:'';
const topicPrefix = _.has(data, 'modelData.schemaTopic') ? `${data.modelData.schemaTopic}-`:'';

const schemaNameStrategy = _.get(data, 'modelData.schemaNameStrategy', '');
switch(schemaNameStrategy){
case RecordNameStrategy:
return `${containerPrefix}${name}${typePostfix}`
case TopicRecordNameStrategy:
return `${topicPrefix}${containerPrefix}${name}${typePostfix}`
default:
return `${name}${typePostfix}`;
}
}

return `POST /subjects/${getName()}/versions\n${JSON.stringify(
{ schema, schemaType: "AVRO" },
null,
4
)}`;
};

const getScript = (data) => {
const name = getRecordName(data);
let avroSchema = { name };
Expand All @@ -146,9 +174,9 @@ const getScript = (data) => {

const needMinify = (additionalOptions.find(option => option.id === 'minify') || {}).value;
if (targetScriptType === 'confluentSchemaRegistry') {
const schema = needMinify?JSON.stringify(avroSchema):avroSchema;
const schema = needMinify ? JSON.stringify(avroSchema) : avroSchema;

return `POST /subjects/${name}/versions\n${JSON.stringify({ schema, schemaType: "AVRO" }, null, 4)}`
return getConfluentPostQuery({ data, schema});
}

if (needMinify) {
Expand Down
3 changes: 2 additions & 1 deletion properties_pane/defaultData.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"model": {
"modelName": "New model",
"dbVersion": "",
"dbVendor": "Avro"
"dbVendor": "Avro",
"schemaNameStrategy":"TopicNameStrategy"
},
"container": {
"name": "New_namespace",
Expand Down
11 changes: 11 additions & 0 deletions properties_pane/entity_level/entityLevelConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ making sure that you maintain a proper JSON format.
"propertyTooltip": "Namespace",
"propertyType": "selecthashed"
},
{
"propertyName": "Confluent schema type",
"propertyKeyword": "schemaType",
"propertyTooltip": "Select from list of options",
"propertyType": "select",
"options": [
"",
"value",
"key"
]
},
{
"propertyName": "Additional properties",
"propertyKeyword": "additionalProperties",
Expand Down
22 changes: 22 additions & 0 deletions properties_pane/model_level/modelLevelConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,28 @@ making sure that you maintain a proper JSON format.
],
"disabledOption": true
},
{
"propertyName": "Subject Name Strategy",
"propertyKeyword": "schemaNameStrategy",
"propertyTooltip": "Select from list of options",
"propertyType": "select",
"options": [
"TopicNameStrategy",
"RecordNameStrategy",
"TopicRecordNameStrategy"
]
},
{
"propertyName": "Topic",
"propertyKeyword": "schemaTopic",
"shouldValidate": false,
"propertyTooltip": "",
"propertyType": "text",
"dependency": {
"key": "schemaNameStrategy",
"value": "TopicRecordNameStrategy"
}
},
{
"propertyName": "Comments",
"propertyKeyword": "comments",
Expand Down
2 changes: 1 addition & 1 deletion reverse_engineering/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const jsonSchemaAdapter = require('./helpers/adaptJsonSchema');
const DEFAULT_FIELD_NAME = 'New_field';
let stateExtension = null;

const ADDITIONAL_PROPS = ['logicalType', 'scale', 'precision', 'name', 'arrayItemName', 'doc', 'order', 'aliases', 'symbols', 'namespace', 'size', 'default', 'pattern', 'choice'];
const ADDITIONAL_PROPS = ['schemaType', 'logicalType', 'scale', 'precision', 'name', 'arrayItemName', 'doc', 'order', 'aliases', 'symbols', 'namespace', 'size', 'default', 'pattern', 'choice'];
const DATA_TYPES = [
'string',
'bytes',
Expand Down

0 comments on commit 796d0e1

Please sign in to comment.