Skip to content

Commit 560562e

Browse files
committed
Update implementation to use separate mapper and search attribute provider
1 parent d6a2368 commit 560562e

File tree

10 files changed

+163
-108
lines changed

10 files changed

+163
-108
lines changed

chasm/component.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,46 @@ func (UnimplementedComponent) mustEmbedUnimplementedComponent() {}
3838

3939
var UnimplementedComponentT = reflect.TypeFor[UnimplementedComponent]()
4040

41+
type ComponentSearchAttributesProvider struct {
42+
searchAttributes map[string]VisibilityValue
43+
memo map[string]VisibilityValue
44+
aliasToField map[string]string
45+
fieldToAlias map[string]string
46+
}
47+
48+
func (s *ComponentSearchAttributesProvider) SearchAttributes(ctx Context) map[string]VisibilityValue {
49+
return s.searchAttributes
50+
}
51+
52+
func (s *ComponentSearchAttributesProvider) Memo(ctx Context) map[string]VisibilityValue {
53+
return s.memo
54+
}
55+
56+
func (s *ComponentSearchAttributesProvider) UpsertSearchAttributes(attributes ...SearchAttributeKeyValue) {
57+
if s.searchAttributes == nil {
58+
s.searchAttributes = make(map[string]VisibilityValue)
59+
}
60+
if s.aliasToField == nil {
61+
s.aliasToField = make(map[string]string)
62+
}
63+
if s.fieldToAlias == nil {
64+
s.fieldToAlias = make(map[string]string)
65+
}
66+
for _, attribute := range attributes {
67+
s.searchAttributes[attribute.field] = attribute.value
68+
s.aliasToField[attribute.alias] = attribute.field
69+
s.fieldToAlias[attribute.field] = attribute.alias
70+
}
71+
}
72+
73+
func (s *ComponentSearchAttributesProvider) GetAlias(field string) string {
74+
return s.fieldToAlias[field]
75+
}
76+
77+
func (s *ComponentSearchAttributesProvider) GetField(alias string) string {
78+
return s.aliasToField[alias]
79+
}
80+
4181
// Shall it be named ComponentLifecycleState?
4282
type LifecycleState int
4383

