From 3c4da6c4c0630784b2ca6a0e6ba73e17224b0d64 Mon Sep 17 00:00:00 2001 From: Rohan Juneja Date: Mon, 4 Mar 2024 16:56:48 -0800 Subject: [PATCH] allow predicates/specs to be passed in directly --- src/load/all_specs_sync_loader.ts | 16 ++++++++++++---- src/load/api_list_specs_sync_loader.ts | 4 ++-- src/load/component_specs_sync_loader.ts | 4 ++-- src/load/single_spec_sync_loader.ts | 4 ++-- src/load/sync_loader_factory.ts | 15 ++++++++------- src/load/tag_specs_sync_loader.ts | 4 ++-- src/load/team_specs_sync_loader.ts | 4 ++-- .../sync_operations_builder.ts | 1 + .../sync_operations_builder_with_reasoner.ts | 6 ++++++ src/types.ts | 2 ++ 10 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/load/all_specs_sync_loader.ts b/src/load/all_specs_sync_loader.ts index 6e59ba5..ca92a78 100644 --- a/src/load/all_specs_sync_loader.ts +++ b/src/load/all_specs_sync_loader.ts @@ -7,14 +7,22 @@ const debug = Debug("bte:smartapi-kg:AllSpecsSyncLoader"); export default class AllSpecsSyncLoader extends BaseLoader { private _file_path: string; - constructor(path: string) { + private _smartapiSpecs?: SmartAPISpec | SmartAPIQueryResult; + constructor(path: string, smartapiSpecs?: SmartAPISpec | SmartAPIQueryResult) { super(); this._file_path = path; + this._smartapiSpecs = smartapiSpecs; } protected fetch(): SmartAPIQueryResult { - debug(`Fetching from file path: ${this._file_path}`); - const file = fs.readFileSync(this._file_path, "utf-8"); - const data = JSON.parse(file) as SmartAPIQueryResult | SmartAPISpec; + let data: SmartAPIQueryResult | SmartAPISpec; + if (this._smartapiSpecs) { + data = this._smartapiSpecs; + } else { + debug(`Fetching from file path: ${this._file_path}`); + const file = fs.readFileSync(this._file_path, "utf-8"); + data = JSON.parse(file) as SmartAPIQueryResult | SmartAPISpec; + } + let result; if (!("hits" in data)) { result = { diff --git a/src/load/api_list_specs_sync_loader.ts b/src/load/api_list_specs_sync_loader.ts index 3a2c0ea..8e9a30b 100644 --- a/src/load/api_list_specs_sync_loader.ts +++ b/src/load/api_list_specs_sync_loader.ts @@ -7,8 +7,8 @@ const debug = Debug("bte:smartapi-kg:APIListSpecsSyncLoader"); export default class APIListSpecsSyncLoader extends AllSpecsSyncLoader { private _apiList: apiListObject | undefined; - constructor(path: string, apiList?: apiListObject) { - super(path); + constructor(path: string, apiList?: apiListObject, smartapiSpecs?: SmartAPISpec | SmartAPIQueryResult) { + super(path, smartapiSpecs); this._apiList = apiList; } diff --git a/src/load/component_specs_sync_loader.ts b/src/load/component_specs_sync_loader.ts index 201c163..8093194 100644 --- a/src/load/component_specs_sync_loader.ts +++ b/src/load/component_specs_sync_loader.ts @@ -5,8 +5,8 @@ import AllSpecsSyncLoader from "./all_specs_sync_loader"; export default class ComponentSpecsSyncLoader extends AllSpecsSyncLoader { private _component: string; - constructor(component: string, path: string) { - super(path); + constructor(component: string, path: string, smartapiSpecs?: SmartAPISpec | SmartAPIQueryResult) { + super(path, smartapiSpecs); this._component = component; } diff --git a/src/load/single_spec_sync_loader.ts b/src/load/single_spec_sync_loader.ts index 0db984a..f64b0bf 100644 --- a/src/load/single_spec_sync_loader.ts +++ b/src/load/single_spec_sync_loader.ts @@ -8,8 +8,8 @@ export default class SingleSpecSyncLoader extends AllSpecsSyncLoader { private _smartAPIID: string; private _apiList: apiListObject | undefined; - constructor(smartAPIID: string, path: string, apiList?: apiListObject) { - super(path); + constructor(smartAPIID: string, path: string, apiList?: apiListObject, smartapiSpecs?: SmartAPISpec | SmartAPIQueryResult) { + super(path, smartapiSpecs); this._smartAPIID = smartAPIID; this._apiList = apiList; } diff --git a/src/load/sync_loader_factory.ts b/src/load/sync_loader_factory.ts index 3bc726c..224b886 100644 --- a/src/load/sync_loader_factory.ts +++ b/src/load/sync_loader_factory.ts @@ -5,7 +5,7 @@ import TagSpecsSyncLoader from "./tag_specs_sync_loader"; import ComponentSpecsSyncLoader from "./component_specs_sync_loader"; import APIListSpecsSyncLoader from "./api_list_specs_sync_loader"; import { SmartAPISpec } from "../parser/types"; -import { apiListObject } from "../types"; +import { SmartAPIQueryResult, apiListObject } from "../types"; import Debug from "debug"; const debug = Debug("bte:smartapi-kg:SyncLoader"); @@ -16,25 +16,26 @@ export const syncLoaderFactory = ( component: string = undefined, apiList: apiListObject = undefined, path: string, + smartapiSpecs?: SmartAPISpec | SmartAPIQueryResult, ): SmartAPISpec[] => { let loader; if (!(typeof smartAPIID === "undefined")) { - loader = new SingleSpecSyncLoader(smartAPIID, path, apiList); + loader = new SingleSpecSyncLoader(smartAPIID, path, apiList, smartapiSpecs); debug("Using single spec sync loader now."); } else if (!(typeof teamName === "undefined")) { - loader = new TeamSpecsSyncLoader(teamName, path, apiList); + loader = new TeamSpecsSyncLoader(teamName, path, apiList, smartapiSpecs); debug("Using team spec sync loader now."); } else if (!(typeof tag === "undefined")) { - loader = new TagSpecsSyncLoader(tag, path); + loader = new TagSpecsSyncLoader(tag, path, smartapiSpecs); debug("Using tags spec sync loader now."); } else if (!(typeof component === "undefined")) { - loader = new ComponentSpecsSyncLoader(component, path); + loader = new ComponentSpecsSyncLoader(component, path, smartapiSpecs); debug("Using component spec sync loader now."); } else if (!(typeof apiList === "undefined")) { - loader = new APIListSpecsSyncLoader(path, apiList); + loader = new APIListSpecsSyncLoader(path, apiList, smartapiSpecs); debug("Using api list spec sync loader now."); } else { - loader = new AllSpecsSyncLoader(path); + loader = new AllSpecsSyncLoader(path, smartapiSpecs); debug("Using all specs sync loader now."); } return loader.load(); diff --git a/src/load/tag_specs_sync_loader.ts b/src/load/tag_specs_sync_loader.ts index 64f0bca..a47b1c9 100644 --- a/src/load/tag_specs_sync_loader.ts +++ b/src/load/tag_specs_sync_loader.ts @@ -5,8 +5,8 @@ import AllSpecsSyncLoader from "./all_specs_sync_loader"; export default class TagSpecsSyncLoader extends AllSpecsSyncLoader { private _tag: string; - constructor(tag: string, path: string) { - super(path); + constructor(tag: string, path: string, smartapiSpecs?: SmartAPISpec | SmartAPIQueryResult) { + super(path, smartapiSpecs); this._tag = tag; } diff --git a/src/load/team_specs_sync_loader.ts b/src/load/team_specs_sync_loader.ts index 37e1427..3a3c6fc 100644 --- a/src/load/team_specs_sync_loader.ts +++ b/src/load/team_specs_sync_loader.ts @@ -5,8 +5,8 @@ import APIListSpecsSyncLoader from "./api_list_specs_sync_loader"; export default class TeamSpecsSyncLoader extends APIListSpecsSyncLoader { private _teamName: string; - constructor(teamName: string, path: string, apiList?: apiListObject) { - super(path, apiList); + constructor(teamName: string, path: string, apiList?: apiListObject, smartapiSpecs?: SmartAPISpec | SmartAPIQueryResult) { + super(path, apiList, smartapiSpecs); this._teamName = teamName; } diff --git a/src/operations_builder/sync_operations_builder.ts b/src/operations_builder/sync_operations_builder.ts index 3a5e165..bd31788 100644 --- a/src/operations_builder/sync_operations_builder.ts +++ b/src/operations_builder/sync_operations_builder.ts @@ -18,6 +18,7 @@ export default class SyncOperationsBuilder extends BaseOperationsBuilder { this._options.component, this._options.apiList, this._file_path, + this._options.smartapiSpecs ); return this.loadOpsFromSpecs(specs); } diff --git a/src/operations_builder/sync_operations_builder_with_reasoner.ts b/src/operations_builder/sync_operations_builder_with_reasoner.ts index 1e84329..16cff77 100644 --- a/src/operations_builder/sync_operations_builder_with_reasoner.ts +++ b/src/operations_builder/sync_operations_builder_with_reasoner.ts @@ -137,6 +137,10 @@ export default class SyncOperationsBuilderWithReasoner extends BaseOperationsBui } private fetch(): PredicatesMetadata[] { + if (this._options.predicates) { + return this._options.predicates; + } + const file = fs.readFileSync(this._predicates_file_path, "utf-8"); const data = JSON.parse(file) as PredicatesMetadata[]; return data; @@ -150,6 +154,7 @@ export default class SyncOperationsBuilderWithReasoner extends BaseOperationsBui this._options.component, this._options.apiList, this._file_path, + this._options.smartapiSpecs ); const nonTRAPIOps = this.loadOpsFromSpecs(specs); const predicatesMetadata = this.fetch(); @@ -160,6 +165,7 @@ export default class SyncOperationsBuilderWithReasoner extends BaseOperationsBui undefined, undefined, this._file_path, + this._options.smartapiSpecs ).filter( spec => "info" in spec && diff --git a/src/types.ts b/src/types.ts index 3736818..c7b7c08 100644 --- a/src/types.ts +++ b/src/types.ts @@ -22,6 +22,8 @@ export interface BuilderOptions { smartAPIID?: string; component?: string; apiList?: apiListObject; + predicates?: PredicatesMetadata[]; + smartapiSpecs?: SmartAPISpec | SmartAPIQueryResult; } interface PredicatesQueryOperationInterface {