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

hclwrite: Allow updating block type and labels #340

Merged
merged 3 commits into from
Aug 21, 2020

Commits on Aug 21, 2020

  1. hclwrite: Allow updating block type and labels

    Fixes hashicorp#338
    
    Add methods to update block type and labels to enable us to refactor HCL
    configurations such as renaming Terraform resources.
    
    - `*Block.SetType(typeName string)`
    - `*Block.SetLabels(labels []string)`
    
    Some additional notes about SetLabels:
    
    Since we cannot assume that old and new labels are equal in length,
    remove old labels and insert new ones before TokenOBrace.
    
    To implement this, I also added the following methods.
    
    - `*nodes.Insert(pos *node, c nodeContent) *node`
    - `*nodes.InsertNode(pos *node, n *node) *node`
    
    They are similar to the existing Append / AppendNode,
    but insert a node before a given position.
    minamijoyo authored and apparentlymart committed Aug 21, 2020
    Configuration menu
    Copy the full SHA
    0262c13 View commit details
    Browse the repository at this point in the history
  2. hclwrite: Fix a bug that Block.open/close positions were not recorded…

    … in parser
    
    While implementing Block.SetLabels(), I found a new hclwrite parser bug.
    
    The NewBlock() method records positions of TokenOBrace / TokenCBrace.
    Nevertheless when generating blocks via hclwrite.ParseConfig(),
    they were not recorded.
    
    The position of TokenOBrace is needed for Block.SetLabels(),
    so I also fixed this existing bug.
    minamijoyo authored and apparentlymart committed Aug 21, 2020
    Configuration menu
    Copy the full SHA
    7ead21e View commit details
    Browse the repository at this point in the history
  3. hclwrite: Make block labels a node in their own right

    All of the other subdivisions of a block were already nodes, but we'd
    represented the labels as an undifferentiated set of nodes belonging
    directly to the block's child node list.
    
    Now that we support replacing the labels in the public API, that's a good
    excuse to refactor this slightly to make the labels their own node. As
    well as being consistent with everything else in Block, this also makes
    it easier to implement the Block.SetLabels operation because we can
    just  change the children of the labels node, rather than having to
    carefully identify and extract the individual child nodes of the block
    that happen to represent labels.
    
    Internally this models the labels in a similar sort of way as the content
    of a body, although we've kept the public API directly on the Block type
    here because that's a more straightforward model for the use-cases we
    currently know and matches better with the API of hcl.Block. This is just
    an internal change for consistency.
    
    I also added a few tests for having comments interspersed with labels
    while I was here, because that helped to better exercise the new
    parseBlockLabels function.
    apparentlymart committed Aug 21, 2020
    Configuration menu
    Copy the full SHA
    7e5b148 View commit details
    Browse the repository at this point in the history