mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-10 08:35:42 +00:00
Output line comments in Lua output
This commit is contained in:
parent
e756ac2c5a
commit
b839c0ea92
@ -17,7 +17,7 @@ yq -o=lua '.' sample.yml
|
||||
will output
|
||||
```lua
|
||||
return {
|
||||
["country"] = "Australia";
|
||||
["country"] = "Australia"; -- this place
|
||||
["cities"] = {
|
||||
"Sydney",
|
||||
"Melbourne",
|
||||
@ -47,7 +47,7 @@ yq -o=lua '.' sample.yml
|
||||
will output
|
||||
```lua
|
||||
return {
|
||||
country = "Australia";
|
||||
country = "Australia"; -- this place
|
||||
cities = {
|
||||
"Sydney",
|
||||
"Melbourne",
|
||||
|
||||
@ -134,6 +134,13 @@ func (le *luaEncoder) encodeArray(writer io.Writer, node *yaml.Node) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if child.LineComment != "" {
|
||||
sansPrefix, _ := strings.CutPrefix(child.LineComment, "#")
|
||||
err = writeString(writer, " --"+sansPrefix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
le.indent--
|
||||
if len(node.Content) != 0 {
|
||||
@ -214,6 +221,20 @@ func (le *luaEncoder) encodeMap(writer io.Writer, node *yaml.Node) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
if child.LineComment != "" {
|
||||
sansPrefix, _ := strings.CutPrefix(child.LineComment, "#")
|
||||
err = writeString(writer, strings.Repeat(" ", i%2)+"--"+sansPrefix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if (i % 2) == 0 {
|
||||
// newline and indent after comments on keys
|
||||
err = le.writeIndent(writer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
le.indent--
|
||||
if len(node.Content) != 0 {
|
||||
|
||||
@ -19,9 +19,8 @@ cities:
|
||||
- Melbourne
|
||||
- Brisbane
|
||||
- Perth`,
|
||||
// TODO comments, -- this place
|
||||
expected: `return {
|
||||
["country"] = "Australia";
|
||||
["country"] = "Australia"; -- this place
|
||||
["cities"] = {
|
||||
"Sydney",
|
||||
"Melbourne",
|
||||
@ -43,7 +42,7 @@ cities:
|
||||
- Brisbane
|
||||
- Perth`,
|
||||
expected: `return {
|
||||
country = "Australia";
|
||||
country = "Australia"; -- this place
|
||||
cities = {
|
||||
"Sydney",
|
||||
"Melbourne",
|
||||
|
||||
Loading…
Reference in New Issue
Block a user