Documentation
¶
Index ¶
- func Get[T any](ctx context.Context, attributeGetter AttributeGetter, diags *diag.Diagnostics, ...) (out T)
- func GetAttribute[T any](ctx context.Context, attributeGetter AttributeGetter, attributePath path.Path, ...) (out T)
- func Set[T any](ctx context.Context, attributeSetter AttributeSetter, in T, ...) diag.Diagnostics
- func SetAttributeTo[T any](ctx context.Context, attributeSetter AttributeSetter, attributePath path.Path, ...) diag.Diagnostics
- func SetPartial[T any](ctx context.Context, attributeSetter AttributeSetter, in T) diag.Diagnostics
- func ValueFrom[T any](in T, opts ...ConverterOption) (tftypes.Value, error)
- func ValueFromConverterForTypedNilHandler[T any]() func() (tftypes.Value, error)
- func ValueTo[T any](in tftypes.Value, opts ...ConverterOption) (out T, err error)
- type AttributeGetter
- type AttributeSetter
- type ConverterOption
- func WithAttributePath(attributePath path.Path) ConverterOption
- func WithSetEmptyContainersToNull() ConverterOption
- func WithSetUnknownValueToZero() ConverterOption
- func WithUseSetForElementsOf[T any]() ConverterOption
- func WithValueFromConverter(converters ...ValueFromConverter) ConverterOption
- func WithValueFromConverterFor[T any](nilHandler func() (tftypes.Value, error), f ValueFromFunc[T]) ConverterOption
- func WithValueToConverter(converters ...ValueToConverter) ConverterOption
- func WithValueToConverterFor[T any](f ValueToFunc[T]) ConverterOption
- type ConverterOptions
- type NullIsUnknown
- type ValueFromConverter
- type ValueFromFunc
- type ValueToConverter
- type ValueToFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Get ¶
func Get[T any](ctx context.Context, attributeGetter AttributeGetter, diags *diag.Diagnostics, opts ...ConverterOption) (out T)
Get gets the whole given Terraform value and converts to T using ValueTo.
func GetAttribute ¶
func GetAttribute[T any](ctx context.Context, attributeGetter AttributeGetter, attributePath path.Path, diags *diag.Diagnostics, opts ...ConverterOption) (out T)
GetAttribute gets a sub-part according to the attribute path. See also Get.
func Set ¶
func Set[T any](ctx context.Context, attributeSetter AttributeSetter, in T, opts ...ConverterOption) diag.Diagnostics
Set sets the whole Terraform value converted from the given input using ValueFrom.
func SetAttributeTo ¶
func SetAttributeTo[T any](ctx context.Context, attributeSetter AttributeSetter, attributePath path.Path, in T, opts ...ConverterOption) diag.Diagnostics
SetAttributeTo sets the whole attribute to the given input, converted with ValueFrom.
func SetPartial ¶
func SetPartial[T any](ctx context.Context, attributeSetter AttributeSetter, in T) diag.Diagnostics
SetPartial only sets the attribute parts present in the input T. Useful when modifying/setting plans only partially (targeting computed values only).
func ValueFrom ¶
func ValueFrom[T any](in T, opts ...ConverterOption) (tftypes.Value, error)
ValueFrom builds a Terraform value from the given generic input using reflection. It traverses nil values to construct the full Terraform type for the value. The value itself can be null if the input is nil. Adjustments are made with WithValueFromConverter option.
func ValueFromConverterForTypedNilHandler ¶
ValueFromConverterForTypedNilHandler constructs are nil value factory for given type T. Used when converter in WithValueFromConverterFor would otherwise get a nil/zero value passed down, which saves some repetitive work in the ValueFromFunc.
func ValueTo ¶
func ValueTo[T any](in tftypes.Value, opts ...ConverterOption) (out T, err error)
ValueTo converts a Terraform value to the desired Go value of type T (which is typically a model struct with `tfsdk` tags). Adjustments are made with WithValueToConverter option. See also ValueFrom counterpart.
Types ¶
type AttributeGetter ¶
type AttributeGetter interface {
Get(ctx context.Context, target any) diag.Diagnostics
GetAttribute(context.Context, path.Path, any) diag.Diagnostics
}
AttributeGetter are implemented by Terraform State, Plan, Config.
type AttributeSetter ¶
type AttributeSetter interface {
Set(context.Context, any) diag.Diagnostics
SetAttribute(context.Context, path.Path, any) diag.Diagnostics
}
AttributeSetter are implemented by Terraform State, Plan, Config.
type ConverterOption ¶
type ConverterOption func(*converter)
func WithAttributePath ¶
func WithAttributePath(attributePath path.Path) ConverterOption
WithAttributePath sets a start path for conversion. Useful when calling ValueTo/ValueFrom from converters itself.
func WithSetEmptyContainersToNull ¶
func WithSetEmptyContainersToNull() ConverterOption
WithSetEmptyContainersToNull sets empty maps and slices to nil when encountered during ValueFrom.
func WithSetUnknownValueToZero ¶
func WithSetUnknownValueToZero() ConverterOption
WithSetUnknownValueToZero set the output to zero value (see reflect.Zero) if an unknown value is encountered in the input. Useful when converting input plans which have computed values set to unknown.
func WithUseSetForElementsOf ¶
func WithUseSetForElementsOf[T any]() ConverterOption
WithUseSetForElementsOf detects slices with given T as sets and uses the corresponding tftypes.Set for building the value.
func WithValueFromConverter ¶
func WithValueFromConverter(converters ...ValueFromConverter) ConverterOption
WithValueFromConverter set additional converters for ValueFrom. See also WithValueFromConverterFor for more convenience.
func WithValueFromConverterFor ¶
func WithValueFromConverterFor[T any](nilHandler func() (tftypes.Value, error), f ValueFromFunc[T]) ConverterOption
WithValueFromConverterFor converts a target type T with the given ValueFromFunc mapper. Supports dedicated nilness handling with ValueFromConverterForTypedNilHandler.
func WithValueToConverter ¶
func WithValueToConverter(converters ...ValueToConverter) ConverterOption
WithValueToConverter adds extra converters to the ValueTo conversion. See WithValueToConverterFor for more convenience.
func WithValueToConverterFor ¶
func WithValueToConverterFor[T any](f ValueToFunc[T]) ConverterOption
WithValueToConverterFor targets the given type t and converts values with the provided ValueToFunc.
type ConverterOptions ¶
type ConverterOptions []ConverterOption
func (ConverterOptions) Append ¶
func (opts ConverterOptions) Append(options ...ConverterOption) ConverterOptions
type NullIsUnknown ¶
type NullIsUnknown[T any] struct { Value *T }
NullIsUnknown wraps a non-nil known value or if nil, the value will become unknown during ValueFrom conversion. This can be more convenient compared to directly manipulating attributes.
func KnownValue ¶
func KnownValue[T any](v T) NullIsUnknown[T]
func (NullIsUnknown[T]) Get ¶
func (v NullIsUnknown[T]) Get() T
func (NullIsUnknown[T]) IsUnknown ¶
func (v NullIsUnknown[T]) IsUnknown() bool
func (NullIsUnknown[T]) Unwrap ¶
func (v NullIsUnknown[T]) Unwrap() reflect.Value
func (*NullIsUnknown[T]) UnwrapAddr ¶
func (v *NullIsUnknown[T]) UnwrapAddr() reflect.Value
type ValueFromConverter ¶
type ValueFromConverter func(attributePath path.Path, in reflect.Value, haveNil, haveUnknown bool) (out tftypes.Value, matched bool, err error)
A ValueFromConverter gets haveNil=true if conversion happens below a nil container such as slice, map or pointer-to-struct. An implementation is expected to return a null tftypes.Value with proper type information.
type ValueFromFunc ¶
type ValueToConverter ¶
type ValueToConverter func(attributePath path.Path, in tftypes.Value, out reflect.Value) (matched bool, err error)
A ValueToConverter gets the out target passed in to investigate the target kind/type. An implementation is expected to call reflect.Value.Set on the given out if matched is returned as true.