Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoder ignores table indentation on array tables #888

Closed
pelletier opened this issue Aug 25, 2023 · 4 comments · Fixed by #889
Closed

Encoder ignores table indentation on array tables #888

pelletier opened this issue Aug 25, 2023 · 4 comments · Fixed by #889
Labels
bug Issues describing a bug in go-toml.

Comments

@pelletier
Copy link
Owner

Describe the bug

Follow up from #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

  • go-toml: v2.0.9
  • go: 1.2.1
  • operating system: any
@pelletier
Copy link
Owner Author

@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
Copy link
Contributor

Felixoid commented Aug 28, 2023

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 61d5d11

Felixoid added a commit to Felixoid/go-toml that referenced this issue Aug 28, 2023
@pelletier
Copy link
Owner Author

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

@Felixoid
Copy link
Contributor

Looks like an easy fix, #892

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issues describing a bug in go-toml.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants