-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(yaml): Support YAML 1.1 features
- Loading branch information
Showing
7 changed files
with
175 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
"@kosko/yaml": major | ||
"@kosko/helm": major | ||
"@kosko/kustomize": major | ||
--- | ||
|
||
Support the following YAML 1.1 features to match the behavior of [sigs.k8s.io/yaml](https://pkg.go.dev/sigs.k8s.io/yaml). | ||
|
||
- Numbers starting with `0` (e.g. `0777`) are interpreted as octal numbers, instead of decimal numbers. | ||
- YAML 1.2 octal number `0o` prefix is still supported. | ||
- YAML 1.1 booleans (`yes`, `no`, `on`, `off`, `y`, `n`) are interpreted as booleans, instead of strings. |
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
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,76 @@ | ||
import { parse } from "../yaml"; | ||
|
||
test.each([ | ||
// YAML 1.1 | ||
{ input: "0123", expected: 83 }, | ||
// YAML 1.2 | ||
{ input: "0o123", expected: 83 }, | ||
// Decimal | ||
{ input: "123", expected: 123 }, | ||
// String | ||
{ input: `"0123"`, expected: "0123" }, | ||
// YAML 1.1 in key | ||
{ input: "0123: value", expected: { 83: "value" } }, | ||
// YAML 1.2 in key | ||
{ input: "0o123: value", expected: { 83: "value" } } | ||
])("octal number: $input -> $expected", ({ input, expected }) => { | ||
expect(parse(input)).toEqual([expected]); | ||
}); | ||
|
||
test.each([ | ||
// Yes | ||
{ input: "yes", expected: true }, | ||
{ input: "Yes", expected: true }, | ||
{ input: "YES", expected: true }, | ||
// No | ||
{ input: "no", expected: false }, | ||
{ input: "No", expected: false }, | ||
{ input: "NO", expected: false }, | ||
// On | ||
{ input: "on", expected: true }, | ||
{ input: "On", expected: true }, | ||
{ input: "ON", expected: true }, | ||
// Off | ||
{ input: "off", expected: false }, | ||
{ input: "Off", expected: false }, | ||
{ input: "OFF", expected: false }, | ||
// Y | ||
{ input: "y", expected: true }, | ||
{ input: "Y", expected: true }, | ||
// N | ||
{ input: "n", expected: false }, | ||
{ input: "N", expected: false }, | ||
// Yes in key | ||
{ input: "yes: value", expected: { true: "value" } }, | ||
// No in key | ||
{ input: "no: value", expected: { false: "value" } }, | ||
// Yes in value | ||
{ input: "key: yes", expected: { key: true } }, | ||
// No in value | ||
{ input: "key: no", expected: { key: false } } | ||
])("boolean: $input -> $expected", ({ input, expected }) => { | ||
expect(parse(input)).toEqual([expected]); | ||
}); | ||
|
||
test("anchor", () => { | ||
expect( | ||
parse(` | ||
a: &anchor | ||
foo: bar | ||
b: *anchor | ||
`) | ||
).toEqual([{ a: { foo: "bar" }, b: { foo: "bar" } }]); | ||
}); | ||
|
||
test("map merging", () => { | ||
expect( | ||
parse(` | ||
a: &src-a | ||
a: 1 | ||
b: &src-b | ||
b: 2 | ||
result: | ||
<<: [*src-a, *src-b] | ||
`) | ||
).toEqual([{ a: { a: 1 }, b: { b: 2 }, result: { a: 1, b: 2 } }]); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { parseAllDocuments, visit } from "yaml"; | ||
|
||
const reLegacyOctNum = /^0[0-7]+$/; | ||
|
||
// Match all-lowercase, all-uppercase, and title-case variants | ||
const legacyTrues = new Set(["y", "Y", "yes", "Yes", "YES", "on", "On", "ON"]); | ||
const legacyFalses = new Set(["n", "N", "no", "No", "NO", "off", "Off", "OFF"]); | ||
|
||
/** | ||
* The purpose of this function is to match the behavior of [sigs.k8s.io/yaml] | ||
* as closely as possible. | ||
* | ||
* [sigs.k8s.io/yaml] uses [go-yaml](https://github.com/go-yaml/yaml) under the | ||
* hood, which supports most of YAML 1.2, but preserves some YAML 1.1 behaviors | ||
* for backwards compatibility. You can see [here](https://github.com/go-yaml/yaml?tab=readme-ov-file#compatibility) | ||
* for more information. | ||
* | ||
* [sigs.k8s.io/yaml]: https://pkg.go.dev/sigs.k8s.io/yaml | ||
* [go-yaml]: https://github.com/go-yaml/yaml | ||
*/ | ||
export function parse(content: string): unknown[] { | ||
const documents = parseAllDocuments(content, { | ||
// Enable map merging | ||
merge: true | ||
}); | ||
const results = []; | ||
|
||
for (const doc of documents) { | ||
visit(doc, { | ||
Scalar(key, node) { | ||
if (node.type !== "PLAIN" || !node.source) return; | ||
|
||
// Handle YAML 1.1 octal numbers | ||
if ( | ||
typeof node.value === "number" && | ||
reLegacyOctNum.test(node.source) | ||
) { | ||
node.value = parseInt(node.source, 8); | ||
} | ||
|
||
// Handle YAML 1.1 booleans | ||
if (typeof node.value === "string") { | ||
if (legacyTrues.has(node.source)) { | ||
node.value = true; | ||
} else if (legacyFalses.has(node.source)) { | ||
node.value = false; | ||
} | ||
} | ||
} | ||
}); | ||
|
||
results.push(doc.toJSON()); | ||
} | ||
|
||
return results; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.