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{}, options ...MarshalOption) ([]byte, error)
- func MarshalAST(ast Node) ([]byte, error)
- func MarshalASTToWriter(ast Node, w io.Writer) error
- func StripComments(node Node) error
- func Unmarshal(data []byte, v interface{}, options ...MarshalOption) error
- func UnmarshalAST(ast *AST, v interface{}, options ...MarshalOption) error
- func UnmarshalBlock(block *Block, v interface{}, options ...MarshalOption) error
- func Visit(node Node, visitor func(node Node, next func() error) error) error
- type AST
- func BlockSchema(name string, v interface{}, options ...MarshalOption) (*AST, error)
- func MarshalToAST(v interface{}, options ...MarshalOption) (*AST, error)
- func MustBlockSchema(name string, v interface{}, options ...MarshalOption) *AST
- func MustSchema(v interface{}, options ...MarshalOption) *AST
- func Parse(r io.Reader, options ...ParseOption) (*AST, error)
- func ParseBytes(data []byte, options ...ParseOption) (*AST, error)
- func ParseString(str string, options ...ParseOption) (*AST, error)
- func Schema(v interface{}, options ...MarshalOption) (*AST, error)
- type Attribute
- type Block
- type Bool
- type Call
- type Comment
- type CommentList
- type Entries
- type Entry
- type Heredoc
- type List
- type Map
- type MapEntry
- type MarshalOption
- func AllowExtra(ok bool) MarshalOption
- func BareBooleanAttributes(v bool) MarshalOption
- func HereDocsForMultiLine(n int) MarshalOption
- func HydratedImplicitBlocks(v bool) MarshalOption
- func InferHCLTags(v bool) MarshalOption
- func WithDefaultTransformer(transformer func(string) string) MarshalOption
- func WithSchemaComments(v bool) MarshalOption
- type Node
- type Number
- type ParseOption
- type Position
- type RecursiveEntry
- type String
- type Type
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddParentRefs ¶
AddParentRefs recursively updates an AST's parent references.
This is called automatically during Parse*(), but can be called on a manually constructed AST.
func Marshal ¶
func Marshal(v interface{}, options ...MarshalOption) ([]byte, error)
Marshal a Go type to HCL.
func MarshalAST ¶
MarshalAST marshals an AST to HCL bytes.
func MarshalASTToWriter ¶
MarshalASTToWriter marshals a hcl.AST to an io.Writer.
func StripComments ¶
StripComments recursively from an AST node.
func Unmarshal ¶
func Unmarshal(data []byte, v interface{}, options ...MarshalOption) error
Unmarshal HCL into a Go struct.
func UnmarshalAST ¶
func UnmarshalAST(ast *AST, v interface{}, options ...MarshalOption) error
UnmarshalAST unmarshalls an already parsed or constructed AST into a Go struct.
func UnmarshalBlock ¶
func UnmarshalBlock(block *Block, v interface{}, options ...MarshalOption) error
UnmarshalBlock into a struct.
Types ¶
type AST ¶
type AST struct {
Pos lexer.Position `parser:""`
Entries Entries `parser:"@@*"`
TrailingComments CommentList
Schema bool `parser:""`
}
AST for HCL.
func BlockSchema ¶
func BlockSchema(name string, v interface{}, options ...MarshalOption) (*AST, error)
BlockSchema reflects a block schema for a Go struct.
func MarshalToAST ¶
func MarshalToAST(v interface{}, options ...MarshalOption) (*AST, error)
MarshalToAST marshals a Go type to a hcl.AST.
func MustBlockSchema ¶
func MustBlockSchema(name string, v interface{}, options ...MarshalOption) *AST
MustBlockSchema reflects a block schema from a Go struct, panicking if an error occurs.
func MustSchema ¶
func MustSchema(v interface{}, options ...MarshalOption) *AST
MustSchema constructs a schema from a Go type, or panics.
func Parse ¶
func Parse(r io.Reader, options ...ParseOption) (*AST, error)
Parse HCL from an io.Reader.
func ParseBytes ¶
func ParseBytes(data []byte, options ...ParseOption) (*AST, error)
ParseBytes parses HCL from bytes.
func ParseString ¶
func ParseString(str string, options ...ParseOption) (*AST, error)
ParseString parses HCL from a string.
func Schema ¶
func Schema(v interface{}, options ...MarshalOption) (*AST, error)
Schema reflects a schema from a Go value.
A schema is itself HCL.
type Attribute ¶
type Attribute struct {
Pos lexer.Position `parser:""`
Parent Node `parser:""`
Comments CommentList
Key string `parser:"@Ident"`
Value Value `parser:"( '=':Punct @@ )?"`
Default Value `parser:"( '(' ( ( 'default' '(' @@ ')'"`
Enum []Value `parser:" | 'enum' '(' @@ (',' @@)* ')'"`
Optional bool `parser:" | @'optional' ) )+ ')' )?"`
}
Attribute is a key=value attribute.
type Block ¶
type Block struct {
Pos lexer.Position `parser:""`
Parent Node `parser:""`
Comments CommentList
Name string `parser:"@Ident"`
Repeated bool `parser:"( '(' @'repeated' ')' )?"`
Labels []string `parser:"@( Ident | String )*"`
Body Entries `parser:"'{' @@* '}'"`
TrailingComments CommentList
}
Block represents am optionally labelled HCL block.
type Bool ¶
type Bool struct {
Pos lexer.Position `parser:""`
Parent Node `parser:""`
Bool bool `parser:"@'true':Ident | 'false':Ident"`
}
Bool represents a parsed boolean value.
type Call ¶
type Call struct {
Pos lexer.Position `parser:""`
Parent Node `parser:""`
Args []Value `parser:"'(' @@ ( ',' @@ )* ')'"`
}
Call represents a function call.
type Comment ¶ added in v2.3.0
type Comment struct {
Pos lexer.Position `parser:""`
EndPos lexer.Position `parser:""`
Parent Node `parser:""`
Comments CommentList `parser:"@Comment"`
}
func (*Comment) EndPosition ¶ added in v2.3.0
type CommentList ¶ added in v2.3.0
type CommentList []string
CommentList represents a list of comments that can handle multiline comment splitting
func (*CommentList) Capture ¶ added in v2.3.0
func (c *CommentList) Capture(values []string) error
Capture implements the participle Capture interface to split multiline comments
type Entries ¶
type Entries []Entry
Entries in the root of the AST or a Block.
func (Entries) MarshalJSON ¶
type Heredoc ¶
type Heredoc struct {
Pos lexer.Position `parser:""`
Parent Node `parser:""`
Delimiter string `parser:"(@Heredoc"`
Doc string `parser:" @(Body | EOL)* End)"`
}
Heredoc represents a heredoc string.
func (*Heredoc) GetHeredoc ¶
GetHeredoc gets the heredoc as a string.
This will correctly format indented heredocs.
type List ¶
type List struct {
Pos lexer.Position `parser:""`
Parent Node `parser:""`
List []Value `parser:"( '[' ( @@ ( ',' @@ )* )? ','? ']' )"`
}
A List of values.
type Map ¶
type Map struct {
Pos lexer.Position `parser:""`
Parent Node `parser:""`
Entries []*MapEntry `parser:"( '{' ( @@ ( ',' @@ )* ','? )? '}' )"`
}
A Map of key to value.
type MapEntry ¶
type MapEntry struct {
Pos lexer.Position `parser:""`
Parent Node `parser:""`
Comments []string `parser:"@Comment*"`
Key Value `parser:"@@ ':'"`
Value Value `parser:"@@"`
}
MapEntry represents a key+value in a map.
type MarshalOption ¶
type MarshalOption func(options *marshalState)
MarshalOption configures optional marshalling behaviour.
func AllowExtra ¶
func AllowExtra(ok bool) MarshalOption
AllowExtra fields in configuration to be skipped.
func BareBooleanAttributes ¶
func BareBooleanAttributes(v bool) MarshalOption
BareBooleanAttributes specifies whether attributes without values will be treated as boolean true values.
eg.
attr
NOTE: This is non-standard HCL.
func HereDocsForMultiLine ¶
func HereDocsForMultiLine(n int) MarshalOption
HereDocsForMultiLine will marshal multi-line strings >= n lines as indented heredocs rather than quoted strings.
func HydratedImplicitBlocks ¶ added in v2.6.0
func HydratedImplicitBlocks(v bool) MarshalOption
HydratedImplicitBlocks will treat single (non-repeated) blocks as always present during unmarshalling, even if absent from the config. This causes defaults within those blocks to be applied and required fields to be enforced.
func InferHCLTags ¶
func InferHCLTags(v bool) MarshalOption
InferHCLTags specifies whether to infer behaviour if hcl:"" tags are not present.
This currently just means that all structs become blocks.
func WithDefaultTransformer ¶ added in v2.4.0
func WithDefaultTransformer(transformer func(string) string) MarshalOption
WithDefaultTransformer allows custom processing of default values in struct tags.
func WithSchemaComments ¶
func WithSchemaComments(v bool) MarshalOption
WithSchemaComments will export the contents of the help struct tag as comments when marshaling.
type Node ¶
type Node interface {
Position() Position
Detach() bool
// contains filtered or unexported methods
}
Node is the the interface implemented by all AST nodes.
type Number ¶
type Number struct {
Pos lexer.Position `parser:""`
Parent Node `parser:""`
Float *big.Float `parser:"@Number"`
}
Number of arbitrary precision.
type ParseOption ¶ added in v2.3.0
type ParseOption func(*parseConfig)
ParseOption represents a functional option for Parse, ParseString, and ParseBytes.
func WithDetachedComments ¶ added in v2.3.0
func WithDetachedComments(preserve bool) ParseOption
WithDetachedComments controls whether comments that are not directly associated with a block or attribute are preserved in the AST. If set to false (default), detached comments will be stripped from the AST during post-processing. If set to true, detached comments will be preserved as separate entries in the AST.
type RecursiveEntry ¶
type RecursiveEntry struct{}
RecursiveEntry is an Entry representing that a schema is recursive.
func (*RecursiveEntry) Clone ¶
func (*RecursiveEntry) Clone() Entry
func (*RecursiveEntry) Detach ¶
func (*RecursiveEntry) Detach() bool
func (*RecursiveEntry) EntryKey ¶
func (*RecursiveEntry) EntryKey() string
func (*RecursiveEntry) Position ¶
func (*RecursiveEntry) Position() Position
type String ¶
type String struct {
Pos lexer.Position `parser:""`
Parent Node `parser:""`
Str string `parser:"@(String | Ident)"`
}
String literal.