pelletier / go-toml

Go library for the TOML file format
https://github.com/pelletier/go-toml
Other
1.72k stars 209 forks source link

Encoder ignores table indentation on array tables #888

Closed pelletier closed 1 year ago

pelletier commented 1 year ago

Describe the bug

Follow up from https://github.com/pelletier/go-toml/issues/786. In https://go.dev/play/p/i30ktekXzYD, array tables are not indented even though the option has been enabled.

To Reproduce Steps to reproduce the behavior. Including TOML files.

package main

import (
    "bytes"
    "fmt"

    "github.com/pelletier/go-toml/v2"
)

type Thing struct {
    FieldA string
    FieldB string
}

type Cfg struct {
    Custom []Thing
}

func main() {
    buf := new(bytes.Buffer)

    config := Cfg{
        Custom: []Thing{
            Thing{FieldA: "field a 1", FieldB: "field b 1"},
            Thing{FieldA: "field a 2", FieldB: "field b 2"},
        },
    }

    encoder := toml.NewEncoder(buf).SetIndentTables(true)
    encoder.Encode(config)
    fmt.Print(buf.String())
}

https://go.dev/play/p/EjEVbGp7xKD

Expected behavior

Output should be:

[[Custom]]
  FieldA = 'field a 1'
  FieldB = 'field b 1'

[[Custom]]
  FieldA = 'field a 2'
  FieldB = 'field b 2'

Versions

pelletier commented 1 year ago

@Felixoid merged a patch for this. It should now indent array tables as you expect. Feel free to reopen and let me know if it doesn't work for you!

Felixoid commented 1 year ago

Thanks, it works.

But do you think it's an error or a new design? Looks strange to me, and feels like the comment # custom config should be aligned with [[

> git diff
diff --git a/marshaler_test.go b/marshaler_test.go
index b4b8e28..514c885 100644
--- a/marshaler_test.go
+++ b/marshaler_test.go
@@ -1208,7 +1208,7 @@ func TestMarhsalIssue888(t *testing.T) {
        }

        type Cfg struct {
-               Custom []Thing
+               Custom []Thing `comment:"custom config"`
        }

        buf := new(bytes.Buffer)
@@ -1223,7 +1223,8 @@ func TestMarhsalIssue888(t *testing.T) {
        encoder := toml.NewEncoder(buf).SetIndentTables(true)
        encoder.Encode(config)

-       expected := `[[Custom]]
+       expected := `# custom config
+[[Custom]]
   # my field A
   FieldA = 'field a 1'
   # my field B
> go test ./marshaler_test.go  -test.run TestMarhsalIssue888
--- FAIL: TestMarhsalIssue888 (0.00s)
    marshaler_test.go:1240:
                Error Trace:    /home/felixoid/Space/Felixoid/github/pelletier/go-toml/marshaler_test.go:1240
                Error:          Not equal:
                                expected: "# custom config\n[[Custom]]\n  # my field A\n  FieldA = 'field a 1'\n  # my field B\n  FieldB = 'field b 1'\n\n[[Custom]]\n  # my field A\n  FieldA = 'field a 2'\n  # my field B\n  FieldB = 'field b 2'\n"
                                actual  : "  # custom config\n[[Custom]]\n  # my field A\n  FieldA = 'field a 1'\n  # my field B\n  FieldB = 'field b 1'\n\n[[Custom]]\n  # my field A\n  FieldA = 'field a 2'\n  # my field B\n  FieldB = 'field b 2'\n"

                                Diff:
                                --- Expected
                                +++ Actual
                                @@ -1,2 +1,2 @@
                                -# custom config
                                +  # custom config
                                 [[Custom]]
                Test:           TestMarhsalIssue888
FAIL

The code is in https://github.com/pelletier/go-toml/commit/61d5d1127c793eb49113bb89ea0eff50077a8319

pelletier commented 1 year ago

You're right it seems wrong. I think the comment decorating the array table should be aligned with it.

Felixoid commented 1 year ago

Looks like an easy fix, https://github.com/pelletier/go-toml/pull/892