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.


Class Method Detail

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

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


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.


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.


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.


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)


def identifier(value) #

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


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.


def list(&) #

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


def literal(value : Bool) #

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


def literal(value) #

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


def map(&) #

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


def node : HCL::AST::Node #

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


def to_hcl(_builder : HCL::Builder) #

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


def to_hcl(io : IO) #

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


def to_s(io : IO) #

Alias for #to_hcl(io : IO).