Skip to content

Commit

Permalink
finos#486 initial unification
Browse files Browse the repository at this point in the history
  • Loading branch information
willosborne authored and Will Osborne committed Nov 14, 2024
1 parent 1e04ea8 commit 0bb10f8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 39 deletions.
13 changes: 8 additions & 5 deletions shared/src/commands/generate/components/instantiate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Logger } from "winston";
import { initLogger } from "../../helper";
import { SchemaDirectory } from "../schema-directory";
import { logRequiredMessage, mergeSchemas } from "../util";
import { getPropertyValue } from "./property";
import { initLogger } from "../../helper.js";
import { SchemaDirectory } from "../schema-directory.js";
import { logRequiredMessage, mergeSchemas } from "../util.js";
import { getPropertyValue } from "./property.js";

export function instantiateGenericObject(definition: object, schemaDirectory: SchemaDirectory, objectType: string, debug: boolean = false, instantiateAll: boolean = false): object {
const logger = initLogger(debug);
Expand All @@ -24,10 +24,13 @@ export function instantiateGenericObject(definition: object, schemaDirectory: Sc

const out = {};
for (const [key, detail] of Object.entries(fullDefinition['properties'])) {
if (!instantiateAll && required && !required.includes(key)) {
if (!instantiateAll && required && !required.includes(key)) { // TODO should we always do interfaces even if not required?
logger.debug('Skipping property ' + key + ' as it is not marked as required.');
continue;
}
if (!!detail?.const) {
out[key] = getPropertyValue(key, detail);
}
if (detail?.type === 'object') {
// recursive instantiation
logger.info('Recursively instantiating an ' + objectType + ' object');
Expand Down
36 changes: 3 additions & 33 deletions shared/src/commands/generate/components/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { initLogger } from '../../helper.js';
import { SchemaDirectory } from '../schema-directory.js';
import { logRequiredMessage, mergeSchemas } from '../util.js';
import { instantiateGenericObject } from './instantiate.js';
import { getPropertyValue } from './property.js';

/**
Expand All @@ -13,38 +14,7 @@ import { getPropertyValue } from './property.js';
* @returns An instantiated node.
*/
export function instantiateNode(nodeDef: any, schemaDirectory: SchemaDirectory, debug: boolean = false, instantiateAll: boolean = false): any {
const logger = initLogger(debug);
let fullDefinition = nodeDef;
if (nodeDef['$ref']) {
const ref = nodeDef['$ref'];
const schemaDef = schemaDirectory.getDefinition(ref);

fullDefinition = mergeSchemas(schemaDef, nodeDef);
}
logger.debug('Generating node 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 (key === 'interfaces') {
const interfaces = instantiateNodeInterfaces(detail, schemaDirectory, debug, instantiateAll);
out['interfaces'] = interfaces;
continue;
}

if (!instantiateAll && required && !required.includes(key)) {
logger.debug('Skipping property ' + key + ' as it is not marked as required.');
continue;
}
out[key] = getPropertyValue(key, detail);
}
return out;
return instantiateGenericObject(nodeDef, schemaDirectory, 'node', debug, instantiateAll);
}

/**
Expand Down Expand Up @@ -72,6 +42,7 @@ export function instantiateNodes(pattern: any, schemaDirectory: SchemaDirectory,
return outputNodes;
}

// TODO remove all this
/**
* Instantiate an individual interface on a node.
* @param interfaceDef The definition of the interface.
Expand Down Expand Up @@ -100,7 +71,6 @@ export function instantiateInterface(interfaceDef: object, schemaDirectory: Sche

const out = {};
for (const [key, detail] of Object.entries(fullDefinition['properties'])) {
// TODO flag to force instantiate all
if (!instantiateAll && required && !required.includes(key)) {
logger.debug('Skipping property ' + key + ' as it is not marked as required.');
continue;
Expand Down
1 change: 0 additions & 1 deletion shared/src/commands/generate/components/property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ interface Detail {
}

export function getPropertyValue(keyName: string, detail: Detail): string | string[] | number | object {
// TODO follow refs here
// should be able to instantiate not just a simple enum type but also a whole sub-object
// if both const and type are defined, prefer const
if (detail.const) {
Expand Down

0 comments on commit 0bb10f8

Please sign in to comment.