pelletier / go-toml

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

key is already defined error when key with same key name is used inside a different table #914

Closed akhilerm closed 1 year ago

akhilerm commented 1 year ago

Describe the bug When there are 2 keys with same name, one inside a table and one outside, based on the order of the outer key , the parsing will pass or fail. If the outer key is written immediately after the table, the parsing fails.

To Reproduce Consider the following struct definition

type release struct {
    ProjectName  string                        `toml:"project_name"`
    Previous     string                        `toml:"previous"`
    OverrideDeps map[string]dependencyOverride `toml:"override_deps"`
}

type dependencyOverride struct {
    Previous string `toml:"previous"`
}

Expected behavior Irrespective of where the key previous is present in the outer table, the parsing should succeed.

Versions

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

pelletier commented 1 year ago

This is a actually a TOML issue. In this format, indentation doesn't mean anything. In your second example, the document looks like this:

project_name="containerd"
[override_deps]
[override_deps."github.com/containerd/log"]
previous="github.com/containerd/containerd"
previous="v1.26.4"

In TOML, all keys defined after a table belong to that table, until end of file or the next table. So in this case, the table override_deps."github.com/containerd/log" does indeed have the key previous twice.

akhilerm commented 1 year ago

Got it. Thank you for the explanation. I will proceed with closing the issue.