Skip to content

Prevent ObjectWithOptionalAttrs from escaping#29559

Merged
jbardin merged 4 commits intomainfrom
jbardin/optional-attrs
Sep 13, 2021
Merged

Prevent ObjectWithOptionalAttrs from escaping#29559
jbardin merged 4 commits intomainfrom
jbardin/optional-attrs

Conversation

@jbardin
Copy link
Copy Markdown
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
Copy Markdown
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!

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.
@jbardin jbardin force-pushed the jbardin/optional-attrs branch from d7272b7 to 7f26531 Compare September 13, 2021 12:51
@jbardin jbardin merged commit 8ed9a27 into main Sep 13, 2021
@jbardin jbardin deleted the jbardin/optional-attrs branch September 13, 2021 12:58
@jbardin jbardin mentioned this pull request Sep 13, 2021
@github-actions
Copy link
Copy Markdown
Contributor

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