chasm/lib/tests/payload.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var (
3131
type (
3232
PayloadStore struct {
3333
chasm.UnimplementedComponent
34+
chasm.ComponentSearchAttributesProvider
3435

3536
State *testspb.TestPayloadStore
3637

@@ -147,13 +148,13 @@ func (s *PayloadStore) LifecycleState(
147148

148149
// SearchAttributes implements chasm.VisibilitySearchAttributesProvider interface
149150
func (s *PayloadStore) SearchAttributes(
150-
_ chasm.Context,
151-
) []*chasm.SearchAttribute {
151+
ctx chasm.Context,
152+
) map[string]chasm.VisibilityValue {
152153
// TODO: UpsertSearchAttribute as well when CHASM framework supports Per-Component SearchAttributes
153154
// For now, we just update a random existing pre-defined SA to make sure the logic works.
154155
attr := chasm.NewSearchAttributeKeywordByAlias(TestKeywordSAFieldName, TestKeywordSAFieldName)
155-
attr.SetValue(TestKeywordSAFieldValue)
156-
return []*chasm.SearchAttribute{&attr.SearchAttribute}
156+
s.UpsertSearchAttributes(attr.SetValue(TestKeywordSAFieldValue))
157+
return s.ComponentSearchAttributesProvider.SearchAttributes(ctx)
157158
}
158159

159160
// Memo implements chasm.VisibilityMemoProvider interface

chasm/registrable_component.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,17 @@ func WithSearchAttributes(searchAttributes []*SearchAttribute) RegistrableCompon
6868
for _, sa := range searchAttributes {
6969
alias := sa.GetAlias()
7070

71-
if existingKey, exists := aliasToKey[alias]; exists {
71+
if existingField, exists := aliasToKey[alias]; exists {
7272
//nolint:forbidigo // panic is appropriate during component registration setup
7373
panic(fmt.Sprintf(
74-
"duplicate search attribute alias %q: keys %q and %q both map to the same alias",
74+
"duplicate search attribute alias %q: fields %q and %q both map to the same alias",
7575
alias,
76-
existingKey,
77-
sa.GetKey(),
76+
existingField,
77+
sa.GetField(),
7878
))
7979
}
8080

81-
aliasToKey[alias] = sa.GetKey()
81+
aliasToKey[alias] = sa.GetField()
8282
}
8383
}
8484
}

chasm/search_attribute.go

Lines changed: 83 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@ import (
99

1010
type (
1111
SearchAttribute struct {
12-
key string
13-
// alias refers to a fully formed schema field, which is either a Predefined or CHASM search attribute
14-
alias string
12+
// alias refers to the user defined name of the search attribute
13+
alias string
14+
// field refers to a fully formed schema field, which is either a Predefined or CHASM search attribute
15+
field string
1516
valueType enumspb.IndexedValueType
16-
value any
17+
}
18+
19+
SearchAttributeKeyValue struct {
20+
alias string
21+
field string
22+
value VisibilityValue
1723
}
1824

1925
SearchAttributeBool struct {
@@ -64,165 +70,185 @@ func ResolveFieldName(valueType enumspb.IndexedValueType, index int) string {
6470
}
6571
}
6672

67-
// GetKey returns the search attribute key.
68-
func (s *SearchAttribute) GetKey() string {
69-
return s.key
70-
}
71-
72-
// GetAlias returns the search attribute alias (field name).
73+
// GetAlias returns the search attribute alias.
7374
func (s *SearchAttribute) GetAlias() string {
7475
return s.alias
7576
}
7677

78+
// GetField returns the search attribute field name.
79+
func (s *SearchAttribute) GetField() string {
80+
return s.field
81+
}
82+
7783
// GetValueType returns the indexed value type.
7884
func (s *SearchAttribute) GetValueType() enumspb.IndexedValueType {
7985
return s.valueType
8086
}
8187

82-
// GetValue returns the search attribute value.
83-
func (s *SearchAttribute) GetValue() any {
84-
return s.value
85-
}
86-
87-
func NewSearchAttributeBoolByIndex(key string, index int) *SearchAttributeBool {
88+
func NewSearchAttributeBoolByIndex(alias string, index int) *SearchAttributeBool {
8889
return &SearchAttributeBool{
8990
SearchAttribute: SearchAttribute{
90-
key: key,
91-
alias: ResolveFieldName(enumspb.INDEXED_VALUE_TYPE_BOOL, index),
91+
alias: alias,
92+
field: ResolveFieldName(enumspb.INDEXED_VALUE_TYPE_BOOL, index),
9293
valueType: enumspb.INDEXED_VALUE_TYPE_BOOL,
9394
},
9495
}
9596
}
9697

97-
func NewSearchAttributeBoolByAlias(key string, alias string) *SearchAttributeBool {
98+
func NewSearchAttributeBoolByAlias(alias string, field string) *SearchAttributeBool {
9899
return &SearchAttributeBool{
99100
SearchAttribute: SearchAttribute{
100-
key: key,
101101
alias: alias,
102+
field: field,
102103
valueType: enumspb.INDEXED_VALUE_TYPE_BOOL,
103104
},
104105
}
105106
}
106107

107-
func (s *SearchAttributeBool) SetValue(value bool) {
108-
s.value = value
108+
func (s SearchAttributeBool) SetValue(value bool) SearchAttributeKeyValue {
109+
return SearchAttributeKeyValue{
110+
alias: s.alias,
111+
field: s.field,
112+
value: VisibilityValueBool(value),
113+
}
109114
}
110115

111-
func NewSearchAttributeTimeByIndex(key string, index int) *SearchAttributeTime {
116+
func NewSearchAttributeTimeByIndex(alias string, index int) *SearchAttributeTime {
112117
return &SearchAttributeTime{
113118
SearchAttribute: SearchAttribute{
114-
key: key,
115-
alias: ResolveFieldName(enumspb.INDEXED_VALUE_TYPE_DATETIME, index),
119+
alias: alias,
120+
field: ResolveFieldName(enumspb.INDEXED_VALUE_TYPE_DATETIME, index),
116121
valueType: enumspb.INDEXED_VALUE_TYPE_DATETIME,
117122
},
118123
}
119124
}
120125

121-
func NewSearchAttributeTimeByAlias(key string, alias string) *SearchAttributeTime {
126+
func NewSearchAttributeTimeByAlias(alias string, field string) *SearchAttributeTime {
122127
return &SearchAttributeTime{
123128
SearchAttribute: SearchAttribute{
124-
key: key,
125129
alias: alias,
130+
field: field,
126131
valueType: enumspb.INDEXED_VALUE_TYPE_DATETIME,
127132
},
128133
}
129134
}
130135

131-
func (s *SearchAttributeTime) SetValue(value time.Time) {
132-
s.value = value
136+
func (s SearchAttributeTime) SetValue(value time.Time) SearchAttributeKeyValue {
137+
return SearchAttributeKeyValue{
138+
alias: s.alias,
139+
field: s.field,
140+
value: VisibilityValueTime(value),
141+
}
133142
}
134143

135-
func NewSearchAttributeFloat64ByIndex(key string, index int) *SearchAttributeFloat64 {
144+
func NewSearchAttributeFloat64ByIndex(alias string, index int) *SearchAttributeFloat64 {
136145
return &SearchAttributeFloat64{
137146
SearchAttribute: SearchAttribute{
138-
key: key,
139-
alias: ResolveFieldName(enumspb.INDEXED_VALUE_TYPE_DOUBLE, index),
147+
alias: alias,
148+
field: ResolveFieldName(enumspb.INDEXED_VALUE_TYPE_DOUBLE, index),
140149
valueType: enumspb.INDEXED_VALUE_TYPE_DOUBLE,
141150
},
142151
}
143152
}
144153

145-
func NewSearchAttributeFloat64ByAlias(key string, alias string) *SearchAttributeFloat64 {
154+
func NewSearchAttributeFloat64ByAlias(alias string, field string) *SearchAttributeFloat64 {
146155
return &SearchAttributeFloat64{
147156
SearchAttribute: SearchAttribute{
148-
key: key,
149157
alias: alias,
158+
field: field,
150159
valueType: enumspb.INDEXED_VALUE_TYPE_DOUBLE,
151160
},
152161
}
153162
}
154163

155-
func (s *SearchAttributeFloat64) SetValue(value float64) {
156-
s.value = value
164+
func (s SearchAttributeFloat64) SetValue(value float64) SearchAttributeKeyValue {
165+
return SearchAttributeKeyValue{
166+
alias: s.alias,
167+
field: s.field,
168+
value: VisibilityValueFloat64(value),
169+
}
157170
}
158171

159-
func NewSearchAttributeKeywordByIndex(key string, index int) *SearchAttributeKeyword {
172+
func NewSearchAttributeKeywordByIndex(alias string, index int) *SearchAttributeKeyword {
160173
return &SearchAttributeKeyword{
161174
SearchAttribute: SearchAttribute{
162-
key: key,
163-
alias: ResolveFieldName(enumspb.INDEXED_VALUE_TYPE_KEYWORD, index),
175+
alias: alias,
176+
field: ResolveFieldName(enumspb.INDEXED_VALUE_TYPE_KEYWORD, index),
164177
valueType: enumspb.INDEXED_VALUE_TYPE_KEYWORD,
165178
},
166179
}
167180
}
168181

169-
func NewSearchAttributeKeywordByAlias(key string, alias string) *SearchAttributeKeyword {
182+
func NewSearchAttributeKeywordByAlias(alias string, field string) *SearchAttributeKeyword {
170183
return &SearchAttributeKeyword{
171184
SearchAttribute: SearchAttribute{
172-
key: key,
173185
alias: alias,
186+
field: field,
174187
valueType: enumspb.INDEXED_VALUE_TYPE_KEYWORD,
175188
},
176189
}
177190
}
178191

179-
func (s *SearchAttributeKeyword) SetValue(value string) {
180-
s.value = value
192+
func (s SearchAttributeKeyword) SetValue(value string) SearchAttributeKeyValue {
193+
return SearchAttributeKeyValue{
194+
alias: s.alias,
195+
field: s.field,
196+
value: VisibilityValueString(value),
197+
}
181198
}
182199

183-
func NewSearchAttributeTextByIndex(key string, index int) *SearchAttributeText {
200+
func NewSearchAttributeTextByIndex(alias string, index int) *SearchAttributeText {
184201
return &SearchAttributeText{
185202
SearchAttribute: SearchAttribute{
186-
key: key,
187-
alias: ResolveFieldName(enumspb.INDEXED_VALUE_TYPE_TEXT, index),
203+
alias: alias,
204+
field: ResolveFieldName(enumspb.INDEXED_VALUE_TYPE_TEXT, index),
188205
valueType: enumspb.INDEXED_VALUE_TYPE_TEXT,
189206
},
190207
}
191208
}
192209

193-
func NewSearchAttributeTextByAlias(key string, alias string) *SearchAttributeText {
210+
func NewSearchAttributeTextByAlias(alias string, field string) *SearchAttributeText {
194211
return &SearchAttributeText{
195212
SearchAttribute: SearchAttribute{
196-
key: key,
197213
alias: alias,
214+
field: field,
198215
valueType: enumspb.INDEXED_VALUE_TYPE_TEXT,
199216
},
200217
}
201218
}
202-
func (s *SearchAttributeText) SetValue(value string) {
203-
s.value = value
219+
220+
func (s SearchAttributeText) SetValue(value string) SearchAttributeKeyValue {
221+
return SearchAttributeKeyValue{
222+
alias: s.alias,
223+
field: s.field,
224+
value: VisibilityValueString(value),
225+
}
204226
}
205227

206-
func NewSearchAttributeKeywordListByIndex(key string, index int) *SearchAttributeKeywordList {
228+
func NewSearchAttributeKeywordListByIndex(alias string, index int) *SearchAttributeKeywordList {
207229
return &SearchAttributeKeywordList{
208230
SearchAttribute: SearchAttribute{
209-
key: key,
210-
alias: ResolveFieldName(enumspb.INDEXED_VALUE_TYPE_KEYWORD_LIST, index),
231+
alias: alias,
232+
field: ResolveFieldName(enumspb.INDEXED_VALUE_TYPE_KEYWORD_LIST, index),
211233
valueType: enumspb.INDEXED_VALUE_TYPE_KEYWORD_LIST,
212234
},
213235
}
214236
}
215237

216-
func NewSearchAttributeKeywordListByAlias(key string, alias string) *SearchAttributeKeywordList {
238+
func NewSearchAttributeKeywordListByAlias(alias string, field string) *SearchAttributeKeywordList {
217239
return &SearchAttributeKeywordList{
218240
SearchAttribute: SearchAttribute{
219-
key: key,
220241
alias: alias,
242+
field: field,
221243
valueType: enumspb.INDEXED_VALUE_TYPE_KEYWORD_LIST,
222244
},
223245
}
224246
}
225247

226-
func (s *SearchAttributeKeywordList) SetValue(value []string) {
227-
s.value = value
248+
func (s SearchAttributeKeywordList) SetValue(value []string) SearchAttributeKeyValue {
249+
return SearchAttributeKeyValue{
250+
alias: s.alias,
251+
field: s.field,
252+
value: VisibilityValueStringSlice(value),
253+
}
228254
}

chasm/test_component_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type (
1919
protoMessageType = persistencespb.WorkflowExecutionState // Random proto message.
2020
TestComponent struct {
2121
UnimplementedComponent
22+
ComponentSearchAttributesProvider
2223

2324
ComponentData *protoMessageType
2425
SubComponent1 Field[*TestSubComponent1]
@@ -95,10 +96,10 @@ func (tc *TestComponent) Fail(_ MutableContext) {
9596
}
9697

9798
// SearchAttributes implements VisibilitySearchAttributesProvider interface.
98-
func (tc *TestComponent) SearchAttributes(_ Context) []*SearchAttribute {
99+
func (tc *TestComponent) SearchAttributes(ctx Context) map[string]VisibilityValue {
99100
attr := NewSearchAttributeTimeByAlias(testComponentStartTimeSAKey, testComponentStartTimeSAKey)
100-
attr.SetValue(tc.ComponentData.GetStartTime().AsTime())
101-
return []*SearchAttribute{&attr.SearchAttribute}
101+
tc.UpsertSearchAttributes(attr.SetValue(tc.ComponentData.GetStartTime().AsTime()))
102+
return tc.ComponentSearchAttributesProvider.SearchAttributes(ctx)
102103
}
103104

104105
// Memo implements VisibilityMemoProvider interface.

0 commit comments

Comments
 (0)