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.

1 package chasm
2
3 import (
4 "reflect"
5
6 "go.temporal.io/api/serviceerror"
7 "google.golang.org/protobuf/proto"
8 )
9
10 const (
11 // Used by reflection.
12 internalFieldName = "Internal"
13 )
14
15 type Field[T any] struct {
16 // This struct needs to be created via reflection, but reflection can't set private fields.
17 Internal fieldInternal
18 }
19
20 // re. Data v.s. Component.
21 // Components have behavior and has a lifecycle.
22 // while Data doesn't and must be attached to a component.
23 //
24 // You can define a component just for storing the data,
25 // that may contain other information like ref count etc.
26 // most importantly, the framework needs to know when it's safe to delete the data.
27 // i.e. the lifecycle of that data component reaches completed.
28 func NewDataField[D proto.Message](
29 ctx MutableContext,
30 d D,
31 > ) Field[D] { field.go ×1
32 > return Field[D]{
33 > Internal: newFieldInternalWithValue(fieldTypeData, d),
34 > }
35 > }
36
37 func NewComponentField[C Component](
38 ctx MutableContext,
39 c C,
40 options ...ComponentFieldOption,
41 > ) Field[C] { field.go ×2
42 > opts := &componentFieldOptions{}
43 > for _, o := range options {
44 o(opts)
45 }
46 > internal := newFieldInternalWithValue(fieldTypeComponent, c) field.go ×2
47 > internal.detached = opts.detached
48 > return Field[C]{
49 > Internal: internal,
50 > }
51 }
52
53 // ComponentPointerTo returns a CHASM field populated with a pointer to the given
54 // component. The target component must be a proper ancestor of the referring
55 // component within the same component tree. Pointers to non-ancestor components
56 // (e.g., siblings, descendants, or components from a different tree) will cause
57 // the transaction to fail when it is closed.
58 func ComponentPointerTo[C Component](
59 ctx MutableContext,
60 c C,
61 > ) Field[C] { tree.go ×9
62 > return Field[C]{
63 > Internal: newFieldInternalWithValue(fieldTypeDeferredPointer, c),
64 > }
65 > }
66
67 // DataPointerTo returns a CHASM field populated with a pointer to the given
68 // message. Pointers are resolved at the time the transaction is closed, and the
69 // transaction will fail if any pointers cannot be resolved.
70 func DataPointerTo[D proto.Message](
71 ctx MutableContext,
72 d D,
73 ) Field[D] {
74 return Field[D]{
75 Internal: newFieldInternalWithValue(fieldTypeDeferredPointer, d),
76 }
77 }
78
79 // TryGet returns the value of the field and a boolean indicating if the value was found, deserializing if necessary.
80 // Panics rather than returning an error, as errors are supposed to be handled by the framework as opposed to the
81 // application, even if the error is an application bug.
82 > func (f Field[T]) TryGet(chasmContext Context) (T, bool) { field.go ×1
83 > var nilT T
84 >
85 > // If node is nil, then there is nothing to deserialize from, return value (even if it is also nil).
86 > if f.Internal.node == nil {
87 > if f.Internal.v == nil { field.go ×1
88 > return nilT, false field.go ×1
89 > }
90 > vT, isT := f.Internal.v.(T) field.go ×2
91 > if !isT {
92 // nolint:forbidigo // Panic is intended here for framework error handling.
93 panic(serviceerror.NewInternalf("internal value doesn't implement %s", reflect.TypeFor[T]().Name()))
94 }
95 > return vT, true field.go ×2
96 }
97
98 > var nodeValue any field.go ×5
99 > switch f.Internal.fieldType() {
100 > case fieldTypeComponent:
101 > if err := f.Internal.node.prepareComponentValue(chasmContext); err != nil {
102 // nolint:forbidigo // Panic is intended here for framework error handling.
103 panic(err)
104 }
105 > nodeValue = f.Internal.node.value field.go ×5
106 > case fieldTypeData: tree.go ×4
107 > // For data fields, T is always a concrete type.
108 > if err := f.Internal.node.prepareDataValue(chasmContext, reflect.TypeFor[T]()); err != nil {
109 // nolint:forbidigo // Panic is intended here for framework error handling.
110 panic(err)
111 }
112 > nodeValue = f.Internal.node.value tree.go ×4
113 > case fieldTypePointer: field.go ×4
114 > if err := f.Internal.node.preparePointerValue(); err != nil {
115 // nolint:forbidigo // Panic is intended here for framework error handling.
116 panic(err)
117 }
118 //nolint:revive // value is guaranteed to be of type []string.
119 > path := f.Internal.value().([]string) field.go ×4
120 > if referencedNode, found := f.Internal.node.root().findNode(path); found {
121 > var err error
122 > switch referencedNode.fieldType() {
123 > case fieldTypeComponent:
124 > err = referencedNode.prepareComponentValue(chasmContext)
125 case fieldTypeData:
126 err = referencedNode.prepareDataValue(chasmContext, reflect.TypeFor[T]())
127 default:
128 err = serviceerror.NewInternalf("pointer field referenced an unhandled value: %v", referencedNode.fieldType())
129 }
130 > if err != nil { field.go ×4
131 // nolint:forbidigo // Panic is intended here for framework error handling.
132 panic(err)
133 }
134 > nodeValue = referencedNode.value field.go ×4
135 }
136 case fieldTypeDeferredPointer:
137 // For deferred pointers, return the component directly stored in v
138 nodeValue = f.Internal.v
139 default:
140 // nolint:forbidigo // Panic is intended here for framework error handling.
141 panic(serviceerror.NewInternalf("unsupported field type: %v", f.Internal.fieldType()))
142 }
143
144 > if nodeValue == nil { field.go ×5
145 return nilT, false
146 }
147 > vT, isT := nodeValue.(T) field.go ×5
148 > if !isT {
149 // nolint:forbidigo // Panic is intended here for framework error handling.
150 panic(serviceerror.NewInternalf("node value doesn't implement %s", reflect.TypeFor[T]().Name()))
151 }
152 > return vT, true field.go ×5
153 }
154
155 // Get returns the value of the field, deserializing it if necessary.
156 // Panics rather than returning an error, as errors are supposed to be handled by the framework as opposed to the
157 // application, even if the error is an application bug.
158 > func (f Field[T]) Get(chasmContext Context) T { field.go ×2
159 > v, ok := f.TryGet(chasmContext)
160 > if !ok {
161 // nolint:forbidigo // Panic is intended here for framework error handling.
162 panic(serviceerror.NewInternalf("field value of type %s not found", reflect.TypeFor[T]().Name()))
163 }
164 > return v field.go ×2
165 }
166
167 > func NewEmptyField[T any]() Field[T] { field.go ×1
168 > return Field[T]{}
169 > }