Skip to content

Commit

Permalink
Merge pull request #85 from mtseluiko/HCK-2750-avro-for-confluent-inf…
Browse files Browse the repository at this point in the history
…er-subject-name-strategy-fr

Hck 2750 avro for confluent infer subject name strategy fr
  • Loading branch information
mtseluiko authored Dec 15, 2022
2 parents 843a886 + 6c8ab5d commit e5e2f96
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion reverse_engineering/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { setDependencies, dependencies } = require('../shared/appDependencies');
const jsonSchemaAdapter = require('./helpers/adaptJsonSchema');
const convertToJsonSchemas = require('./helpers/convertToJsonSchemas');
const { openAvroFile, getExtension } = require('./helpers/fileHelper');
const { openAvroFile } = require('./helpers/fileHelper');
const { getNamespace } = require('./helpers/generalHelper');

let _;
Expand Down Expand Up @@ -55,6 +55,12 @@ const getPackages = (avroSchema, jsonSchemas) => {

return jsonSchemas.map((jsonSchema, index) => {
const { namespace, schemaType, schemaGroupName, confluentSubjectName, schemaTopic } = schemasData[index] || {};
const schemaNameStrategy = inferSchemaNameStrategy({
name: jsonSchema.title,
namespace,
confluentSubjectName,
schemaTopic,
});

return {
objectNames: {
Expand All @@ -73,11 +79,43 @@ const getPackages = (avroSchema, jsonSchemas) => {
schemaTopic: schemaTopic,
schemaGroupName: schemaGroupName,
confluentSubjectName: confluentSubjectName,
...(schemaNameStrategy && { schemaNameStrategy }),
}),
};
});
};

const inferSchemaNameStrategy = ({ name, namespace, confluentSubjectName, schemaTopic }) => {
let splittedSubjectName = (confluentSubjectName || '').split('-').filter(Boolean);
const endsWithSchemaType = ['key', 'value'].includes(_.last(splittedSubjectName));
const startsWithTopic = _.first(splittedSubjectName) === schemaTopic;

if (endsWithSchemaType) {
splittedSubjectName = splittedSubjectName.slice(0, -1);
}

if (startsWithTopic) {
splittedSubjectName = splittedSubjectName.slice(1);
}

if (startsWithTopic && _.isEmpty(splittedSubjectName)) {
return 'TopicNameStrategy';
}

const splittedRecordName = [
namespace,
...(name || '').split('-'),
].filter(Boolean);

const recordNameStrategy = _.isEqual(splittedRecordName, splittedSubjectName);

if (!recordNameStrategy) {
return;
}

return startsWithTopic ? 'TopicRecordNameStrategy' : 'RecordNameStrategy';
};

const getSchemasData = avroSchema => {
avroSchema = _.isArray(avroSchema) ? avroSchema : [ avroSchema ];

Expand Down

0 comments on commit e5e2f96

Please sign in to comment.