Skip to content

Feature allow unset pointer #80

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

Merged
merged 2 commits into from
Jun 16, 2025

Conversation

rostislaved
Copy link

Description

This pull request adds a config flag AllowUnsetPointer to allow pointer fields in structs to be treated as optional during decoding. When enabled, missing pointer fields will not be reported as unset even if ErrorUnset is true.

This is useful when decoding into structs with optional pointer fields, while still maintaining strict handling (ErrorUnset) for required non-pointer fields.


Current behavior example

type Config struct {
	URL  *string
	Port int
}

input := map[string]any{
	"Port": 8080,
}

var cfg Config

dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
	ErrorUnset: true,
	Result:     &cfg,
})
if err != nil {
	panic(err)
}

err = dec.Decode(input)
// err is not nil: "has unset fields: URL"

New behavior with AllowUnsetPointer: true

type Config struct {
	URL  *string
	Port int
}

input := map[string]any{
	"Port": 8080,
}

var cfg Config

dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
	ErrorUnset:        true,
	AllowUnsetPointer: true,
	Result:            &cfg,
})
if err != nil {
	panic(err)
}

err = dec.Decode(input)
// err is nil
// cfg.URL == nil
// cfg.Port == 8080
Comment on lines +1470 to +1472
if !(d.config.AllowUnsetPointer && fieldValue.Kind() == reflect.Ptr) {
targetValKeysUnused[fieldName] = struct{}{}
}
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be more elegant to continue here and leave setting the map after the condition.

WDYT?

Copy link
Author

Choose a reason for hiding this comment

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

Hi! Did you mean like this?

		if !rawMapVal.IsValid() {
				// There was no matching key in the map for the value in
				// the struct. Remember it for potential errors and metadata.
				if !(d.config.AllowUnsetPointer && fieldValue.Kind() == reflect.Ptr) {
					targetValKeysUnused[fieldName] = struct{}{}

					continue
				}

				continue
			}

If yes, I am fine with this

Copy link
Member

Choose a reason for hiding this comment

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

I think the correct implementation would be:

		if !rawMapVal.IsValid() {
				if d.config.AllowUnsetPointer && fieldValue.Kind() == reflect.Ptr {
					continue
				}

				// There was no matching key in the map for the value in
				// the struct. Remember it for potential errors and metadata.
				targetValKeysUnused[fieldName] = struct{}{}


				continue
		}

Or did I misread what it does?

Copy link
Author

@rostislaved rostislaved May 12, 2025

Choose a reason for hiding this comment

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

Your implementation did the thing. However, if we set AllowUnsetPointer == true and use "continue" we won't add the fieldName to the set and it seems to be formally incorrect, because there is still no "matching key in the map for the value in the struct".

Copy link
Author

Choose a reason for hiding this comment

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

@sagikazarmark What do you think?

Copy link
Author

Choose a reason for hiding this comment

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

@sagikazarmark could you please have a look?

@sagikazarmark sagikazarmark merged commit 8c61ec1 into go-viper:main Jun 16, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants