mirror of
https://github.com/opentffoundation/opentf.git
synced 2026-04-10 12:01:48 -04:00
In type constraints, object attributes may be marked as optional in order to allow them to be omitted from input values. Doing so results in filling the attribute value with a typed `null`. This commit adds a new type `typeexpr.Defaults` which mirrors the structure of a type constraint, storing default values for optional attributes. This will allow specification of non-`null` default values for attributes. The `Defaults` type is a tree structure, each node containing a sub-tree type, a map of children, and for object nodes, a map of defaults. The keys in the children map depend on the type of the node: - Object nodes have children for each attribute; - Tuple nodes have children for each index, with indices converted to string values; - Collection nodes have a single child at the empty string key. When traversing this tree we must take this structure into account, with special cases for map input values which may later be converted to objects. The traversal defined in this commit uses a pre-order transformer in order to pre-populate descendent nodes before their defaults are applied. This allows type nested type default values to be specified more compactly.