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

Incorrect indentation for arrays of custom types #943

Closed
daniel-weisse opened this issue Apr 12, 2024 · 1 comment · Fixed by #944
Closed

Incorrect indentation for arrays of custom types #943

daniel-weisse opened this issue Apr 12, 2024 · 1 comment · Fixed by #944
Labels
bug Issues describing a bug in go-toml.

Comments

@daniel-weisse
Copy link
Contributor

Describe the bug
Using the SetIndentTables(true) option does not correctly indent keys of arrays of custom types.
It does work correctly if the custom type is not a slice.
Example:
If I have some data structure a.b.c I expect the generated TOML to look like the following:

# comment A
[A]
  # comment B
  [A.B]
    # comment C
    [A.B.C]

Assuming c is an array I would expect the following:

# comment A
[A]
  # comment B
  [A.B]
    # comment C
    [[A.B.C]]
      # Entry 1
    [[A.B.C]]
      # Entry 2

Instead, go-toml gives me something like the following:

# comment A
[A]
  # comment B
  [A.B]
    # comment C
[[A.B.C]]
      # Entry 1
[[A.B.C]]
      # Entry 2

To Reproduce

package main

import (
	"bytes"
	"fmt"

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

func main() {
	cfg := layer0{
		Value: "value",
		Layer1: []layer1{
			{
				Value: "value",
				Layer2: []layer2{
					{
						Value: "value",
						Layer3: []layer3{
							{
								Value: "value",
								CustomType: customType{
									Value: "value",
								},
							},
						},
					},
				},
			},
		},
	}

	buf := bytes.Buffer{}
	enc := toml.NewEncoder(&buf).
		SetIndentTables(true).
		SetIndentSymbol("+") // Using "+" as indent symbol for easier comparison
	if err := enc.Encode(cfg); err != nil {
		panic(err)
	}

	fmt.Println(buf.String())
}

type layer0 struct {
	Value  string   `toml:"value" comment:"value comment"`
	Layer1 []layer1 `toml:"layer1" comment:"layer1 comment"`
}

type layer1 struct {
	Value  string   `toml:"value" comment:"value comment"`
	Layer2 []layer2 `toml:"layer2" comment:"layer2 comment"`
}

type layer2 struct {
	Value  string   `toml:"value" comment:"value comment"`
	Layer3 []layer3 `toml:"layer3" comment:"layer3 comment"`
}

type layer3 struct {
	Value      string     `toml:"value" comment:"value comment"`
	CustomType customType `toml:"customType" comment:"customType comment"`
}

type customType struct {
	Value string `toml:"value" comment:"value comment"`
}

Expected behavior
I'm expecting the generated TOML to look like the following, correctly indenting layer1.layer2 with + and layer1.layer2.layer3 with two ++:

$ go run ./
# value comment
value = 'value'

# layer1 comment
[[layer1]]
+# value comment
+value = 'value'

+# layer2 comment
+[[layer1.layer2]]
++# value comment
++value = 'value'

++# layer3 comment
++[[layer1.layer2.layer3]]
+++# value comment
+++value = 'value'

+++# customType comment
+++[layer1.layer2.layer3.customType]
++++# value comment
++++value = 'value'


In practice, only the comments for layer1.layer2 and layer1.layer2.layer3 are indented, the keys are not:

$ go run ./
# value comment
value = 'value'

# layer1 comment
[[layer1]]
+# value comment
+value = 'value'

+# layer2 comment
[[layer1.layer2]]
++# value comment
++value = 'value'

++# layer3 comment
[[layer1.layer2.layer3]]
+++# value comment
+++value = 'value'

+++# customType comment
+++[layer1.layer2.layer3.customType]
++++# value comment
++++value = 'value'


Versions

  • go-toml: v2.2.0
  • go: 1.22.1
  • operating system: Linux

Additional context
Add any other context about the problem here that you think may help to diagnose.

@pelletier
Copy link
Owner

Thank you for the detailed issue report!

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