Skip to content

Commit e2ab7f7

Browse files
authored
Merge pull request #75 from Exabyte-io/feature/SOF-6862
feature/SOF-6862: add listToString yaml custom tag
2 parents 593a1ae + b42b90c commit e2ab7f7

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

src/utils/yaml.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// @ts-nocheck
22
// TODO: remove ts-nocheck
3-
import fs from "fs";
4-
import yaml from "js-yaml";
5-
import lodash from "lodash";
6-
import path from "path";
3+
import * as fs from "fs";
4+
import * as yaml from "js-yaml";
5+
import * as lodash from "lodash";
6+
import * as path from "path";
77

88
import { esseSchema, JSONSchemasInterface } from "../JSONSchemasInterface";
99
import { safeMakeArray } from "./array";
@@ -198,6 +198,21 @@ export const includeType = new yaml.Type("!include", {
198198
},
199199
});
200200

201+
/**
202+
* !listToString YAML tag which concatenate each list item into single string value.
203+
* See the tests for example usage.
204+
*/
205+
export const listToStringType = new yaml.Type("!listToString", {
206+
kind: "sequence",
207+
construct(data) {
208+
try {
209+
return data.filter((d) => typeof d === "string").join("");
210+
} catch (e) {
211+
return data;
212+
}
213+
},
214+
});
215+
201216
/**
202217
* !flatten YAML tag for flattening arrays
203218
* See the tests for example usage.
@@ -248,6 +263,7 @@ export const concatStringType = new yaml.Type("!concatString", {
248263

249264
export const JsYamlTypes = {
250265
include: includeType,
266+
listToString: listToStringType,
251267
parameter: parameterType,
252268
combine: combineType,
253269
esse: esseType,
@@ -261,6 +277,7 @@ export const JsYamlAllSchemas = yaml.DEFAULT_SCHEMA.extend([
261277
combineType,
262278
esseType,
263279
includeType,
280+
listToStringType,
264281
flattenType,
265282
readFileType,
266283
concatStringType,

tests/enums.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as path from "path";
22

33
export const FIXTURES_DIR = path.resolve(__dirname, "./fixtures");
44
export const YAML_COMBINE_FILE = path.resolve(FIXTURES_DIR, "yaml_combine_tag.yml");
5+
export const YAML_LIST_TO_STRING_FILE = path.resolve(FIXTURES_DIR, "yaml_listToString_tag.yml");
56
export const YAML_PARAMETER_FILE = path.resolve(FIXTURES_DIR, "yaml_parameter_tag.yml");
67
export const YAML_ESSE_FILE = path.resolve(FIXTURES_DIR, "yaml_esse_tag.yml");
78
export const YAML_INCLUDE_FILE = path.resolve(FIXTURES_DIR, "yaml_include_tag.yml");
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# listToString content from another Yaml file
2+
case1:
3+
here: original content
4+
there: !listToString
5+
- one # comment 1
6+
- two # comment 2
7+
case2:
8+
here: original content
9+
there: !listToString
10+
- one # comment 1
11+
- 1 # non-string ignored
12+
- some: 1 # object also skipped
13+
other: "object"
14+
- two # comment 2
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// @ts-nocheck
2+
import { expect } from "chai";
3+
import fs from "fs";
4+
import yaml from "js-yaml";
5+
6+
import { listToStringType } from "../../src/utils/yaml";
7+
import { YAML_LIST_TO_STRING_FILE } from "../enums";
8+
9+
const includeSchema = yaml.DEFAULT_SCHEMA.extend([listToStringType]);
10+
11+
describe("YAML tag: !listToString", () => {
12+
const yamlFixture = fs.readFileSync(YAML_LIST_TO_STRING_FILE, "utf8");
13+
14+
it("should correctly concatenate content from list to string", () => {
15+
const parsed = yaml.load(yamlFixture, { schema: includeSchema });
16+
17+
const expected = {
18+
here: "original content",
19+
there: "onetwo",
20+
};
21+
expect(parsed.case1).to.be.eql(expected);
22+
});
23+
24+
it("should correctly filter out and concatenate only strings from list", () => {
25+
const parsed = yaml.load(yamlFixture, { schema: includeSchema });
26+
27+
const expected = {
28+
here: "original content",
29+
there: "onetwo",
30+
};
31+
expect(parsed.case2).to.be.eql(expected);
32+
});
33+
});

0 commit comments

Comments
 (0)