go.temporal.io/server/chasm/field.go
169 LOC · 59 covered · 110 uncovered · 23 ranges · 845 concepts · 12 introducers · 431 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
package chasm
import (
"reflect"
"go.temporal.io/api/serviceerror"
"google.golang.org/protobuf/proto"
)
const (
// Used by reflection.
internalFieldName = "Internal"
)
type Field[T any] struct {
// This struct needs to be created via reflection, but reflection can't set private fields.
Internal fieldInternal
}
// re. Data v.s. Component.
// Components have behavior and has a lifecycle.
// while Data doesn't and must be attached to a component.
//
// You can define a component just for storing the data,
// that may contain other information like ref count etc.
// most importantly, the framework needs to know when it's safe to delete the data.
// i.e. the lifecycle of that data component reaches completed.
func NewDataField[D proto.Message](
ctx MutableContext,
d D,
return Field[D]{
Internal: newFieldInternalWithValue(fieldTypeData, d),
}
}
func NewComponentField[C Component](
ctx MutableContext,
c C,
options ...ComponentFieldOption,
opts := &componentFieldOptions{}
for _, o := range options {
o(opts)
}
internal.detached = opts.detached
return Field[C]{
Internal: internal,
}
}
// ComponentPointerTo returns a CHASM field populated with a pointer to the given
// component. The target component must be a proper ancestor of the referring
// component within the same component tree. Pointers to non-ancestor components
// (e.g., siblings, descendants, or components from a different tree) will cause
// the transaction to fail when it is closed.
func ComponentPointerTo[C Component](
ctx MutableContext,
c C,
return Field[C]{
Internal: newFieldInternalWithValue(fieldTypeDeferredPointer, c),
}
}
// DataPointerTo returns a CHASM field populated with a pointer to the given
// message. Pointers are resolved at the time the transaction is closed, and the
// transaction will fail if any pointers cannot be resolved.
func DataPointerTo[D proto.Message](
ctx MutableContext,
d D,
) Field[D] {
return Field[D]{
Internal: newFieldInternalWithValue(fieldTypeDeferredPointer, d),
}
}
// TryGet returns the value of the field and a boolean indicating if the value was found, deserializing if necessary.
// Panics rather than returning an error, as errors are supposed to be handled by the framework as opposed to the
// application, even if the error is an application bug.
var nilT T
// If node is nil, then there is nothing to deserialize from, return value (even if it is also nil).
if f.Internal.node == nil {
}
if !isT {
// nolint:forbidigo // Panic is intended here for framework error handling.
panic(serviceerror.NewInternalf("internal value doesn't implement %s", reflect.TypeFor[T]().Name()))
}
}
switch f.Internal.fieldType() {
case fieldTypeComponent:
if err := f.Internal.node.prepareComponentValue(chasmContext); err != nil {
// nolint:forbidigo // Panic is intended here for framework error handling.
panic(err)
}
// For data fields, T is always a concrete type.
if err := f.Internal.node.prepareDataValue(chasmContext, reflect.TypeFor[T]()); err != nil {
// nolint:forbidigo // Panic is intended here for framework error handling.
panic(err)
}
if err := f.Internal.node.preparePointerValue(); err != nil {
// nolint:forbidigo // Panic is intended here for framework error handling.
panic(err)
}
//nolint:revive // value is guaranteed to be of type []string.
if referencedNode, found := f.Internal.node.root().findNode(path); found {
var err error
switch referencedNode.fieldType() {
case fieldTypeComponent:
err = referencedNode.prepareComponentValue(chasmContext)
case fieldTypeData:
err = referencedNode.prepareDataValue(chasmContext, reflect.TypeFor[T]())
default:
err = serviceerror.NewInternalf("pointer field referenced an unhandled value: %v", referencedNode.fieldType())
}
// nolint:forbidigo // Panic is intended here for framework error handling.
panic(err)
}
}
case fieldTypeDeferredPointer:
// For deferred pointers, return the component directly stored in v
nodeValue = f.Internal.v
default:
// nolint:forbidigo // Panic is intended here for framework error handling.
panic(serviceerror.NewInternalf("unsupported field type: %v", f.Internal.fieldType()))
}
return nilT, false
}
if !isT {
// nolint:forbidigo // Panic is intended here for framework error handling.
panic(serviceerror.NewInternalf("node value doesn't implement %s", reflect.TypeFor[T]().Name()))
}
}
// Get returns the value of the field, deserializing it if necessary.
// Panics rather than returning an error, as errors are supposed to be handled by the framework as opposed to the
// application, even if the error is an application bug.
v, ok := f.TryGet(chasmContext)
if !ok {
// nolint:forbidigo // Panic is intended here for framework error handling.
panic(serviceerror.NewInternalf("field value of type %s not found", reflect.TypeFor[T]().Name()))
}
}
return Field[T]{}
}