Skip to content

Commit 3725120

Browse files
authored
Merge pull request #61 from Exabyte-io/chore/SOF-5945
Chore/sof 5945
2 parents 3de17b4 + 1a0884d commit 3725120

11 files changed

+152
-3
lines changed

package-lock.json

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/yaml.js

+54
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,70 @@ export const includeType = new yaml.Type("!include", {
183183
},
184184
});
185185

186+
/**
187+
* !flatten YAML tag for flattening arrays
188+
* See the tests for example usage.
189+
*/
190+
export const flattenType = new yaml.Type("!flatten", {
191+
kind: "sequence",
192+
construct(data) {
193+
try {
194+
return data.flat();
195+
} catch (e) {
196+
return data;
197+
}
198+
},
199+
});
200+
201+
/**
202+
* !readFile YAML tag for including file contents as a string.
203+
* See the tests for example usage.
204+
*/
205+
export const readFileType = new yaml.Type("!readFile", {
206+
kind: "scalar",
207+
construct(data) {
208+
try {
209+
return fs.readFileSync(path.resolve(data), "utf8");
210+
} catch (e) {
211+
return data;
212+
}
213+
},
214+
});
215+
216+
/**
217+
* !concatString YAML tag for concatenating strings.
218+
* See the tests for example usage.
219+
*/
220+
export const concatStringType = new yaml.Type("!concatString", {
221+
kind: "sequence",
222+
resolve(data) {
223+
return data.every((d) => lodash.isString(d));
224+
},
225+
construct(data) {
226+
try {
227+
return "".concat(...data);
228+
} catch (e) {
229+
return data;
230+
}
231+
},
232+
});
233+
186234
export const JsYamlTypes = {
187235
include: includeType,
188236
parameter: parameterType,
189237
combine: combineType,
190238
esse: esseType,
239+
flatten: flattenType,
240+
readFile: readFileType,
241+
concatString: concatStringType,
191242
};
192243

193244
export const JsYamlAllSchemas = yaml.DEFAULT_SCHEMA.extend([
194245
parameterType,
195246
combineType,
196247
esseType,
197248
includeType,
249+
flattenType,
250+
readFileType,
251+
concatStringType,
198252
]);

tests/enums.js

+3
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ export const YAML_COMBINE_FILE = path.resolve(FIXTURES_DIR, "yaml_combine_tag.ym
55
export const YAML_PARAMETER_FILE = path.resolve(FIXTURES_DIR, "yaml_parameter_tag.yml");
66
export const YAML_ESSE_FILE = path.resolve(FIXTURES_DIR, "yaml_esse_tag.yml");
77
export const YAML_INCLUDE_FILE = path.resolve(FIXTURES_DIR, "yaml_include_tag.yml");
8+
export const YAML_FLATTEN_FILE = path.resolve(FIXTURES_DIR, "yaml_flatten_tag.yml");
9+
export const YAML_READFILE_FILE = path.resolve(FIXTURES_DIR, "yaml_readFile_tag.yml");
10+
export const YAML_CONCAT_STRING_FILE = path.resolve(FIXTURES_DIR, "yaml_concatString_tag.yml");

tests/fixtures/test_content.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Example text
2+
with linebreaks.
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# concatenate three strings
2+
case1: !concatString
3+
- "Hello "
4+
- world
5+
- "!"
6+
# return a string if there is only a single item
7+
case2: !concatString
8+
- Hello
9+
# return an empty string if the array is empty
10+
case3: !concatString []

tests/fixtures/yaml_flatten_tag.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# convert an array of arrays into a flattened array
2+
case1: !flatten
3+
- [a, b, c]
4+
- d
5+
- [e, f]

tests/fixtures/yaml_readFile_tag.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# read contents of a file into a string
2+
case1: !readFile "tests/fixtures/test_content.txt"

tests/utils/yaml.combine.tests.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import { YAML_COMBINE_FILE } from "../enums";
1010
const combineSchema = yaml.DEFAULT_SCHEMA.extend([combineType, esseType]);
1111

1212
describe("YAML tag: !combine", () => {
13-
const yamlFixture = fs.readFileSync(YAML_COMBINE_FILE, "utf8");
13+
let yamlFixture;
14+
15+
before(() => {
16+
yamlFixture = fs.readFileSync(YAML_COMBINE_FILE, "utf8");
17+
});
18+
1419
it("should correctly parse a custom !combine tag with forEach and config keys", () => {
1520
const parsed = yaml.load(yamlFixture, { schema: combineSchema });
1621
const expectedResult = [
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expect } from "chai";
2+
import fs from "fs";
3+
import yaml from "js-yaml";
4+
5+
import { concatStringType } from "../../src/utils/yaml";
6+
import { YAML_CONCAT_STRING_FILE } from "../enums";
7+
8+
const concatStringSchema = yaml.DEFAULT_SCHEMA.extend([concatStringType]);
9+
10+
describe("YAML tag: !concatString", () => {
11+
const yamlFixture = fs.readFileSync(YAML_CONCAT_STRING_FILE, "utf8");
12+
13+
it("should concatenate two strings", () => {
14+
const parsed = yaml.load(yamlFixture, { schema: concatStringSchema });
15+
const expected = "Hello world!";
16+
expect(parsed.case1).to.be.eql(expected);
17+
});
18+
19+
it("should return a string if there is only a single item", () => {
20+
const parsed = yaml.load(yamlFixture, { schema: concatStringSchema });
21+
const expected = "Hello";
22+
expect(parsed.case2).to.be.eql(expected);
23+
});
24+
25+
it("should return an empty string if the array is empty", () => {
26+
const parsed = yaml.load(yamlFixture, { schema: concatStringSchema });
27+
const expected = "";
28+
expect(parsed.case3).to.be.eql(expected);
29+
});
30+
});

tests/utils/yaml.flatten.tests.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { expect } from "chai";
2+
import fs from "fs";
3+
import yaml from "js-yaml";
4+
5+
import { flattenType } from "../../src/utils/yaml";
6+
import { YAML_FLATTEN_FILE } from "../enums";
7+
8+
const flattenSchema = yaml.DEFAULT_SCHEMA.extend([flattenType]);
9+
10+
describe("YAML tag: !flatten", () => {
11+
const yamlFixture = fs.readFileSync(YAML_FLATTEN_FILE, "utf8");
12+
13+
it("should convert an array of arrays into a flattened array", () => {
14+
const parsed = yaml.load(yamlFixture, { schema: flattenSchema });
15+
const expected = ["a", "b", "c", "d", "e", "f"];
16+
expect(parsed.case1).to.be.eql(expected);
17+
});
18+
});

tests/utils/yaml.readFile.tests.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { expect } from "chai";
2+
import fs from "fs";
3+
import yaml from "js-yaml";
4+
5+
import { readFileType } from "../../src/utils/yaml";
6+
import { YAML_READFILE_FILE } from "../enums";
7+
8+
const readFileSchema = yaml.DEFAULT_SCHEMA.extend([readFileType]);
9+
10+
describe("YAML tag: !readFile", () => {
11+
const yamlFixture = fs.readFileSync(YAML_READFILE_FILE, "utf8");
12+
13+
it("should read contents of a file into a string", () => {
14+
const parsed = yaml.load(yamlFixture, { schema: readFileSchema });
15+
const expected = "Example text\nwith linebreaks.\n";
16+
expect(parsed.case1).to.be.a("string");
17+
expect(parsed.case1).to.be.eql(expected);
18+
});
19+
});

0 commit comments

Comments
 (0)