class HCL::Builder

Overview

An HCL builder helps build a valid HCL abstract syntax tree.

An HCL::BuildError will be raised on any attempts to create an AST that would be invalid (e.g. setting an attribute's value to a block)

require "hcl"

string = HCL.build do |hcl|
  hcl.block "aws_instance", "c4.xlarge" do |blk|
    blk.attribute "security_group_ids" do
      blk.list do |l|
        l << l.literal("sg-123")
        l << l.literal("sg-456")
      end
    end
    blk.attribute("region") { "us-west-2" }
    blk.block("tags") do |t|
      t.attribute("name") { "test" }
    end
  end
end
string # => "aws_instance \"c4.xlarge\" {\n  security_group_ids = [\"sg-123\", \"sg-456\"]\n  region = \"us-west-2\"\n  tags {\n    name = \"test\"\m}"

Defined in:

hcl/builder.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from class Reference

==(other : HCL::Any) ==

Instance methods inherited from class Object

===(other : HCL::Any) ===

Class methods inherited from class Object

from_hcl(string_or_io : String | IO, ctx : HCL::ExpressionContext = HCL::ExpressionContext.default_context) from_hcl

Constructor Detail

def self.new(node : AST::Node) #

Instantiate's a new HCL::Builder with the given root node. This generally does not need to be invoked directly.


[View source]

Class Method Detail

def self.build(node : AST::Node | Nil = AST::Document.new, &) #

Yields an HCL::Builder instance for the given root node, which defaults to HCL::AST::Document. Returns the HCL::Builder instance.


[View source]

Instance Method Detail

def <<(value : AST::Node) #

Appends an HCL::AST::Node to the underlying list node. Raises if the builder is not for a list or if the node is not usable within a list.


[View source]
def <<(value) #

Appends a value to the the underlying list node. Value most be convertable to HCL, either through base types supported by the library or a #to_hcl(builder : HCL::Builder) method on the object.

Raises if the builder is not for a list or if the node is not usable within a list.


[View source]
def attribute(name, &) #

Adds an attribute to the open HCL body or map/object with the value of the passed in block. Value must by an AST node or an object convertable to HCL, either through base types supported by the library or a #to_hcl(builder : HCL::Builder) method on the object.


[View source]
def block(name, *args, &) #

Yields a new HCL::Builder for building a block. Adds the block to the open HCL body. name is the first parameter, but subsequent parameters are used as label values on the block. #label may also be used within the block to set labels.

Raises if used within a non-body node (i.e. not a document or block)


[View source]
def identifier(value) #

Returns a new AST::Identifier node for the given value.


[View source]
def label(value : Symbol | String) #

Appends a new AST::BlockLabel node with the given value to the block's labels collection.

Raises if active node is not a block.


[View source]
def list(&) #

Yields a new HCL::Builder for building a list.


[View source]
def literal(value : Bool) #

Returns a new AST::Literal node for the given boolean value.


[View source]
def literal(value) #

Returns a new AST::Literal node for the given value.


[View source]
def map(&) #

Yields a new HCL::Builder for building a map/object.


[View source]
def node : HCL::AST::Node #

[View source]
def number(value) #

Returns a new AST::Number node with the given value

Raises if value cannot be used or converted to a supported number format


[View source]
def to_hcl(_builder : HCL::Builder) #

Returns the HCL::AST::Node for the builder.


[View source]
def to_hcl(io : IO) #

Writes a string representation of the HCL node to the given IO.


[View source]
def to_s(io : IO) #

Alias for #to_hcl(io : IO).


[View source]