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

Add inline defaults to optional object attribute type constraints #31154

Merged
merged 5 commits into from
Jun 6, 2022

Commits on May 31, 2022

  1. typeexpr: Add support for nested type defaults

    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.
    alisdair committed May 31, 2022
    Configuration menu
    Copy the full SHA
    8f69e36 View commit details
    Browse the repository at this point in the history
  2. configs: Add default argument to optional()

    The optional modifier previously accepted a single argument: the
    attribute type. This commit adds an optional second argument, which
    specifies a default value for the attribute.
    
    To record the default values for a variable's type, we use a separate
    parallel structure of `typeexpr.Defaults`, rather than extending
    `cty.Type` to include a `cty.Value` of defaults (which may in turn
    include a `cty.Type` with defaults, and so on, and so forth).
    
    The new `typeexpr.TypeConstraintWithDefaults` returns a type constraint
    and defaults value. Defaults will be `nil` unless there are default
    values specified somewhere in the variable's type.
    alisdair committed May 31, 2022
    Configuration menu
    Copy the full SHA
    650380f View commit details
    Browse the repository at this point in the history
  3. core: Apply type defaults to module variables

    Now that variables parse and retain a set of default values for
    object attributes, we must apply the defaults during variable
    evaluation. We do so immediately before type conversion, preprocessing
    the given value so that conversion will receive the intended defaults as
    appropriate.
    alisdair committed May 31, 2022
    Configuration menu
    Copy the full SHA
    5b0052c View commit details
    Browse the repository at this point in the history

Commits on Jun 1, 2022

  1. lang: Remove defaults function

    Now that we are able to specify optional object attribute defaults
    inline in a type constraint, the separate `defaults` function is no
    longer needed.
    alisdair committed Jun 1, 2022
    Configuration menu
    Copy the full SHA
    718b087 View commit details
    Browse the repository at this point in the history
  2. website: Update documentation for optional attrs

    Extend the documentation on type constraints to include the new default
    functionality, including a detailed example of a nested structure with
    multiple levels of defaults.
    alisdair committed Jun 1, 2022
    Configuration menu
    Copy the full SHA
    e2a3042 View commit details
    Browse the repository at this point in the history