Skip to content

Commit

Permalink
fix: handle enum
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Aaqil <[email protected]>
  • Loading branch information
aaqilniz authored and samarpanB committed Nov 21, 2023
1 parent 368b5b6 commit f664a76
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/cli/generators/discover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,25 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
templateData.settings,
);
}

Object.keys(templateData.properties).forEach(key => {
const property = templateData.properties[key];
// if the type is enum
if (property.type.startsWith(`'enum`)) {
property.type = property.type.slice(1, -1);
const enumRemoved = property.type.split(`enum`)[1];
const enumValues = enumRemoved.slice(1, -1).replaceAll(`'`, '');
const enumValuesArray = enumValues.split(',');
let enumItems = '';
enumValuesArray.forEach(item => {
enumItems += `'${item}',`;
});
templateData.properties[key]['type'] = 'String';
templateData.properties[key]['tsType'] = 'string';
templateData.properties[key][
'jsonSchema'
] = `{enum: [${enumItems.toString()}]}`;
}
});
this.copyTemplatedFiles(
modelDiscoverer.MODEL_TEMPLATE_PATH,
fullPath,
Expand Down
23 changes: 23 additions & 0 deletions packages/cli/test/unit/discover/import-discovered-model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,29 @@ describe('importDiscoveredModel', () => {
});
});

it('imports enum properties', () => {
const discoveredModel = {
name: 'TestModel',
properties: {
patient: {
type: 'String',
jsonSchema: {enum: ['INPATIENT', 'OUTPATIENT']},
required: false,
length: null,
precision: null,
scale: null,
},
},
};

const modelData = importDiscoveredModel(discoveredModel);
expect(modelData.properties).to.have.property('patient').deepEqual({
jsonSchema: "{enum: ['INPATIENT', 'OUTPATIENT']}",
type: `'string'`,
tsType: 'string',
});
});

it('converts connector metadata to TypeScript object', () => {
const discoveredModel = {
name: 'TestModel',
Expand Down

0 comments on commit f664a76

Please sign in to comment.