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

Prevent ObjectWithOptionalAttrs from escaping #29559

Merged
merged 4 commits into from
Sep 13, 2021
Merged

Conversation

jbardin
Copy link
Member

@jbardin jbardin commented Sep 10, 2021

The use of the ObjectWithOptionalAttrs type should not escape into the type general population. It is only used for the decoding of configuration, and conversions between object values, and otherwise should never be seen in any value or type that could be used to instantiate a value.

Separate the external ImpliedType methods from the cty.Type generated internally for the decoder spec in configschema. Along with preventing optional attrs from escaping via ImpliedType, this separation allows the decoding codepaths to continue running without the overhead of WithoutOptionalAttributesDeep, which is verified by the benchmarks for extremely large schemas

This unfortunately does cause our ImpliedType method to possibly return a different type than the hcldec.ImpliedType function, but the former is only used within terraform for concrete values, while the latter is used to decode HCL. Renaming the ImpliedType methods internally could be done to further differentiate them, but that does cause fairly large diff in the codebase that does not seem worth the effort at this time.

In order to handle optional attributes for the module_variable_optional_attrs experiment, the configs.Variable type needs to keep track of the type constraint for decoding and conversion, as well as the concrete type for creating values and type comparison. Since the Type field is referenced throughout the codebase, and for future refactoring if the handling of optional attributes changes significantly, the type constraint is now loaded into an entirely new field called ConstraintType. This prevents types containing ObjectWithOptionalAttrs from escaping the decode/conversion codepaths into the rest of the codebase.

Fixes #29538

@jbardin jbardin added the 1.0-backport If you add this label to a PR before merging, backport-assistant will open a new PR once merged label Sep 10, 2021
@jbardin jbardin requested a review from a team September 10, 2021 15:39
Copy link
Contributor

@apparentlymart apparentlymart left a comment

Choose a reason for hiding this comment

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

This looks like a fine compromise to keep things roughly rational until we're next working on the optional attributes experiment. Thanks for digging through all this!

internal/configs/configschema/implied_type.go Outdated Show resolved Hide resolved
Objects with optional attributes are only used for the decoding of
HCL, and those types should never be exposed elsewhere within
terraform.

Separate the external ImpliedType method from the cty.Type generated
internally for the decoder spec. This unfortunately causes our
ImpliedType method to return a different type than the
hcldec.ImpliedType function, but the former is only used within
terraform for concrete values, while the latter is used to decode
HCL. Renaming the ImpliedType methods could be done to further
differentiate them, but that does cause fairly large diff in the
codebase that does not seem worth the effort at this time.
In order to handle optional attributes, the Variable type needs to keep
track of the type constraint for decoding and conversion, as well as the
concrete type for creating values and type comparison.

Since the Type field is referenced throughout the codebase, and for
future refactoring if the handling of optional attributes changes
significantly, the constraint is now loaded into an entirely new field
called ConstraintType. This prevents types containing
ObjectWithOptionalAttrs from escaping the decode/conversion codepaths
into the rest of the codebase.
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
1.0-backport If you add this label to a PR before merging, backport-assistant will open a new PR once merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

panic occurs when nested object declared in schema
2 participants