generic

package
v0.19.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2026 License: MPL-2.0 Imports: 12 Imported by: 0

Documentation

Index

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

func ValueFromConverterForTypedNilHandler[T any]() func() (tftypes.Value, error)

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 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 WithSliceTypeAsSet added in v0.19.2

func WithSliceTypeAsSet(sliceTypeAsSet func(p reflect.Type) bool) ConverterOption

WithSliceTypeAsSet detects slices types where tftypes.Set is used 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 WithValueFromEmptyContainer added in v0.18.2

func WithValueFromEmptyContainer(handler ValueFromEmptyContainerHandler) ConverterOption

WithValueFromEmptyContainer defines a handler which is called when the value encountered is an empty slice or map. By default, empty containers are converted to empty Terraform values (not null), and the handler can change this to a null value if desired.

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 ValueFromEmptyContainerHandler added in v0.18.2

type ValueFromEmptyContainerHandler func(attributePath path.Path) (haveNil bool, err error)

type ValueFromFunc

type ValueFromFunc[T any] func(attributePath path.Path, in T) (tftypes.Value, error)

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.

type ValueToFunc

type ValueToFunc[T any] func(attributePath path.Path, in tftypes.Value) (T, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL