From ba90b795da6224ef33d058f1cd0903d5f3076e2b Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Thu, 3 Aug 2023 19:38:20 +0200 Subject: [PATCH] Convert octal for Lua output Lua doesn't have the 0oNNN syntax for octal integers, only decimal and hexadecimal, hence those can be passed trough as is while octal needs special treatment. --- pkg/yqlib/encoder_lua.go | 7 ++++++- pkg/yqlib/lua_test.go | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/pkg/yqlib/encoder_lua.go b/pkg/yqlib/encoder_lua.go index 8ed11d28..fc1fe3c5 100644 --- a/pkg/yqlib/encoder_lua.go +++ b/pkg/yqlib/encoder_lua.go @@ -184,7 +184,12 @@ func (le *luaEncoder) encodeAny(writer io.Writer, node *yaml.Node) error { return writeString(writer, strings.ToLower(node.Value)) case "!!int": if strings.HasPrefix(node.Value, "0o") { - return fmt.Errorf("Lua encoder NYI -- octal") // FIXME + var octalValue int + err := node.Decode(&octalValue) + if err != nil { + return err + } + return writeString(writer, fmt.Sprintf("%d", octalValue)) } return writeString(writer, strings.ToLower(node.Value)) case "!!float": diff --git a/pkg/yqlib/lua_test.go b/pkg/yqlib/lua_test.go index 3cf28ff2..de7e946f 100644 --- a/pkg/yqlib/lua_test.go +++ b/pkg/yqlib/lua_test.go @@ -46,8 +46,8 @@ var luaScenarios = []formatScenario{ { skipDoc: true, description: "Scalar int", - input: "- 1\n- 2\n- 0x10\n- -999\n", - expected: "return {1,2,0x10,-999,};\n", + input: "- 1\n- 2\n- 0x10\n- 0o30\n- -999\n", + expected: "return {1,2,0x10,24,-999,};\n", scenarioType: "encode", }, {