go.temporal.io/server/common/payload/payload.go

127 LOC · 55 covered · 72 uncovered · 24 ranges · 22250 concepts · 20 introducers · 11071 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 payload
2
3 import (
4 "bytes"
5 "maps"
6
7 commonpb "go.temporal.io/api/common/v1"
8 "go.temporal.io/sdk/converter"
9 "go.temporal.io/server/common/util"
10 )
11
12 var (
13 defaultDataConverter = converter.GetDefaultDataConverter()
14
15 nilPayload, _ = Encode(nil) // Data: nil
16 nilSlicePayload, _ = Encode([]string(nil)) // Data: "null" (nil value is json encoded as null)
17 emptySlicePayload, _ = Encode([]string{}) // Data: "[]"
18 )
19
20 > func EncodeString(str string) *commonpb.Payload { payload.go ×1
21 > // Error can be safely ignored here because string always can be converted to JSON
22 > p, _ := defaultDataConverter.ToPayload(str)
23 > return p
24 > }
25
26 > func EncodeBytes(bytes []byte) *commonpb.Payload { payload.go ×1
27 > // Error can be safely ignored here because []byte always can be raw encoded
28 > p, _ := defaultDataConverter.ToPayload(bytes)
29 > return p
30 > }
31
32 > func Encode(value any) (*commonpb.Payload, error) { payload.go ×1
33 > return defaultDataConverter.ToPayload(value)
34 > }
35
36 > func Decode(p *commonpb.Payload, valuePtr any) error { payload.go ×1
37 > return defaultDataConverter.FromPayload(p, valuePtr)
38 > }
39
40 > func ToString(p *commonpb.Payload) string { payload.go ×1
41 > return defaultDataConverter.ToString(p)
42 > }
43
44 // MergeMapOfPayload returns a new map resulting from merging map `src` into `dst`.
45 // If a key in `src` already exists in `dst`, then the value in `src` replaces
46 // the value in `dst`.
47 // If a key in `src` has nil or empty slice payload value, then it deletes
48 // the key from `dst` if it exists.
49 // For example:
50 //
51 // dst := map[string]*commonpb.Payload{
52 // "key1": EncodeString("value1"),
53 // "key2": EncodeString("value2"),
54 // }
55 // src := map[string]*commonpb.Payload{
56 // "key1": EncodeString("newValue1"),
57 // "key2": nilPayload,
58 // }
59 // res := MergeMapOfPayload(dst, src)
60 //
61 // The resulting map `res` is:
62 //
63 // map[string]*commonpb.Payload{
64 // "key1": EncodeString("newValue1"),
65 // }
66 func MergeMapOfPayload(
67 dst map[string]*commonpb.Payload,
68 src map[string]*commonpb.Payload,
69 > ) map[string]*commonpb.Payload { payload.go ×1
70 > if src == nil {
71 > return maps.Clone(dst) payload.go ×1
72 > }
73 > res := util.CloneMapNonNil(dst) payload.go ×2
74 > for k, v := range src {
75 > if isNilPayload(v) { payload.go ×3
76 > delete(res, k) payload.go ×1
77 > } else { payload.go ×3
78 > res[k] = v payload.go ×1
79 > }
80 }
81 > return res payload.go ×2
82 }
83
84 // isNilPayload checks if the payload is equivalent to nil.
85 // There are four cases:
86 // - payload object is nil
87 // - payload's data is nil
88 // - payload's data is "null" (json encoded value for nil objects)
89 // - payload's data is "[]" (empty slice for backwards compatibility)
90 > func isNilPayload(p *commonpb.Payload) bool { payload.go ×3
91 > return p == nil ||
92 > bytes.Equal(p.Data, nilPayload.Data) ||
93 > bytes.Equal(p.Data, nilSlicePayload.Data) ||
94 > bytes.Equal(p.Data, emptySlicePayload.Data)
95 > }
96
97 // FilterNilSearchAttributes returns a new SearchAttributes with nil/empty payload values filtered out.
98 // If the input is nil or all values are nil/empty, returns nil.
99 // This is used to filter out nil search attributes from workflow start and continue-as-new events.
100 // Reuses MergeMapOfPayload which already handles nil payload filtering.
101 > func FilterNilSearchAttributes(sa *commonpb.SearchAttributes) *commonpb.SearchAttributes { payload.go ×1
102 > if sa == nil || len(sa.GetIndexedFields()) == 0 {
103 > return nil payload.go ×1
104 > }
105
106 > filtered := MergeMapOfPayload(nil, sa.GetIndexedFields()) payload.go ×1
107 > if len(filtered) == 0 {
108 > return nil payload.go ×1
109 > }
110 > return &commonpb.SearchAttributes{IndexedFields: filtered} payload.go ×1
111 }
112
113 // FilterNilMemo returns a new Memo with nil/empty payload values filtered out.
114 // If the input is nil or all values are nil/empty, returns nil.
115 // This is used to filter out nil memo fields from workflow start, continue-as-new, and modify-properties events.
116 // Reuses MergeMapOfPayload which already handles nil payload filtering.
117 > func FilterNilMemo(memo *commonpb.Memo) *commonpb.Memo { payload.go ×1
118 > if memo == nil || len(memo.GetFields()) == 0 {
119 > return nil payload.go ×1
120 > }
121
122 > filtered := MergeMapOfPayload(nil, memo.GetFields()) payload.go ×2
123 > if len(filtered) == 0 {
124 > return nil payload.go ×1
125 > }
126 > return &commonpb.Memo{Fields: filtered} payload.go ×2
127 }