Skip to content

Add yaml support #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ We also support JSONata Notebooks. They are automatically activated for files wi

Within notebooks several additional functions are available:

- `$parseString($content[, $type])` takes up to two arguments. It parses `$content` into an JSON object. This happens based on the variable `$type` with a JSON or an XML parser. JSON is the default parser.
- `$loadFile($file[, $type])` takes up to two arguments. `$file` represents the filename to be loaded. With the optional argument `$type` one can specify how this file should be loaded. At the moment only `json` and `xml` can be chosen whereas json is the standard if `$type` is missing.
- `$parseString($content[, $type])` takes up to two arguments. It parses `$content` into an JSON object. This happens based on the variable `$type` with a JSON, XML or YAML parser. JSON is the default parser.
- `$loadFile($file[, $type])` takes up to two arguments. `$file` represents the filename to be loaded. With the optional argument `$type` one can specify how this file should be loaded. At the moment only `json`, `xml` and `yaml` can be chosen whereas json is the standard if `$type` is missing.
- `$loadUrl($url[, $type])` works just like `$loadFile()` but with URLs instead of files.

- `$readFile($file)` reads a file and returns the result as a string.
Expand Down
166 changes: 106 additions & 60 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
},
"dependencies": {
"@types/lodash": "^4.14.178",
"js-yaml": "^4.1.0",
"json-stringify-safe": "^5.0.1",
"jsonata": "^2.0.3",
"lodash": "^4.17.21",
Expand Down
8 changes: 8 additions & 0 deletions src/kernel/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Utils } from 'vscode-uri';
import fetch from 'node-fetch';
import loadJSON from './loader/json';
import loadXML from './loader/xml';
import loadYaml from './loader/yaml';

const jsonata = require('jsonata');

Expand All @@ -22,6 +23,8 @@ export function parseString(content: string, type?: string) {
return loadJSON(content);
} if (type === 'xml') {
return loadXML(content);
} if (type === 'yaml') {
return loadYaml(content)
}
return Promise.reject('unknown file handler!');
}
Expand Down Expand Up @@ -53,6 +56,11 @@ export function readUrl(url: string) {
}

export function loadFile(filename: string, type?: string) {
if (!type && filename.endsWith(".xml")) {
type = "xml"
} else if (!type && (filename.endsWith("yaml") || filename.endsWith("yml"))) {
type = "yaml"
}
return readFile(filename)
.then((content) => parseString(content, type));
}
Expand Down
9 changes: 9 additions & 0 deletions src/kernel/loader/yaml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const yaml = require('js-yaml');

export default function loadYaml(fileString: string) : Promise<unknown> {
try {
return Promise.resolve(yaml.load(fileString));
} catch(e) {
return Promise.reject(e);
}
}