Prevent ObjectWithOptionalAttrs from escaping#29559
Merged
Conversation
apparentlymart
approved these changes
Sep 10, 2021
Contributor
apparentlymart
left a comment
There was a problem hiding this comment.
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.
d7272b7 to
7f26531
Compare
Merged
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The use of the
ObjectWithOptionalAttrstype 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
ImpliedTypemethods from thecty.Typegenerated internally for the decoder spec in configschema. Along with preventing optional attrs from escaping viaImpliedType, this separation allows the decoding codepaths to continue running without the overhead ofWithoutOptionalAttributesDeep, which is verified by the benchmarks for extremely large schemasThis unfortunately does cause our
ImpliedTypemethod to possibly return a different type than thehcldec.ImpliedTypefunction, but the former is only used within terraform for concrete values, while the latter is used to decode HCL. Renaming theImpliedTypemethods 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_attrsexperiment, theconfigs.Variabletype 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 theTypefield 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 calledConstraintType. This prevents types containingObjectWithOptionalAttrsfrom escaping the decode/conversion codepaths into the rest of the codebase.Fixes #29538