reflect

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2022 License: MPL-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package reflect contains the implementation for converting framework-defined data into and from provider-defined Go types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildValue

func BuildValue(ctx context.Context, typ attr.Type, val tftypes.Value, target reflect.Value, opts Options, path path.Path) (reflect.Value, diag.Diagnostics)

BuildValue constructs a reflect.Value of the same type as `target`, populated with the data in `val`. It will defensively instantiate new values to set, making it safe for use with pointer types which may be nil. It tries to give consumers the ability to override its default behaviors wherever possible.

func FromAttributeValue

func FromAttributeValue(ctx context.Context, typ attr.Type, val attr.Value, path path.Path) (attr.Value, diag.Diagnostics)

FromAttributeValue creates an attr.Value from an attr.Value. It just returns the attr.Value it is passed, but reserves the right in the future to do some validation on that attr.Value to make sure it matches the type produced by `typ`.

It is meant to be called through FromValue, not directly.

func FromBigFloat

func FromBigFloat(ctx context.Context, typ attr.Type, val *big.Float, path path.Path) (attr.Value, diag.Diagnostics)

FromBigFloat creates an attr.Value using `typ` from a *big.Float.

It is meant to be called through FromValue, not directly.

func FromBigInt

func FromBigInt(ctx context.Context, typ attr.Type, val *big.Int, path path.Path) (attr.Value, diag.Diagnostics)

FromBigInt creates an attr.Value using `typ` from a *big.Int.

It is meant to be called through FromValue, not directly.

func FromBool

func FromBool(ctx context.Context, typ attr.Type, val bool, path path.Path) (attr.Value, diag.Diagnostics)

FromBool returns an attr.Value as produced by `typ` from a bool.

It is meant to be called through FromValue, not directly.

func FromFloat

func FromFloat(ctx context.Context, typ attr.Type, val float64, path path.Path) (attr.Value, diag.Diagnostics)

FromFloat creates an attr.Value using `typ` from a float64.

It is meant to be called through FromValue, not directly.

func FromInt

func FromInt(ctx context.Context, typ attr.Type, val int64, path path.Path) (attr.Value, diag.Diagnostics)

FromInt creates an attr.Value using `typ` from an int64.

It is meant to be called through FromValue, not directly.

func FromMap

FromMap returns an attr.Value representing the data contained in `val`. `val` must be a map type with keys that are a string type. The attr.Value will be of the type produced by `typ`.

It is meant to be called through FromValue, not directly.

func FromNullable

func FromNullable(ctx context.Context, typ attr.Type, val Nullable, path path.Path) (attr.Value, diag.Diagnostics)

FromNullable creates an attr.Value from the data in a Nullable.

It is meant to be called through FromValue, not directly.

func FromPointer

func FromPointer(ctx context.Context, typ attr.Type, value reflect.Value, path path.Path) (attr.Value, diag.Diagnostics)

FromPointer turns a pointer into an attr.Value using `typ`. If the pointer is nil, the attr.Value will use its null representation. If it is not nil, it will recurse into FromValue to find the attr.Value of the type the value the pointer is referencing.

It is meant to be called through FromValue, not directly.

func FromSlice

func FromSlice(ctx context.Context, typ attr.Type, val reflect.Value, path path.Path) (attr.Value, diag.Diagnostics)

FromSlice returns an attr.Value as produced by `typ` using the data in `val`. `val` must be a slice. `typ` must be an attr.TypeWithElementType or attr.TypeWithElementTypes. If the slice is nil, the representation of null for `typ` will be returned. Otherwise, FromSlice will recurse into FromValue for each element in the slice, using the element type or types defined on `typ` to construct values for them.

It is meant to be called through FromValue, not directly.

func FromString

func FromString(ctx context.Context, typ attr.Type, val string, path path.Path) (attr.Value, diag.Diagnostics)

FromString returns an attr.Value as produced by `typ` from a string.

It is meant to be called through FromValue, not directly.

func FromStruct

FromStruct builds an attr.Value as produced by `typ` from the data in `val`. `val` must be a struct type, and must have all its properties tagged and be a 1:1 match with the attributes reported by `typ`. FromStruct will recurse into FromValue for each attribute, using the type of the attribute as reported by `typ`.

It is meant to be called through FromValue, not directly.

func FromUint

func FromUint(ctx context.Context, typ attr.Type, val uint64, path path.Path) (attr.Value, diag.Diagnostics)

FromUint creates an attr.Value using `typ` from a uint64.

It is meant to be called through FromValue, not directly.

func FromUnknownable

func FromUnknownable(ctx context.Context, typ attr.Type, val Unknownable, path path.Path) (attr.Value, diag.Diagnostics)

FromUnknownable creates an attr.Value from the data in an Unknownable.

It is meant to be called through FromValue, not directly.

func FromValue

func FromValue(ctx context.Context, typ attr.Type, val interface{}, path path.Path) (attr.Value, diag.Diagnostics)

FromValue is the inverse of Into, taking a Go value (`val`) and transforming it into an attr.Value using the attr.Type supplied. `val` will first be transformed into a tftypes.Value, then passed to `typ`'s ValueFromTerraform method.

func FromValueCreator

func FromValueCreator(ctx context.Context, typ attr.Type, val tftypes.ValueCreator, path path.Path) (attr.Value, diag.Diagnostics)

FromValueCreator creates an attr.Value from the data in a tftypes.ValueCreator, calling its ToTerraform5Value method and converting the result to an attr.Value using `typ`.

It is meant to be called from FromValue, not directly.

func Into

func Into(ctx context.Context, typ attr.Type, val tftypes.Value, target interface{}, opts Options, path path.Path) diag.Diagnostics

Into uses the data in `val` to populate `target`, using the reflection package to recursively reflect into structs and slices. If `target` is an attr.Value, its assignment method will be used instead of reflecting. If `target` is a tftypes.ValueConverter, the FromTerraformValue method will be used instead of using reflection. Primitives are set using the val.As method. Structs use reflection: each exported struct field must have a "tfsdk" tag with the name of the field in the tftypes.Value, and all fields in the tftypes.Value must have a corresponding property in the struct. Into will be called for each struct field. Slices will have Into called for each element.

func IsGenericAttrValue added in v0.6.0

func IsGenericAttrValue(ctx context.Context, target interface{}) bool

func Map

func Map(ctx context.Context, typ attr.Type, val tftypes.Value, target reflect.Value, opts Options, path path.Path) (reflect.Value, diag.Diagnostics)

Map creates a map value that matches the type of `target`, and populates it with the contents of `val`.

func NewAttributeValue

func NewAttributeValue(ctx context.Context, typ attr.Type, val tftypes.Value, target reflect.Value, opts Options, path path.Path) (reflect.Value, diag.Diagnostics)

NewAttributeValue creates a new reflect.Value by calling the ValueFromTerraform method on `typ`. It will return an error if the returned `attr.Value` is not the same type as `target`.

It is meant to be called through Into, not directly.

func NewNullable

func NewNullable(ctx context.Context, typ attr.Type, val tftypes.Value, target reflect.Value, opts Options, path path.Path) (reflect.Value, diag.Diagnostics)

NewNullable creates a zero value of `target` (or the concrete type it's referencing, if it's a pointer) and calls its SetNull method.

It is meant to be called through Into, not directly.

func NewUnknownable

func NewUnknownable(ctx context.Context, typ attr.Type, val tftypes.Value, target reflect.Value, opts Options, path path.Path) (reflect.Value, diag.Diagnostics)

NewUnknownable creates a zero value of `target` (or the concrete type it's referencing, if it's a pointer) and calls its SetUnknown method.

It is meant to be called through Into, not directly.

func NewValueConverter

func NewValueConverter(ctx context.Context, typ attr.Type, val tftypes.Value, target reflect.Value, opts Options, path path.Path) (reflect.Value, diag.Diagnostics)

NewValueConverter creates a zero value of `target` (or the concrete type it's referencing, if it's a pointer) and calls its FromTerraform5Value method.

It is meant to be called through Into, not directly.

func Number

func Number(ctx context.Context, typ attr.Type, val tftypes.Value, target reflect.Value, opts Options, path path.Path) (reflect.Value, diag.Diagnostics)

Number creates a *big.Float and populates it with the data in `val`. It then gets converted to the type of `target`, as long as `target` is a valid number type (any of the built-in int, uint, or float types, *big.Float, and *big.Int).

Number will loudly fail when a number cannot be losslessly represented using the requested type, unless opts.AllowRoundingNumbers is set to true. This setting is mildly dangerous, because Terraform does not like when you round things, as a general rule of thumb.

It is meant to be called through Into, not directly.

func Pointer

func Pointer(ctx context.Context, typ attr.Type, val tftypes.Value, target reflect.Value, opts Options, path path.Path) (reflect.Value, diag.Diagnostics)

Pointer builds a new zero value of the concrete type that `target` references, populates it with BuildValue, and takes a pointer to it.

It is meant to be called through Into, not directly.

func Primitive

func Primitive(ctx context.Context, typ attr.Type, val tftypes.Value, target reflect.Value, path path.Path) (reflect.Value, diag.Diagnostics)

Primitive builds a string or boolean, depending on the type of `target`, and populates it with the data in `val`.

It is meant to be called through `Into`, not directly.

func Struct

func Struct(ctx context.Context, typ attr.Type, object tftypes.Value, target reflect.Value, opts Options, path path.Path) (reflect.Value, diag.Diagnostics)

Struct builds a new struct using the data in `object`, as long as `object` is a `tftypes.Object`. It will take the struct type from `target`, which must be a struct type.

The properties on `target` must be tagged with a "tfsdk" label containing the field name to map to that property. Every property must be tagged, and every property must be present in the type of `object`, and all the attributes in the type of `object` must have a corresponding property. Properties that don't map to object attributes must have a `tfsdk:"-"` tag, explicitly defining them as not part of the object. This is to catch typos and other mistakes early.

Struct is meant to be called from Into, not directly.

Types

type DiagIntoIncompatibleType added in v0.3.0

type DiagIntoIncompatibleType struct {
	Val        tftypes.Value
	TargetType reflect.Type
	Err        error
}

func (DiagIntoIncompatibleType) Detail added in v0.3.0

func (d DiagIntoIncompatibleType) Detail() string

func (DiagIntoIncompatibleType) Equal added in v0.3.0

func (DiagIntoIncompatibleType) Severity added in v0.3.0

func (d DiagIntoIncompatibleType) Severity() diag.Severity

func (DiagIntoIncompatibleType) Summary added in v0.3.0

func (d DiagIntoIncompatibleType) Summary() string

type DiagNewAttributeValueIntoWrongType added in v0.3.0

type DiagNewAttributeValueIntoWrongType struct {
	ValType    reflect.Type
	TargetType reflect.Type
	SchemaType attr.Type
}

func (DiagNewAttributeValueIntoWrongType) Detail added in v0.3.0

func (DiagNewAttributeValueIntoWrongType) Equal added in v0.3.0

func (DiagNewAttributeValueIntoWrongType) Severity added in v0.3.0

func (DiagNewAttributeValueIntoWrongType) Summary added in v0.3.0

type Nullable

type Nullable interface {
	SetNull(context.Context, bool) error
	SetValue(context.Context, interface{}) error
	GetNull(context.Context) bool
	GetValue(context.Context) interface{}
}

Nullable is an interface for types that can be explicitly set to null.

type Options

type Options struct {
	// UnhandledNullAsEmpty controls whether null values should be
	// translated into empty values without provider interaction, or if
	// they must be explicitly handled.
	UnhandledNullAsEmpty bool

	// UnhandledUnknownAsEmpty controls whether null values should be
	// translated into empty values without provider interaction, or if
	// they must be explicitly handled.
	UnhandledUnknownAsEmpty bool

	// AllowRoundingNumbers silently rounds numbers that don't fit
	// perfectly in the types they're being stored in, rather than
	// returning errors. Numbers will always be rounded towards 0.
	AllowRoundingNumbers bool
}

Options provides configuration settings for how the reflection behavior works, letting callers tweak different behaviors based on their needs.

type Unknownable

type Unknownable interface {
	SetUnknown(context.Context, bool) error
	SetValue(context.Context, interface{}) error
	GetUnknown(context.Context) bool
	GetValue(context.Context) interface{}
}

Unknownable is an interface for types that can be explicitly set to known or unknown.

Jump to

Keyboard shortcuts

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