From 8f6d57f58eb7035dbeb8009bbf030b3b3587226b Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 4 Oct 2023 12:08:14 +0200 Subject: [PATCH 1/3] encoder_lua: Handle explicitly positive infinity --- pkg/yqlib/doc/usage/lua.md | 4 ++++ pkg/yqlib/encoder_lua.go | 2 +- pkg/yqlib/lua_test.go | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/yqlib/doc/usage/lua.md b/pkg/yqlib/doc/usage/lua.md index d6f067e48c..6b6dcc8b49 100644 --- a/pkg/yqlib/doc/usage/lua.md +++ b/pkg/yqlib/doc/usage/lua.md @@ -129,6 +129,8 @@ numbers: - octal: 0o30 - float: 123.45 - infinity: .inf + plus_infinity: +.inf + minus_infinity: -.inf - not: .nan ``` @@ -162,6 +164,8 @@ return { }, { ["infinity"] = (1/0); + ["plus_infinity"] = (1/0); + ["minus_infinity"] = (-1/0); }, { ["not"] = (0/0); diff --git a/pkg/yqlib/encoder_lua.go b/pkg/yqlib/encoder_lua.go index d1bb81aa17..8efa18895f 100644 --- a/pkg/yqlib/encoder_lua.go +++ b/pkg/yqlib/encoder_lua.go @@ -292,7 +292,7 @@ func (le *luaEncoder) encodeAny(writer io.Writer, node *yaml.Node) error { return writeString(writer, strings.ToLower(node.Value)) case "!!float": switch strings.ToLower(node.Value) { - case ".inf": + case ".inf", "+.inf": return writeString(writer, "(1/0)") case "-.inf": return writeString(writer, "(-1/0)") diff --git a/pkg/yqlib/lua_test.go b/pkg/yqlib/lua_test.go index ddcbd023cc..bb36e9cc5a 100644 --- a/pkg/yqlib/lua_test.go +++ b/pkg/yqlib/lua_test.go @@ -135,6 +135,8 @@ numbers: - octal: 0o30 - float: 123.45 - infinity: .inf + plus_infinity: +.inf + minus_infinity: -.inf - not: .nan `, expected: `return { @@ -161,6 +163,8 @@ numbers: }, { ["infinity"] = (1/0); + ["plus_infinity"] = (1/0); + ["minus_infinity"] = (-1/0); }, { ["not"] = (0/0); From e709a583f42079ea65552f713ecd66450bb7c92d Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 4 Oct 2023 12:28:47 +0200 Subject: [PATCH 2/3] encoder_lua: Fix inclusion of pre-/suffix when prettyPrinted It seems certain operations like --prettyPrint or subset selections does not produce a DocumentNode, which is where the lua pre- and suffix was printed, causing those to be omitted. --- acceptance_tests/output-format.sh | 41 ++++++++++++++++++++++++++++- pkg/yqlib/encoder_lua.go | 43 ++++++++++++++++++------------- 2 files changed, 65 insertions(+), 19 deletions(-) diff --git a/acceptance_tests/output-format.sh b/acceptance_tests/output-format.sh index 268185d403..4e9ad325da 100755 --- a/acceptance_tests/output-format.sh +++ b/acceptance_tests/output-format.sh @@ -271,4 +271,43 @@ EOM assertEquals "$expected" "$X" } -source ./scripts/shunit2 \ No newline at end of file +testLuaOutputPretty() { + cat >test.yml <test.yml < Date: Wed, 4 Oct 2023 14:10:39 +0200 Subject: [PATCH 3/3] encoder_lua: Improve Tag handling robustness Using the method call seems more reliable in case the input parser forgets to set the tag. --- pkg/yqlib/encoder_lua.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/yqlib/encoder_lua.go b/pkg/yqlib/encoder_lua.go index e83687d85d..c1ac1574d6 100644 --- a/pkg/yqlib/encoder_lua.go +++ b/pkg/yqlib/encoder_lua.go @@ -270,7 +270,7 @@ func (le *luaEncoder) encodeAny(writer io.Writer, node *yaml.Node) error { case yaml.MappingNode: return le.encodeMap(writer, node, false) case yaml.ScalarNode: - switch node.Tag { + switch node.ShortTag() { case "!!str": return le.encodeString(writer, node) case "!!null":