-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53c8ad5
commit 347c1d6
Showing
4 changed files
with
87 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,51 @@ | ||
import { load as loadYaml, YAMLException } from "js-yaml"; | ||
import { flattenTree } from "../utils.js"; | ||
import { dump, load as loadYaml, YAMLException } from "js-yaml"; | ||
import { flattenTree, setPathOnTree } from "../utils.js"; | ||
import { LoadingException } from "../exception.js"; | ||
import { ResultMatcher } from "../../utils/resultMatcher.js"; | ||
|
||
/** @type {import("../types.js").FormatHandler} */ | ||
export const YamlHandler = { | ||
fileExtensions: ["yaml", "yml"], | ||
load: (filePath, content) => { | ||
/** @param {YAMLException} e */ | ||
const raiseLoadingException = (e) => { | ||
throw new LoadingException( | ||
`Could not parse YAML file ${filePath}: ${e.message}`, | ||
{ cause: e }, | ||
); | ||
}; | ||
|
||
return new ResultMatcher(loadYaml) | ||
.ok(flattenTree) | ||
.catch(YAMLException, raiseLoadingException) | ||
.run(content, { filename: filePath }); | ||
const tree = parseAsTree(content, filePath); | ||
return new ResultMatcher(flattenTree) | ||
.catchAll((e) => raiseLoadingException(e, filePath)) | ||
.run(tree); | ||
}, | ||
setPath() { | ||
throw new Error("Not implemented"); | ||
setPath(oldYaml, key, value) { | ||
const tree = parseAsTree(oldYaml); | ||
|
||
const path = key.split("."); | ||
setPathOnTree(tree, path, value); | ||
|
||
const newYaml = dump(tree); | ||
return newYaml; | ||
}, | ||
}; | ||
|
||
/** | ||
* @param {string} content | ||
* @param {string|undefined} filePath | ||
* @return {unknown} | ||
*/ | ||
function parseAsTree(content, filePath = undefined) { | ||
return new ResultMatcher(loadYaml) | ||
.ok((res) => { | ||
if (typeof res !== "object") return {}; | ||
return res; | ||
}) | ||
.catch(YAMLException, (e) => raiseLoadingException(e, filePath)) | ||
.run(content, { filename: filePath }); | ||
} | ||
|
||
/** | ||
* @param {unknown} e | ||
* @param {string | undefined} filePath | ||
*/ | ||
const raiseLoadingException = (e, filePath = undefined) => { | ||
const msg = | ||
e instanceof Error | ||
? `Could not parse YAML file ${filePath ?? ""}: ${e.message}` | ||
: `Could not parse YAML file ${filePath ?? ""}`; | ||
throw new LoadingException(msg, { cause: e }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { LoadingException } from "../exception"; | ||
import { YamlHandler } from "./yaml"; | ||
|
||
describe("YamlHandler", () => { | ||
it("parses an empty file as an empty dictionary", () => { | ||
const result = YamlHandler.load("test.yaml", ""); | ||
expect(result).toEqual(new Map()); | ||
}); | ||
|
||
it("sets a path on an empty file", () => { | ||
const newYaml = YamlHandler.setPath("", "key1.key2", "value"); | ||
expect(newYaml).toEqual("key1:\n key2: value\n"); | ||
}); | ||
|
||
it("sets a path on an existing file", () => { | ||
const oldYaml = "key1:\n key2: value\n"; | ||
const newYaml = YamlHandler.setPath(oldYaml, "key1.key3", "value"); | ||
expect(newYaml).toEqual("key1:\n key2: value\n key3: value\n"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters