Skip to content

Commit

Permalink
Output line comments in Lua output
Browse files Browse the repository at this point in the history
  • Loading branch information
Zash committed Aug 5, 2023
1 parent 982acdb commit 69265f1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pkg/yqlib/doc/usage/lua.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ yq -o=lua '.' sample.yml
will output
```lua
return {
["country"] = "Australia";
["country"] = "Australia"; -- this place
["cities"] = {
"Sydney",
"Melbourne",
Expand Down Expand Up @@ -47,7 +47,7 @@ yq -o=lua '.' sample.yml
will output
```lua
return {
country = "Australia";
country = "Australia"; -- this place
cities = {
"Sydney",
"Melbourne",
Expand Down
21 changes: 21 additions & 0 deletions pkg/yqlib/encoder_lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions pkg/yqlib/lua_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ cities:
- Melbourne
- Brisbane
- Perth`,
// TODO comments, -- this place
expected: `return {
["country"] = "Australia";
["country"] = "Australia"; -- this place
["cities"] = {
"Sydney",
"Melbourne",
Expand All @@ -43,7 +42,7 @@ cities:
- Brisbane
- Perth`,
expected: `return {
country = "Australia";
country = "Australia"; -- this place
cities = {
"Sydney",
"Melbourne",
Expand Down

0 comments on commit 69265f1

Please sign in to comment.