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

feat: support for automatically initializing squashed pointer structs #71

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tuunit
Copy link

@tuunit tuunit commented Feb 15, 2025

Description

This PR introduces support for automatically initializing squashed pointer structs during decodeStructFromMap.

Previously, when decoding into a struct with squashed embedded pointer fields, the decoder would fail if the pointer fields were not pre-initialized. This change ensures that such pointer fields are automatically initialized if they are nil, allowing for seamless decoding without requiring manual initialization.

  • Improves usability by removing the need to pre-initialize squashed pointer structs. Especially useful for nested or dynamic structs and lists.
  • Maintains backward compatibility with existing behavior for non-pointer and pre-initialized fields.

Issue

	type EmbeddedPointerSquash {
		*Basic `mapstructure:",squash"`
	}

	input := map[string]interface{}{
		"Vstring": "foo",
		"Vunique": "bar",
	}

	result := EmbeddedPointerSquash{} // Without initializing the internal struct
	err := Decode(input, &result)
	if err != nil {
		t.Fatalf("got an err: %s", err.Error())
	}

Would throw the error unsupported type for squash: ptr

@tuunit tuunit force-pushed the feat/add-support-for-automatically-initializing-squashed-pointer-structs branch from 47e353d to 0512b74 Compare February 15, 2025 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant