reflection

package
v0.220.2 Latest Latest
Warning

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

Go to latest
Published: May 21, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithTypeRegistry

func ContextWithTypeRegistry(ctx context.Context, r *TypeRegistry) context.Context

ContextWithTypeRegistry adds a type registry to the given context.

func Module

func Module() string

Module returns the FTL module currently being executed.

func TypeRegistryFromContext

func TypeRegistryFromContext(ctx context.Context) optional.Option[*TypeRegistry]

TypeRegistryFromContext retrieves the TypeRegistry previously added to the context with ContextWithTypeRegistry.

Types

type Ref

type Ref struct {
	Module string `json:"module"`
	Name   string `json:"name"`
}

Ref is an untyped reference to a symbol.

func FuncRef

func FuncRef(call any) Ref

FuncRef returns the Ref for a Go function.

Panics if called with a function outside FTL.

func ParseRef

func ParseRef(ref string) (Ref, error)

func RefFromProto

func RefFromProto(p *schemapb.Ref) Ref

func TypeRef

func TypeRef[T any]() Ref

TypeRef returns the Ref for a Go type.

Panics if called with a type outside of FTL.

func TypeRefFromValue

func TypeRefFromValue(v any) Ref

TypeRefFromValue returns the Ref for a Go value.

The value must be a named type such as a struct, enum, or sum type.

func (Ref) String

func (v Ref) String() string

func (Ref) ToProto

func (v Ref) ToProto() *schemapb.Ref

func (*Ref) UnmarshalText

func (v *Ref) UnmarshalText(text []byte) error

type TypeRegistry

type TypeRegistry struct {
	// contains filtered or unexported fields
}

TypeRegistry is used for dynamic type resolution at runtime. It stores associations between sum type discriminators and their variants, for use in encoding and decoding.

FTL manages the type registry for you, so you don't need to create one yourself.

func NewTypeRegistry

func NewTypeRegistry(options ...TypeRegistryOption) *TypeRegistry

NewTypeRegistry creates a new TypeRegistry for instantiating types by their qualified name at runtime.

func (*TypeRegistry) GetDiscriminatorByVariant

func (t *TypeRegistry) GetDiscriminatorByVariant(variant reflect.Type) optional.Option[reflect.Type]

GetDiscriminatorByVariant returns the discriminator type for the given variant type.

func (*TypeRegistry) GetVariantByName

func (t *TypeRegistry) GetVariantByName(discriminator reflect.Type, name string) optional.Option[reflect.Type]

GetVariantByName returns the variant type for the given discriminator and variant name.

func (*TypeRegistry) GetVariantByType

func (t *TypeRegistry) GetVariantByType(discriminator reflect.Type, variantType reflect.Type) optional.Option[string]

GetVariantByType returns the variant name for the given discriminator and variant type.

func (*TypeRegistry) IsSumTypeDiscriminator

func (t *TypeRegistry) IsSumTypeDiscriminator(discriminator reflect.Type) bool

IsSumTypeDiscriminator returns true if the given type is a sum type discriminator.

func (*TypeRegistry) RegisterSumType

func (t *TypeRegistry) RegisterSumType(discriminator reflect.Type, variants map[string]reflect.Type)

RegisterSumType registers a Go sum type with the type registry.

Sum types are represented as enums in the FTL schema.

type TypeRegistryOption

type TypeRegistryOption func(t *TypeRegistry)

TypeRegistryOption is a functional option for configuring a TypeRegistry.

func WithSumType

func WithSumType[Discriminator any](variants ...Discriminator) TypeRegistryOption

WithSumType adds a sum type and its variants to the type registry.

Jump to

Keyboard shortcuts

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