Skip to content

Convert private state types to use common state get/set logic - #1367

Open
chrismarget-j wants to merge 7 commits into
mainfrom
1362-convert-private-state-objects-to-use-a-centralized-marshaler
Open

chrismarget-j wants to merge 7 commits into
mainfrom
1362-convert-private-state-objects-to-use-a-centralized-marshaler

Conversation

@chrismarget-j

@chrismarget-j chrismarget-j commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Prior to this PR, structs which represented private state data implemented the following interface which defined functions that read and write the struct value to and from private state:

type SetterLoader interface {
	SetPrivateState(ctx context.Context, ps State, diags *diag.Diagnostics)
	LoadPrivateState(ctx context.Context, ps State, diags *diag.Diagnostics)

Those functions rely on a fixed key, often hard-coded into the implementation of the interface.

Newer implementations of the interface used reflection to derive a key based on their type name. So the data from private.Something would be read and written using the key "private.Something".

Each type wound up implementing more-or-less the same logic in the SetPrivateState() and LoadPrivateState() functions.

This PR centralizes that logic into new unexpected un-exported functions private.setPrivateState() and private.loadPrivateState() to which the various structs now pass themselves. This approach ensures consistent keying and prevents accidental derivation of a pointer-based key name like *private.Something.

To support migration from the legacy functions which used a hard-coded key, both setPrivateState() and loadPrivateState() allow a legacy key to be passed via a variadic (used to effect "optional") function parameter. When the legacy key string is passed, setPrivateState() writes the data to both locations in private state. loadPrivateState() reads the legacy key first, and falls back to the preferred key for forward compatibility with a provider which has removed legacy support.

At some point in the future, we'll complete the migration by deprecating the old key and removing the data stored using it.

Closes #1362

@chrismarget-j
chrismarget-j added this pull request to stack #1364 November 6, 2026 22:56
@chrismarget-j
chrismarget-j force-pushed the 1362-convert-private-state-objects-to-use-a-centralized-marshaler branch 2 times, most recently from 30ac6b1 to 8fee416 Compare November 15, 2026 23:10
@chrismarget-j chrismarget-j linked an issue Nov 16, 2026 that may be closed by this pull request
@chrismarget-j
chrismarget-j force-pushed the 1362-convert-private-state-objects-to-use-a-centralized-marshaler branch 2 times, most recently from da23d36 to 13a3f73 Compare November 16, 2026 19:08
Base automatically changed from 1361-virtual-network-resource-fails-to-record-dchpenabled-correctly to main November 16, 2026 19:09
@chrismarget-j
chrismarget-j force-pushed the 1362-convert-private-state-objects-to-use-a-centralized-marshaler branch from 13a3f73 to 999d599 Compare November 16, 2026 19:09
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.

Convert private state objects to use a centralized marshaler

2 participants