Skip to content

Commit

Permalink
fix: ensure JQ string receives array of curies
Browse files Browse the repository at this point in the history
  • Loading branch information
tokebe committed Dec 5, 2023
1 parent ed9ebaf commit b834e91
Showing 1 changed file with 8 additions and 20 deletions.
28 changes: 8 additions & 20 deletions src/transformers/jq_transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,21 @@ import { toArray } from "../utils";
const filterStringsWrap = Object.fromEntries(
fs.readdirSync(Path.resolve(`${__dirname}/../../data/jq/wrap`)).map(file => {
const filePath = Path.resolve(`${__dirname}/../../data/jq/wrap/${file}`);
return [
Path.parse(filePath).name,
fs.readFileSync(filePath, { encoding: "utf8" }),
];
return [Path.parse(filePath).name, fs.readFileSync(filePath, { encoding: "utf8" })];
}),
);

const filterStringsPair = Object.fromEntries(
fs.readdirSync(Path.resolve(`${__dirname}/../../data/jq/pair`)).map(file => {
const filePath = Path.resolve(`${__dirname}/../../data/jq/pair/${file}`);
return [
Path.parse(filePath).name,
fs.readFileSync(filePath, { encoding: "utf8" }),
];
return [Path.parse(filePath).name, fs.readFileSync(filePath, { encoding: "utf8" })];
}),
);

export default class JQTransformer extends BaseTransformer {
// TODO more specific typing?
async wrap(res: JSONDoc | JSONDoc[]): Promise<JSONDoc> {
let filterString: string | undefined =
this.config.wrap ?? filterStringsWrap[this.config.type];
let filterString: string | undefined = this.config.wrap ?? filterStringsWrap[this.config.type];
if (typeof filterString === "undefined") return super.wrap(res);
filterString = generateFilterString(filterString);
return JSON.parse(
Expand All @@ -43,10 +36,8 @@ export default class JQTransformer extends BaseTransformer {
}

async pairCurieWithAPIResponse(): Promise<PairedResponse> {
let filterString: string | undefined =
this.config.pair ?? filterStringsPair[this.config.type];
if (typeof filterString === "undefined")
return super.pairCurieWithAPIResponse();
let filterString: string | undefined = this.config.pair ?? filterStringsPair[this.config.type];
if (typeof filterString === "undefined") return super.pairCurieWithAPIResponse();
const data = {
response: this.data.response,
edge: {
Expand All @@ -58,10 +49,9 @@ export default class JQTransformer extends BaseTransformer {
type: this.edge.association.input_type,
// input is array or is object with queryInputs
curies:
Array.isArray(this.edge.input) ||
typeof this.edge.input === "string"
Array.isArray(this.edge.input) || typeof this.edge.input === "string"
? toArray(this.edge.input)
: this.edge.input.queryInputs,
: toArray(this.edge.input.queryInputs),
},
predicate: this.edge.association.predicate,
output: {
Expand All @@ -71,8 +61,6 @@ export default class JQTransformer extends BaseTransformer {
},
};
filterString = `.edge as $edge | ${generateFilterString(filterString)}`;
return JSON.parse(
(await jq.run(filterString, data, { input: "json" })) as string,
);
return JSON.parse((await jq.run(filterString, data, { input: "json" })) as string);
}
}

0 comments on commit b834e91

Please sign in to comment.