Documentation
¶
Overview ¶
Package hcl implements parsing, encoding and decoding of HCL from Go types.
Its purpose is to provide idiomatic Go functions and types for HCL.
Index ¶
- func AddParentRefs(node Node) error
- func Marshal(v interface{}) ([]byte, error)
- func MarshalAST(ast Node) ([]byte, error)
- func MarshalASTToWriter(ast Node, w io.Writer) error
- func MarshalJSON(ast *AST, options MarshalJSONOptions) ([]byte, error)
- func StripComments(node Node) error
- func Unmarshal(data []byte, v interface{}) error
- func UnmarshalAST(ast *AST, v interface{}) error
- func UnmarshalBlock(block *Block, v interface{}) error
- func Visit(node Node, visit func(node Node, next func() error) error) error
- type AST
- func BlockSchema(name string, v interface{}) (*AST, error)
- func MarshalToAST(v interface{}) (*AST, error)
- func MustBlockSchema(name string, v interface{}) *AST
- func MustSchema(v interface{}) *AST
- func Parse(r io.Reader) (*AST, error)
- func ParseBytes(data []byte) (*AST, error)
- func ParseString(str string) (*AST, error)
- func Schema(v interface{}) (*AST, error)
- type Attribute
- type Block
- type Bool
- type Entry
- type MapEntry
- type MarshalJSONOptions
- type Node
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddParentRefs ¶ added in v0.1.5
AddParentRefs recursively updates an AST's parent references.
This is called automatically during Parse*(), but can be called on a manually constructed AST.
func MarshalAST ¶
MarshalAST marshals an AST to HCL bytes.
func MarshalASTToWriter ¶
MarshalASTToWriter marshals a hcl.AST to an io.Writer.
func MarshalJSON ¶ added in v0.1.5
func MarshalJSON(ast *AST, options MarshalJSONOptions) ([]byte, error)
MarshalJSON gives fine-grained control over JSON marshaling of an AST.
Currently this just means that emission of comments can be controlled.
func StripComments ¶
StripComments recursively from an AST node.
func UnmarshalAST ¶
UnmarshalAST unmarshals an already parsed or constructed AST into a Go struct.
func UnmarshalBlock ¶ added in v0.1.5
UnmarshalBlock into a struct.
Types ¶
type AST ¶
type AST struct {
Pos lexer.Position `parser:"" json:"-"`
Entries []*Entry `parser:"@@*" json:"entries,omitempty"`
TrailingComments []string `parser:"@Comment*" json:"trailing_comments,omitempty"`
Schema bool `parser:"" json:"schema,omitempty"`
}
AST for HCL.
func BlockSchema ¶ added in v0.1.5
BlockSchema reflects a block schema for a Go struct.
func MarshalToAST ¶
MarshalToAST marshals a Go type to a hcl.AST.
func MustBlockSchema ¶ added in v0.1.5
MustBlockSchema reflects a block schema from a Go struct, panicking if an error occurs.
func MustSchema ¶ added in v0.1.5
func MustSchema(v interface{}) *AST
MustSchema constructs a schema from a Go type, or panics.
func (*AST) MarshalJSON ¶ added in v0.1.5
type Attribute ¶
type Attribute struct {
Pos lexer.Position `parser:"" json:"-"`
Parent Node `parser:"" json:"-"`
Comments []string `parser:"@Comment*" json:"comments,omitempty"`
Key string `parser:"@Ident '='" json:"key"`
Value *Value `parser:"@@" json:"value"`
// Set for schemas when the attribute is optional.
Optional bool `parser:"" json:"optional,omitempty"`
}
Attribute is a key+value attribute.
type Block ¶
type Block struct {
Pos lexer.Position `parser:"" json:"-"`
Parent Node `parser:"" json:"-"`
Comments []string `parser:"@Comment*" json:"comments,omitempty"`
Name string `parser:"@Ident" json:"name"`
Labels []string `parser:"@( Ident | String )*" json:"labels,omitempty"`
Body []*Entry `parser:"'{' @@*" json:"body"`
TrailingComments []string `parser:"@Comment* '}'" json:"trailing_comments,omitempty"`
// The block can be repeated. This is surfaced in schemas.
Repeated bool `parser:"" json:"repeated,omitempty"`
}
Block represents am optionally labelled HCL block.
type Entry ¶
type Entry struct {
Pos lexer.Position `parser:"" json:"-"`
Parent Node `parser:"" json:"-"`
Attribute *Attribute `parser:"( @@" json:"attribute,omitempty"`
Block *Block `parser:" | @@ )" json:"block,omitempty"`
}
Entry at the top-level of a HCL file or block.
type MapEntry ¶
type MapEntry struct {
Pos lexer.Position `parser:"" json:"-"`
Parent Node `parser:"" json:"-"`
Comments []string `parser:"@Comment*" json:"comments,omitempty"`
Key *Value `parser:"@@ ':'" json:"key"`
Value *Value `parser:"@@" json:"value"`
}
MapEntry represents a key+value in a map.
type MarshalJSONOptions ¶ added in v0.1.5
type MarshalJSONOptions struct {
// Include comments as JSON attributes.
Comments bool
}
MarshalJSONOptions controls how custom JSON marshaling is applied.
type Node ¶
type Node interface {
// contains filtered or unexported methods
}
Node is the the interface implemented by all AST nodes.
type Value ¶
type Value struct {
Pos lexer.Position `parser:"" json:"-"`
Parent Node `parser:"" json:"-"`
Bool *Bool `parser:"( @('true' | 'false')" json:"bool,omitempty"`
Number *big.Float `parser:" | @Number" json:"number,omitempty"`
Type *string `parser:" | @('number':Ident | 'string':Ident | 'boolean':Ident)" json:"type,omitempty"`
Str *string `parser:" | @(String | Ident)" json:"str,omitempty"`
HeredocDelimiter string `parser:" | (@Heredoc" json:"heredoc_delimiter,omitempty"`
Heredoc *string `parser:" @(Body | EOL)* End)" json:"heredoc,omitempty"`
HaveList bool `parser:" | ( @'['" json:"have_list,omitempty"` // Need this to detect empty lists.
List []*Value `parser:" ( @@ ( ',' @@ )* )? ','? ']' )" json:"list,omitempty"`
HaveMap bool `parser:" | ( @'{'" json:"have_map,omitempty"` // Need this to detect empty maps.
Map []*MapEntry `parser:" ( @@ ( ',' @@ )* ','? )? '}' ) )" json:"map,omitempty"`
}
Value is a scalar, list or map.
func (*Value) GetHeredoc ¶ added in v0.1.5
GetHeredoc gets the heredoc as a string.
This will correctly format indented heredocs.