pelletier / go-toml

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

Wrong marshaling map #836

Closed irbgeo closed 1 year ago

irbgeo commented 1 year ago

When marshaling a nested map an invalid toml file is generated

Reproduce

var testData map[string]any = map[string]any{
    "plugins": map[string]any{
        "io.containerd.grpc.v1.cri": map[string]any{
            "containerd": map[string]any{
                "runtimes": map[string]any{
                    "runc": map[string]any{
                        "options": map[string]any{
                            "SystemdCgroup": true,
                        },
                        "runtime_type": "io.containerd.runc.v2",
                    },
                },
            },
        },
    },
    "version": 2,
}

// v2 "github.com/pelletier/go-toml/v2"
data, _ = v2.Marshal(testData)
fmt.Printf("%s\n", data)

Actual result

version = 2

[plugins]
[plugins.'io.containerd.grpc.v1.cri']
[plugins.'io.containerd.grpc.v1.cri'.containerd]
[plugins.'io.containerd.grpc.v1.cri'.containerd.runtimes]
[plugins.'io.containerd.grpc.v1.cri'.containerd.runtimes.runc]
runtime_type = 'io.containerd.runc.v2'

[plugins.'io.containerd.grpc.v1.cri'.containerd.runtimes.runc.options]
SystemdCgroup = true

Expected result

version = 2

[plugins]

  [plugins."io.containerd.grpc.v1.cri"]

    [plugins."io.containerd.grpc.v1.cri".containerd]

      [plugins."io.containerd.grpc.v1.cri".containerd.runtimes]

        [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
          runtime_type = "io.containerd.runc.v2"

          [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
            SystemdCgroup = true

Versions

FR-Solution commented 1 year ago

+1

pelletier commented 1 year ago

Can you expand on what makes the emitted output "invalid"?

Running this test data through marshal and unmarshal seems to result in the same initial data: https://go.dev/play/p/H500SrxNO-t

If your issue is the lack of indentation, you can achieve this using an Encoder and the option SetIndentTables:

e := toml.NewEncoder(os.Stdout)
e.SetIndentTables(true)
e.Encode(testData)

https://go.dev/play/p/0OPYX7b8wrw

irbgeo commented 1 year ago

@pelletier

Can you expand on what makes the emitted output "invalid"? I was expecting indents in my example, thanks for providing a working version,thanks

P.S. I think it would be right to correct the behavior of the standard application of your package