generated from finos-labs/project-blueprint
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7703c31
commit 8d98914
Showing
4 changed files
with
224 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import { SchemaDirectory } from '../schema-directory'; | ||
import { instantiateAllMetadata, instantiateMetadataObject } from './metadata'; | ||
|
||
jest.mock('../../helper', () => { | ||
return { | ||
initLogger: () => { | ||
return { | ||
info: () => { }, | ||
debug: () => { } | ||
}; | ||
} | ||
}; | ||
}); | ||
|
||
jest.mock('../schema-directory'); | ||
|
||
let mockSchemaDir; | ||
|
||
beforeEach(() => { | ||
mockSchemaDir = new SchemaDirectory(); | ||
}); | ||
|
||
|
||
describe('instantiateMetadataObject', () => { | ||
it('instantiate metadata object with simple properties', () => { | ||
const metadataDef = { | ||
'type': 'object', | ||
'properties': { | ||
'string-prop': { | ||
'type': 'string' | ||
}, | ||
'integer-prop': { | ||
'type': 'integer' | ||
}, | ||
'const-prop': { | ||
'const': 'constant' | ||
} | ||
} | ||
}; | ||
expect(instantiateMetadataObject(metadataDef, mockSchemaDir, false, true)) | ||
.toEqual( | ||
{ | ||
'string-prop': '{{ STRING_PROP }}', | ||
'integer-prop': -1, | ||
'const-prop': 'constant' | ||
}, | ||
); | ||
}); | ||
|
||
it('instantiate metadata object with nested object properties', () => { | ||
const metadataDef = { | ||
'type': 'object', | ||
'properties': { | ||
'property-name': { | ||
'type': 'object', | ||
'properties': { | ||
'example': { | ||
'type': 'string' | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
expect(instantiateMetadataObject(metadataDef, mockSchemaDir, false, true)) | ||
.toEqual( | ||
{ | ||
'property-name': { | ||
'example': '{{ EXAMPLE }}' | ||
} | ||
}, | ||
); | ||
}); | ||
|
||
it('instantiate metadata object with $ref', () => { | ||
const reference = 'http://calm.com/example-ref'; | ||
const metadataDef = { | ||
'$ref': reference | ||
}; | ||
|
||
const returnedDef = { | ||
'type': 'object', | ||
'properties': { | ||
'property-name': { | ||
'type': 'object', | ||
'properties': { | ||
'example': { | ||
'type': 'string' | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const spy = jest.spyOn(mockSchemaDir, 'getDefinition'); | ||
spy.mockReturnValue(returnedDef); | ||
|
||
|
||
expect(instantiateMetadataObject(metadataDef, mockSchemaDir, false, true)) | ||
.toEqual( | ||
{ | ||
'property-name': { | ||
'example': '{{ EXAMPLE }}' | ||
} | ||
}, | ||
); | ||
expect(spy).toHaveBeenCalledWith(reference); | ||
}); | ||
}); | ||
|
||
function getSamplePatternWithMetadata(...metadataDefs): object { | ||
return { | ||
properties: { | ||
metadata: { | ||
type: 'array', | ||
prefixItems: [ | ||
...metadataDefs | ||
] | ||
} | ||
} | ||
}; | ||
} | ||
|
||
|
||
describe('instantiateAllMetadata', () => { | ||
it('instantiate simple metadata list with two objects', () => { | ||
const pattern = getSamplePatternWithMetadata({ | ||
'type': 'object', | ||
'properties': { | ||
'property-name': { | ||
'type': 'string' | ||
} | ||
} | ||
}, | ||
{ | ||
'type': 'object', | ||
'properties': { | ||
'property-name-2': { | ||
'type': 'integer' | ||
} | ||
} | ||
} | ||
); | ||
expect(instantiateAllMetadata(pattern, mockSchemaDir, false, true)) | ||
.toEqual( | ||
[ | ||
{ | ||
'property-name': '{{ PROPERTY_NAME }}' | ||
}, | ||
{ | ||
'property-name-2': -1 | ||
} | ||
] | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { initLogger } from '../../helper.js'; | ||
import { SchemaDirectory } from '../schema-directory.js'; | ||
import { logRequiredMessage, mergeSchemas } from '../util.js'; | ||
import { getPropertyValue } from './property.js'; | ||
|
||
export function instantiateMetadataObject(definition: object, schemaDirectory: SchemaDirectory, debug: boolean = false, instantiateAll: boolean = false): object { | ||
const logger = initLogger(debug); | ||
let fullDefinition = definition; | ||
if (definition['$ref']) { | ||
const ref = definition['$ref']; | ||
const schemaDef = schemaDirectory.getDefinition(ref); | ||
|
||
fullDefinition = mergeSchemas(schemaDef, definition); | ||
} | ||
logger.debug('Generating metadata object from ' + JSON.stringify(fullDefinition)); | ||
|
||
if (!('properties' in fullDefinition)) { | ||
return {}; | ||
} | ||
|
||
const required = fullDefinition['required']; | ||
logRequiredMessage(logger, required, instantiateAll); | ||
|
||
const out = {}; | ||
for (const [key, detail] of Object.entries(fullDefinition['properties'])) { | ||
if (!instantiateAll && required && !required.includes(key)) { | ||
logger.debug('Skipping property ' + key + ' as it is not marked as required.'); | ||
continue; | ||
} | ||
if (detail?.type == 'object') { | ||
// recursive instantiation | ||
logger.debug('Recursively instantiating a metadata object'); | ||
out[key] = instantiateMetadataObject(detail, schemaDirectory, instantiateAll, debug); | ||
} | ||
else { | ||
out[key] = getPropertyValue(key, detail); | ||
} | ||
} | ||
return out; | ||
} | ||
|
||
export function instantiateAllMetadata(pattern: object, schemaDirectory: SchemaDirectory, debug: boolean = false, instantiateAll: boolean = false): object[] { | ||
const logger = initLogger(debug); | ||
const metadataObjects = pattern['properties']?.metadata?.prefixItems; | ||
if (!metadataObjects) { | ||
logger.debug('Warning: pattern has no metadata fields defined, skipping instantiation.'); | ||
if (pattern['properties']?.metadata?.items) { | ||
logger.warn('Note: properties.metadata.items is deprecated: please use prefixItems instead.'); | ||
} | ||
return []; | ||
} | ||
const outputMetadata = []; | ||
|
||
for (const node of metadataObjects) { | ||
outputMetadata.push(instantiateMetadataObject(node, schemaDirectory, debug, instantiateAll)); | ||
} | ||
return outputMetadata; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters