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

Allow ConfigParseError to unwrap #1433

Merged
merged 3 commits into from May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion util.go
Expand Up @@ -27,10 +27,15 @@ type ConfigParseError struct {
}

// Error returns the formatted configuration error.
func (pe ConfigParseError) Error() string {
func (pe *ConfigParseError) Error() string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why make it a pointer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly I don't remember right now. I came from yaml issue referenced above and had some restriction with non-pointer values. Started discussion in https://groups.google.com/u/1/g/golang-nuts/c/3M3QDkochcs/m/uo-x6jA0BgAJ which concluded in

however i think it is a good default position to use pointer values to
implement interfaces generally, if there is no other good reason not to.

If you're not happy with pointer type I'll need to recreate the entire case and try to find out what the root cause issue with non-pointer types was.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I'm not happy doing it unless I understand why it's necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have reverted the pointer type and added a test- it does unwrap as expected.

return fmt.Sprintf("While parsing config: %s", pe.err.Error())
}

// Unwrap returns the wrapped error.
func (pe *ConfigParseError) Unwrap() error {
return pe.err
}

// toCaseInsensitiveValue checks if the value is a map;
// if so, create a copy and lower-case the keys recursively.
func toCaseInsensitiveValue(value interface{}) interface{} {
Expand Down
2 changes: 1 addition & 1 deletion viper.go
Expand Up @@ -1700,7 +1700,7 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error {
case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini", "properties", "props", "prop", "dotenv", "env":
err := v.decoderRegistry.Decode(format, buf.Bytes(), c)
if err != nil {
return ConfigParseError{err}
return &ConfigParseError{err}
}
}

Expand Down