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.
This commit is contained in:
Kim Alvefur 2023-08-03 19:38:20 +02:00
parent 096497cdfc
commit ba90b795da
2 changed files with 8 additions and 3 deletions

View File

@ -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":

View File

@ -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",
},
{