Documentation ¶
Overview ¶
Package typeexpr extends HCL with a convention for describing HCL types within configuration files.
The type syntax is processed statically from a hcl.Expression, so it cannot use any of the usual language operators. This is similar to type expressions in statically-typed programming languages.
variable "example" { type = list(string) }
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ConvertFunc function.Function
ConvertFunc is a cty function that implements type conversions.
Its signature is as follows:
convert(value, type_constraint)
...where type_constraint is a type constraint expression as defined by typeexpr.TypeConstraint.
It relies on HCL's customdecode extension and so it's not suitable for use in non-HCL contexts or if you are using a HCL syntax implementation that does not support customdecode for function arguments. However, it _is_ supported for function calls in the HCL native expression syntax.
var TypeConstraintType cty.Type
TypeConstraintType is a cty capsule type that allows cty type constraints to be used as values.
If TypeConstraintType is used in a context supporting the customdecode.CustomExpressionDecoder extension then it will implement expression decoding using the TypeConstraint function, thus allowing type expressions to be used in contexts where value expressions might normally be expected, such as in arguments to function calls.
Functions ¶
func Type ¶
func Type(expr hcl.Expression) (cty.Type, hcl.Diagnostics)
Type attempts to process the given expression as a type expression and, if successful, returns the resulting type. If unsuccessful, error diagnostics are returned.
func TypeConstraint ¶
func TypeConstraint(expr hcl.Expression) (cty.Type, hcl.Diagnostics)
TypeConstraint attempts to parse the given expression as a type constraint and, if successful, returns the resulting type. If unsuccessful, error diagnostics are returned.
A type constraint has the same structure as a type, but it additionally allows the keyword "any" to represent cty.DynamicPseudoType, which is often used as a wildcard in type checking and type conversion operations.
func TypeConstraintFromVal ¶
TypeConstraintFromVal extracts the type from a cty.Value of TypeConstraintType that was previously constructed using TypeConstraintVal.
If the given value isn't a known, non-null value of TypeConstraintType then this function will panic.
func TypeConstraintVal ¶
TypeConstraintVal constructs a cty.Value whose type is TypeConstraintType.
func TypeString ¶
TypeString returns a string rendering of the given type as it would be expected to appear in the HCL native syntax.
This is primarily intended for showing types to the user in an application that uses typexpr, where the user can be assumed to be familiar with the type expression syntax. In applications that do not use typeexpr these results may be confusing to the user and so type.FriendlyName may be preferable, even though it's less precise.
TypeString produces reasonable results only for types like what would be produced by the Type and TypeConstraint functions. In particular, it cannot support capsule types.
Types ¶
type Defaults ¶
type Defaults struct { // Type of the node for which these defaults apply. This is necessary in // order to determine how to inspect the Defaults and Children collections. Type cty.Type // DefaultValues contains the default values for each object attribute, // indexed by attribute name. DefaultValues map[string]cty.Value // Children is a map of Defaults for elements contained in this type. This // only applies to structural and collection types. // // The map is indexed by string instead of cty.Value because cty.Number // instances are non-comparable, due to embedding a *big.Float. // // Collections have a single element type, which is stored at key "". Children map[string]*Defaults }
Defaults represents a type tree which may contain default values for optional object attributes at any level. This is used to apply nested defaults to a given cty.Value before converting it to a concrete type.
func TypeConstraintWithDefaults ¶
func TypeConstraintWithDefaults(expr hcl.Expression) (cty.Type, *Defaults, hcl.Diagnostics)
TypeConstraintWithDefaults attempts to parse the given expression as a type constraint which may include default values for object attributes. If successful both the resulting type and corresponding defaults are returned. If unsuccessful, error diagnostics are returned.
func (*Defaults) Apply ¶
Apply walks the given value, applying specified defaults wherever optional attributes are missing. The input and output values may have different types, and the result may still require type conversion to the final desired type.
This function is permissive and does not report errors, assuming that the caller will have better context to report useful type conversion failure diagnostics.