Skip to content

Commit

Permalink
import text
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffhendrey committed Oct 1, 2024
1 parent 95da3f4 commit 507bd8f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions example/importme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello - I am an imported text file
this is my second line
10 changes: 9 additions & 1 deletion src/TemplateProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,8 @@ export default class TemplateProcessor {
format = 'json';
} else if (contentType.includes("text/yaml")) {
format = 'yaml';
}else if (contentType.includes("text/plain")) {
format = 'yaml';
}
} //we can still encounter incorrect conetnt-type like text/plain for json or yaml on various hosting sites like github raw
if(!format){
Expand All @@ -670,12 +672,16 @@ export default class TemplateProcessor {
format = 'json';
} else if (fileExtension === 'yaml' || fileExtension === 'yml') {
format = 'yaml';
} else if (fileExtension === 'text' || fileExtension === 'txt') {
format = 'text';
}
}

switch (format) {
case 'json':
return await resp.json();
case 'text':
return await resp.text();
case 'yaml':
const text = await resp.text();
return yaml.load(text);
Expand Down Expand Up @@ -1778,15 +1784,17 @@ export default class TemplateProcessor {
return JSON.parse(content);
}else if (fileExtension === '.yaml' || fileExtension === '.yml') {
return yaml.load(content);
}else if (fileExtension === '.text' || fileExtension === '.txt') {
return content;
}else if (fileExtension === '.js' || fileExtension === '.mjs') {
throw new Error('js and mjs imports not implemented yet');
}else{
throw new Error('import file extension must be .json or .yaml or .yml');
}
} catch(e) {
this.logger.debug('import was not a local file');
throw e;
}
return content;
}

private wrapInOrdinaryFunction(jsonataLambda:any) {
Expand Down
13 changes: 13 additions & 0 deletions src/test/TemplateProcessor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,19 @@ test("local import with non-absolute --importPath", async () => {
});
});

test("local import textfile with non-absolute --importPath", async () => {
const template = {
"foo": "bar",
"baz": "${ $import('importme.txt') }"
};
const tp = new TemplateProcessor(template, {}, {importPath: 'example'});
await tp.initialize();
expect(tp.output).toEqual({
"baz": "Hello - I am an imported text file\nthis is my second line",
"foo": "bar"
});
});

test("deep view", async () => {
const template = {
"closureExpression": "/${ ($names := $distinct(data.pD.data.name); {'yAxis': [ {'categories': $names} ]}) }",
Expand Down

0 comments on commit 507bd8f

Please sign in to comment.