mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 13:48:06 +00:00
Lua output fixes (#1811)
* encoder_lua: Handle explicitly positive infinity * 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. * encoder_lua: Improve Tag handling robustness Using the method call seems more reliable in case the input parser forgets to set the tag.
This commit is contained in:
parent
610896e5ed
commit
7c78a15b23
@ -271,4 +271,43 @@ EOM
|
|||||||
assertEquals "$expected" "$X"
|
assertEquals "$expected" "$X"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
testLuaOutputPretty() {
|
||||||
|
cat >test.yml <<EOL
|
||||||
|
animals:
|
||||||
|
cat: meow
|
||||||
|
EOL
|
||||||
|
|
||||||
|
read -r -d '' expected << EOM
|
||||||
|
return {
|
||||||
|
["animals"] = {
|
||||||
|
["cat"] = "meow";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
EOM
|
||||||
|
|
||||||
|
X=$(./yq e --output-format=lua test.yml)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
|
||||||
|
X=$(./yq e --output-format=lua --prettyPrint test.yml)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
testLuaOutputSubset() {
|
||||||
|
cat >test.yml <<EOL
|
||||||
|
animals:
|
||||||
|
cat: meow
|
||||||
|
EOL
|
||||||
|
|
||||||
|
read -r -d '' expected << EOM
|
||||||
|
return {
|
||||||
|
["cat"] = "meow";
|
||||||
|
};
|
||||||
|
EOM
|
||||||
|
|
||||||
|
X=$(./yq e --output-format=lua '.animals' test.yml)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
source ./scripts/shunit2
|
source ./scripts/shunit2
|
@ -129,6 +129,8 @@ numbers:
|
|||||||
- octal: 0o30
|
- octal: 0o30
|
||||||
- float: 123.45
|
- float: 123.45
|
||||||
- infinity: .inf
|
- infinity: .inf
|
||||||
|
plus_infinity: +.inf
|
||||||
|
minus_infinity: -.inf
|
||||||
- not: .nan
|
- not: .nan
|
||||||
|
|
||||||
```
|
```
|
||||||
@ -162,6 +164,8 @@ return {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
["infinity"] = (1/0);
|
["infinity"] = (1/0);
|
||||||
|
["plus_infinity"] = (1/0);
|
||||||
|
["minus_infinity"] = (-1/0);
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
["not"] = (0/0);
|
["not"] = (0/0);
|
||||||
|
@ -129,7 +129,7 @@ func (le *luaEncoder) encodeArray(writer io.Writer, node *yaml.Node) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err := le.Encode(writer, child)
|
err := le.encodeAny(writer, child)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -192,7 +192,7 @@ func (le *luaEncoder) encodeMap(writer io.Writer, node *yaml.Node, global bool)
|
|||||||
for i, child := range node.Content {
|
for i, child := range node.Content {
|
||||||
if (i % 2) == 1 {
|
if (i % 2) == 1 {
|
||||||
// value
|
// value
|
||||||
err := le.Encode(writer, child)
|
err := le.encodeAny(writer, child)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -270,7 +270,7 @@ func (le *luaEncoder) encodeAny(writer io.Writer, node *yaml.Node) error {
|
|||||||
case yaml.MappingNode:
|
case yaml.MappingNode:
|
||||||
return le.encodeMap(writer, node, false)
|
return le.encodeMap(writer, node, false)
|
||||||
case yaml.ScalarNode:
|
case yaml.ScalarNode:
|
||||||
switch node.Tag {
|
switch node.ShortTag() {
|
||||||
case "!!str":
|
case "!!str":
|
||||||
return le.encodeString(writer, node)
|
return le.encodeString(writer, node)
|
||||||
case "!!null":
|
case "!!null":
|
||||||
@ -292,7 +292,7 @@ func (le *luaEncoder) encodeAny(writer io.Writer, node *yaml.Node) error {
|
|||||||
return writeString(writer, strings.ToLower(node.Value))
|
return writeString(writer, strings.ToLower(node.Value))
|
||||||
case "!!float":
|
case "!!float":
|
||||||
switch strings.ToLower(node.Value) {
|
switch strings.ToLower(node.Value) {
|
||||||
case ".inf":
|
case ".inf", "+.inf":
|
||||||
return writeString(writer, "(1/0)")
|
return writeString(writer, "(1/0)")
|
||||||
case "-.inf":
|
case "-.inf":
|
||||||
return writeString(writer, "(-1/0)")
|
return writeString(writer, "(-1/0)")
|
||||||
@ -305,26 +305,33 @@ func (le *luaEncoder) encodeAny(writer io.Writer, node *yaml.Node) error {
|
|||||||
return fmt.Errorf("Lua encoder NYI -- %s", node.ShortTag())
|
return fmt.Errorf("Lua encoder NYI -- %s", node.ShortTag())
|
||||||
}
|
}
|
||||||
case yaml.DocumentNode:
|
case yaml.DocumentNode:
|
||||||
if le.globals {
|
return le.encodeAny(writer, node.Content[0])
|
||||||
if node.Content[0].Kind != yaml.MappingNode {
|
|
||||||
return fmt.Errorf("--lua-global requires a top level MappingNode")
|
|
||||||
}
|
|
||||||
return le.encodeMap(writer, node.Content[0], true)
|
|
||||||
}
|
|
||||||
err := writeString(writer, le.docPrefix)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = le.encodeAny(writer, node.Content[0])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return writeString(writer, le.docSuffix)
|
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("Lua encoder NYI -- %s", node.ShortTag())
|
return fmt.Errorf("Lua encoder NYI -- %s", node.ShortTag())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (le *luaEncoder) Encode(writer io.Writer, node *yaml.Node) error {
|
func (le *luaEncoder) encodeTopLevel(writer io.Writer, node *yaml.Node) error {
|
||||||
return le.encodeAny(writer, node)
|
err := writeString(writer, le.docPrefix)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = le.encodeAny(writer, node)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return writeString(writer, le.docSuffix)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (le *luaEncoder) Encode(writer io.Writer, node *yaml.Node) error {
|
||||||
|
if node.Kind == yaml.DocumentNode {
|
||||||
|
return le.Encode(writer, node.Content[0])
|
||||||
|
}
|
||||||
|
if le.globals {
|
||||||
|
if node.Kind != yaml.MappingNode {
|
||||||
|
return fmt.Errorf("--lua-global requires a top level MappingNode")
|
||||||
|
}
|
||||||
|
return le.encodeMap(writer, node, true)
|
||||||
|
}
|
||||||
|
return le.encodeTopLevel(writer, node)
|
||||||
}
|
}
|
||||||
|
@ -135,6 +135,8 @@ numbers:
|
|||||||
- octal: 0o30
|
- octal: 0o30
|
||||||
- float: 123.45
|
- float: 123.45
|
||||||
- infinity: .inf
|
- infinity: .inf
|
||||||
|
plus_infinity: +.inf
|
||||||
|
minus_infinity: -.inf
|
||||||
- not: .nan
|
- not: .nan
|
||||||
`,
|
`,
|
||||||
expected: `return {
|
expected: `return {
|
||||||
@ -161,6 +163,8 @@ numbers:
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
["infinity"] = (1/0);
|
["infinity"] = (1/0);
|
||||||
|
["plus_infinity"] = (1/0);
|
||||||
|
["minus_infinity"] = (-1/0);
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
["not"] = (0/0);
|
["not"] = (0/0);
|
||||||
|
Loading…
Reference in New Issue
Block a user