Skip to content

Commit

Permalink
allow predicates/specs to be passed in directly
Browse files Browse the repository at this point in the history
  • Loading branch information
rjawesome committed Mar 5, 2024
1 parent 7386b43 commit 3c4da6c
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 21 deletions.
16 changes: 12 additions & 4 deletions src/load/all_specs_sync_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions src/load/api_list_specs_sync_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/load/component_specs_sync_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/load/single_spec_sync_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
15 changes: 8 additions & 7 deletions src/load/sync_loader_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/load/tag_specs_sync_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/load/team_specs_sync_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions src/operations_builder/sync_operations_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -160,6 +165,7 @@ export default class SyncOperationsBuilderWithReasoner extends BaseOperationsBui
undefined,
undefined,
this._file_path,
this._options.smartapiSpecs
).filter(
spec =>
"info" in spec &&
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface BuilderOptions {
smartAPIID?: string;
component?: string;
apiList?: apiListObject;
predicates?: PredicatesMetadata[];
smartapiSpecs?: SmartAPISpec | SmartAPIQueryResult;
}

interface PredicatesQueryOperationInterface {
Expand Down

0 comments on commit 3c4da6c

Please sign in to comment.