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

Decode: improve errors on integers and strings #891

Merged
merged 3 commits into from Aug 28, 2023
Merged

Decode: improve errors on integers and strings #891

merged 3 commits into from Aug 28, 2023

Conversation

pelletier
Copy link
Owner

Add struct field location and context to errors reported when trying to unmarshal a string or integer into an inappropriate Go type.

Given this program:

type Server struct {
        Path string
        Port int
}

type Cfg struct {
        Server Server
}

var cfg Cfg
data := `[server]
path = "/my/path"
port = "bad"`

file := strings.NewReader(data)
err := toml.NewDecoder(file).Decode(&cfg)
fmt.Println(err)
fmt.Println(err.(*toml.DecodeError).String()) // crashes on main

Output before patch:

toml: cannot store TOML string into a Go int

Output after patch:

toml: cannot decode TOML string into struct field toml_test.Server.Port of type int
1| [server]
2| path = "/my/path"
3| port = "bad"
 |        ~~~~~ cannot decode TOML string into struct field toml_test.Server.Port of type int

Based on #890

@pelletier pelletier added the bug Issues describing a bug in go-toml. label Aug 28, 2023
Base automatically changed from fix-raw-integers to v2 August 28, 2023 15:02
@pelletier pelletier changed the title Decode: add location to errors of ints and strings Decode: improve errors on integers and strings Aug 28, 2023
@pelletier pelletier merged commit 4835627 into v2 Aug 28, 2023
9 checks passed
@pelletier pelletier deleted the ints-err-loc branch August 28, 2023 15:17
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 this pull request may close these issues.

None yet

1 participant