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

Strict parsing #197

Closed
ghost opened this issue Oct 13, 2017 · 1 comment
Closed

Strict parsing #197

ghost opened this issue Oct 13, 2017 · 1 comment

Comments

@ghost
Copy link

ghost commented Oct 13, 2017

Is there a way to instruct toml to fail if any key=value pair associated to a struct field is not present in the config file?

The reason I ask is because toml.DecodeFile() does not warn you when a key=value pair is missing, and using md.IsDefined() for each key is not mantainable in large config files.

If the feature is not present, would you consider adding it in the future? Having guarantees about the config file completeness is a big deal.

arp242 added a commit that referenced this issue Jun 20, 2021
Refactor things a wee bit to add a decoder struct. This doesn't really
*do* anything right now, but will be needed to add options, such as
"strict mode" in #197 or "extended errors" in #299.

Also add DecodeFS() as a companion to DecodeFile() to read from a fs.FS,
and since using a reader is the default with NewDecoder() (and was
already easy to do before with strings.NewReader()) I marked
DecodeReader() as deprecated.
@arp242
Copy link
Collaborator

arp242 commented Jun 24, 2021

I looked a bit at this, and adding this is possible but non-trivial.

I'm also not sure how common this use case necessarily is; certainly most config files I'm familiar with don't need every single key to be set, if there are keys that are required to be set then it's usually only a subset. And should we could all exported struct fields or only those with a toml tag set? Different people probably want different things.

And most importantly: this is a use case that can already be solved by checking meta.Keys(), which lists all keys that were encountered in the TOML file. For example:

var s struct {
	A int `toml:"a"`
}
file := `
	a = 42
	[tbl]
	a = 43`
meta, _ := toml.Decode(file, &s)
fmt.Printf("Keys: %q; Undecoded: %q\n", meta.Keys(), meta.Undecoded())

Prints:

Keys: ["a" "tbl" "tbl.a"]; Undecoded: ["tbl" "tbl.a"]

You can check if Keys() contains what you want. This is flexible for all use cases, and pretty much the reason the Meta object exists.

Unless there are serious practical problems with this, I think adding this is too much effort for too little benefit. But feel free to comment with more information/use cases and we can look and possibly reopen.

@arp242 arp242 closed this as completed Jun 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant