go.temporal.io/server/common/util/util.go

197 LOC · 88 covered · 109 uncovered · 42 ranges · 15449 concepts · 35 introducers · 7653 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.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the filego.temporal.io/server/common/namespace/namespace.go · 377 LOCnamespace/namespace.goTestLesserNumberOfPollersThanTasksNoDBErrors · 0 introduced LOCTestLesserNumberOfPoller…TestProcessActivityTimeout_Success · 0 introduced LOCTestProcessActivityTimeo…TestProcessActivityTimeout_Pending · 0 introduced LOCTestProcessActivityTimeo…TestPollWorkflowTaskQueues, TestPollWorkflowTaskQueues · 0 introduced LOCTestPollWorkflowTaskQueu…TestProcessUserTimerTimeout_NoTimerAndWfClosed · 0 introduced LOCTestProcessUserTimerTime…TestProcessUserTimerTimeout_Pending · 0 introduced LOCTestProcessUserTimerTime…timer_queue_standby_task_executor.go ×1 · 1 introduced LOCtimer_queue_standby_task…timer_queue_standby_task_executor.go ×3 · 5 introduced LOCtimer_queue_standby_task…TestTaskQueueManagerGetTaskBatch · 0 introduced LOCTestTaskQueueManagerGetT…TestAddThenConsumeActivities · 0 introduced LOCTestAddThenConsumeActivi…ndc_standby_task_util.go ×1 · 6 introduced LOCndc_standby_task_util.go…timer_queue_standby_task_executor.go ×1 · 1 introduced LOCtimer_queue_standby_task…retried_counts_retryable_starts · 0 introduced LOCretried_counts_retryable…TestExecuteTask_Validate_BackoffEqualToLPTIsEligible · 0 introduced LOCTestExecuteTask_Validate…db.go ×1 · 2 introduced LOCdb.go ×1invoker.go ×1 · 4 introduced LOCinvoker.go ×1invoker.go ×1 · 3 introduced LOCinvoker.go ×1matcher_data.go ×1 · 2 introduced LOCmatcher_data.go ×1executors.go ×3 · 29 introduced LOCexecutors.go ×3pri_matcher.go ×1 · 3 introduced LOCpri_matcher.go ×1user_data_manager.go ×10 · 34 introduced LOCuser_data_manager.go ×10TestValidatorWorksOnRoot · 0 introduced LOCTestValidatorWorksOnRootTestCANByIterations · 0 introduced LOCTestCANByIterationsTestCANBySuggested · 0 introduced LOCTestCANBySuggestedreader.go ×1 · 3 introduced LOCreader.go ×1TestLoadAndSubmitTasks_NoMoreTasks_NoNextSlice · 0 introduced LOCTestLoadAndSubmitTasks_N…pri_matcher.go ×1 · 6 introduced LOCpri_matcher.go ×1matcher_data.go ×3 · 15 introduced LOCmatcher_data.go ×3TestMatchBacklogTask · 0 introduced LOCTestMatchBacklogTaskTestOrder · 0 introduced LOCTestOrdernexus_handler.go ×3 · 10 introduced LOCnexus_handler.go ×3TestMinPriorityZeroMatchesAll · 0 introduced LOCTestMinPriorityZeroMatch…util.go ×1 · 2 introduced LOCutil.go ×1TestNotifyNexusEndpointsOwnershipLost · 0 introduced LOCTestNotifyNexusEndpoints…matcher_data.go ×1 · 2 introduced LOCmatcher_data.go ×1slice.go ×2 · 3 introduced LOCslice.go ×2workflow.go ×3 · 13 introduced LOCworkflow.go ×3user_data_manager.go ×1 · 5 introduced LOCuser_data_manager.go ×1cancels_all_polls_for_worker, cancels_forwarded_polls_with_same_pollerID_on_different_partitions · 0 introduced LOCcancels_all_polls_for_wo…user_data_manager.go ×1 · 2 introduced LOCuser_data_manager.go ×1activity.go ×12 · 72 introduced LOCactivity.go ×12matching_engine.go ×1 · 4 introduced LOCmatching_engine.go ×1nexus_endpoint_client.go ×2 · 13 introduced LOCnexus_endpoint_client.go…namespace_handler.go ×1 · 4 introduced LOCnamespace_handler.go ×1add_to_existing · 0 introduced LOCadd_to_existingnamespace_handler.go ×1 · 1 introduced LOCnamespace_handler.go ×1namespace_handler.go ×1 · 1 introduced LOCnamespace_handler.go ×1version_sets.go ×2 · 13 introduced LOCversion_sets.go ×2util.go ×1 · 2 introduced LOCutil.go ×1TestSpecStartTime · 0 introduced LOCTestSpecStartTimeutil.go ×1 · 2 introduced LOCutil.go ×1util.go ×2 · 3 introduced LOCutil.go ×2util.go ×2 · 3 introduced LOCutil.go ×2util.go ×1 · 2 introduced LOCutil.go ×1util.go ×1 · 1 introduced LOCutil.go ×1util.go ×2 · 4 introduced LOCutil.go ×2util.go ×2 · 3 introduced LOCutil.go ×2query_parser.go ×4 · 7 introduced LOCquery_parser.go ×4query_parser.go ×5 · 13 introduced LOCquery_parser.go ×5util.go ×1 · 2 introduced LOCutil.go ×1util.go ×1 · 4 introduced LOCutil.go ×1util.go ×1 · 2 introduced LOCutil.go ×1util.go ×2 · 5 introduced LOCutil.go ×2namespace.go ×3 · 19 introduced LOCnamespace.go ×3empty_map · 0 introduced LOCempty_mappayload.go ×2 · 3 introduced LOCpayload.go ×2util.go ×1 · 2 introduced LOCutil.go ×1util.go ×1 · 2 introduced LOCutil.go ×1util.go ×1 · 2 introduced LOCutil.go ×1util.go ×2 · 4 introduced LOCutil.go ×2util.go ×2 · 3 introduced LOCutil.go ×2util.go ×1 · 3 introduced LOCutil.go ×1util.go ×1 · 2 introduced LOCutil.go ×1util.go ×1 · 2 introduced LOCutil.go ×1util.go ×1 · 1 introduced LOCutil.go ×1util.go ×1 · 5 introduced LOCutil.go ×1util.go ×1 · 2 introduced LOCutil.go ×1util.go ×1 · 3 introduced LOCutil.go ×1util.go ×1 · 2 introduced LOCutil.go ×1util.go ×1 · 2 introduced LOCutil.go ×1util.go ×1 · 2 introduced LOCutil.go ×1util.go ×1 · 2 introduced LOCutil.go ×1util.go ×1 · 2 introduced LOCutil.go ×1util.go ×1 · 2 introduced LOCutil.go ×1util.go ×1 · 2 introduced LOCutil.go ×1util.go ×1 · 2 introduced LOCutil.go ×1util.go ×1 · 2 introduced LOCutil.go ×1TestUpdateStartedActivityExecutionOptionsDoesNotBumpStartedStamp · introduced test · go.temporal.io/server/chasm/lib/activity/TestUpdateStartedActivityExecutionOptionsDoesNotBumpStartedStampTestUpdateStartedActivit…retried_counts_retryable_starts · introduced test · go.temporal.io/server/chasm/lib/scheduler/TestExecuteTask_EventLog/retried_counts_retryable_startsretried_counts_retryable…TestExecuteTask_Validate_BackoffEqualToLPTIsEligible · introduced test · go.temporal.io/server/chasm/lib/scheduler/TestExecuteTask_Validate_BackoffEqualToLPTIsEligibleTestExecuteTask_Validate…TestParseCloseTime · introduced test · go.temporal.io/server/common/archiver/filestore/TestQueryParserSuite/TestParseCloseTimeTestParseCloseTimeempty_map · introduced test · go.temporal.io/server/common/payload/TestMergeMapOfPayload/empty_mapempty_mapTestDeleteFromMap_CleansUpEmptyOuter · introduced test · go.temporal.io/server/common/util/TestDeleteFromMap_CleansUpEmptyOuterTestDeleteFromMap_Cleans…TestDeleteFromMap_DeletesInnerKey · introduced test · go.temporal.io/server/common/util/TestDeleteFromMap_DeletesInnerKeyTestDeleteFromMap_Delete…TestDeleteFromMap_NonExistentInnerKey · introduced test · go.temporal.io/server/common/util/TestDeleteFromMap_NonExistentInnerKeyTestDeleteFromMap_NonExi…TestDeleteFromMap_NonExistentOuterKey · introduced test · go.temporal.io/server/common/util/TestDeleteFromMap_NonExistentOuterKeyTestDeleteFromMap_NonExi…TestGetOrSetMap_CreatesNew · introduced test · go.temporal.io/server/common/util/TestGetOrSetMap_CreatesNewTestGetOrSetMap_CreatesN…TestGetOrSetMap_ReturnsExisting · introduced test · go.temporal.io/server/common/util/TestGetOrSetMap_ReturnsExistingTestGetOrSetMap_ReturnsE…TestGetOrSetMap_ReturnsSame · introduced test · go.temporal.io/server/common/util/TestGetOrSetMap_ReturnsSameTestGetOrSetMap_ReturnsS…TestGetOrSetNew_CreatesNew · introduced test · go.temporal.io/server/common/util/TestGetOrSetNew_CreatesNewTestGetOrSetNew_CreatesN…TestGetOrSetNew_ReturnsExisting · introduced test · go.temporal.io/server/common/util/TestGetOrSetNew_ReturnsExistingTestGetOrSetNew_ReturnsE…when_given_a_slice_and_a_function_should_apply_function_to_every_element_of_the_original_slice · introduced test · go.temporal.io/server/common/util/TestMapSlice/when_given_a_slice_and_a_function_should_apply_function_to_every_element_of_the_original_slicewhen_given_a_slice_and_a…when_given_an_empty_slice_should_return_empty_slice · introduced test · go.temporal.io/server/common/util/TestMapSlice/when_given_an_empty_slice_should_return_empty_slicewhen_given_an_empty_slic…when_given_nil_as_slice_should_return_nil · introduced test · go.temporal.io/server/common/util/TestMapSlice/when_given_nil_as_slice_should_return_nilwhen_given_nil_as_slice_…should_not_change_the_input_slice_when_replicating · introduced test · go.temporal.io/server/common/util/TestRepeatSlice/should_not_change_the_input_slice_when_replicatingshould_not_change_the_in…should_not_change_the_input_slice_when_truncating · introduced test · go.temporal.io/server/common/util/TestRepeatSlice/should_not_change_the_input_slice_when_truncatingshould_not_change_the_in…when_input_slice_is_empty_should_return_empty · introduced test · go.temporal.io/server/common/util/TestRepeatSlice/when_input_slice_is_empty_should_return_emptywhen_input_slice_is_empt…when_input_slice_is_nil_should_return_nil · introduced test · go.temporal.io/server/common/util/TestRepeatSlice/when_input_slice_is_nil_should_return_nilwhen_input_slice_is_nil_…when_requested_repeat_number_equal_0_should_return_empty_slice · introduced test · go.temporal.io/server/common/util/TestRepeatSlice/when_requested_repeat_number_equal_0_should_return_empty_slicewhen_requested_repeat_nu…when_requested_repeat_number_is_3_should_return_slice_three_times_the_input · introduced test · go.temporal.io/server/common/util/TestRepeatSlice/when_requested_repeat_number_is_3_should_return_slice_three_times_the_inputwhen_requested_repeat_nu…when_requested_repeat_number_is_less_than_0_should_return_empty_slice · introduced test · go.temporal.io/server/common/util/TestRepeatSlice/when_requested_repeat_number_is_less_than_0_should_return_empty_slicewhen_requested_repeat_nu…add_to_existing · introduced test · go.temporal.io/server/service/frontend/TestNamespaceHandlerCommonSuite/TestUpsertCustomSearchAttributesAliases/add_to_existingadd_to_existingalias_already_in_use_by_a_different_field_is_skipped · introduced test · go.temporal.io/server/service/frontend/TestNamespaceHandlerCommonSuite/TestUpsertCustomSearchAttributesAliases/alias_already_in_use_by_a_different_field_is_skippedalias_already_in_use_by_…delete_existing · introduced test · go.temporal.io/server/service/frontend/TestNamespaceHandlerCommonSuite/TestUpsertCustomSearchAttributesAliases/delete_existingdelete_existingfield_already_allocated · introduced test · go.temporal.io/server/service/frontend/TestNamespaceHandlerCommonSuite/TestUpsertCustomSearchAttributesAliases/field_already_allocatedfield_already_allocatedTestNexusInterceptRequest_HeadersSanitization · introduced test · go.temporal.io/server/service/frontend/TestNexusInterceptRequest_HeadersSanitizationTestNexusInterceptReques…TestExecuteStateMachineTimerTask_ExecutesAllAvailableTimers · introduced test · go.temporal.io/server/service/history/TestTimerQueueActiveTaskExecutorSuite/TestExecuteStateMachineTimerTask_ExecutesAllAvailableTimersTestExecuteStateMachineT…TestProcessUserTimerTimeout_NoTimerAndWfClosed · introduced test · go.temporal.io/server/service/history/TestTimerQueueActiveTaskExecutorSuite/TestProcessUserTimerTimeout_NoTimerAndWfClosedTestProcessUserTimerTime…Ack · introduced test · go.temporal.io/server/service/history/TestTimerQueueStandbyTaskExecutorSuite/TestExecuteTimeSkippingTimerTask/AckAckTestProcessActivityTimeout_Pending · introduced test · go.temporal.io/server/service/history/TestTimerQueueStandbyTaskExecutorSuite/TestProcessActivityTimeout_PendingTestProcessActivityTimeo…TestProcessActivityTimeout_Success · introduced test · go.temporal.io/server/service/history/TestTimerQueueStandbyTaskExecutorSuite/TestProcessActivityTimeout_SuccessTestProcessActivityTimeo…TestProcessUserTimerTimeout_Pending · introduced test · go.temporal.io/server/service/history/TestTimerQueueStandbyTaskExecutorSuite/TestProcessUserTimerTimeout_PendingTestProcessUserTimerTime…TestProcessUserTimerTimeout_Success · introduced test · go.temporal.io/server/service/history/TestTimerQueueStandbyTaskExecutorSuite/TestProcessUserTimerTimeout_SuccessTestProcessUserTimerTime…TestProcessWorkflowExecutionTimeout_Pending · introduced test · go.temporal.io/server/service/history/TestTimerQueueStandbyTaskExecutorSuite/TestProcessWorkflowExecutionTimeout_PendingTestProcessWorkflowExecu…TestProcessWorkflowRunTimeout_Pending · introduced test · go.temporal.io/server/service/history/TestTimerQueueStandbyTaskExecutorSuite/TestProcessWorkflowRunTimeout_PendingTestProcessWorkflowRunTi…TestLoadAndSubmitTasks_MoreTasks · introduced test · go.temporal.io/server/service/history/queues/TestReaderSuite/TestLoadAndSubmitTasks_MoreTasksTestLoadAndSubmitTasks_M…TestLoadAndSubmitTasks_NoMoreTasks_NoNextSlice · introduced test · go.temporal.io/server/service/history/queues/TestReaderSuite/TestLoadAndSubmitTasks_NoMoreTasks_NoNextSliceTestLoadAndSubmitTasks_N…TestSelectTasks_NoError · introduced test · go.temporal.io/server/service/history/queues/TestSliceSuite/TestSelectTasks_NoErrorTestSelectTasks_NoErrorcancels_all_polls_for_worker · introduced test · go.temporal.io/server/service/matching/TestCancelOutstandingWorkerPolls/cancels_all_polls_for_workercancels_all_polls_for_wo…cancels_forwarded_polls_with_same_pollerID_on_different_partitions · introduced test · go.temporal.io/server/service/matching/TestCancelOutstandingWorkerPolls/cancels_forwarded_polls_with_same_pollerID_on_different_partitionscancels_forwarded_polls_…partition_API:_cancels_pollers_across_multiple_batched_partitions · introduced test · go.temporal.io/server/service/matching/TestCancelOutstandingWorkerPolls/partition_API:_cancels_pollers_across_multiple_batched_partitionspartition_API:_cancels_p…TestMatchBacklogTask · introduced test · go.temporal.io/server/service/matching/TestMatcherDataSuite/TestMatchBacklogTaskTestMatchBacklogTaskTestMinPriorityZeroMatchesAll · introduced test · go.temporal.io/server/service/matching/TestMatcherDataSuite/TestMinPriorityZeroMatchesAllTestMinPriorityZeroMatch…TestOrder · introduced test · go.temporal.io/server/service/matching/TestMatcherDataSuite/TestOrderTestOrderTestReprocessTasks · introduced test · go.temporal.io/server/service/matching/TestMatcherDataSuite/TestReprocessTasksTestReprocessTasksTestAddThenConsumeActivities · introduced test · go.temporal.io/server/service/matching/TestMatchingEngine_Classic_Suite/TestAddThenConsumeActivitiesTestAddThenConsumeActivi…TestNotifyNexusEndpointsOwnershipLost · introduced test · go.temporal.io/server/service/matching/TestMatchingEngine_Classic_Suite/TestNotifyNexusEndpointsOwnershipLostTestNotifyNexusEndpoints…TestTaskQueueManagerGetTaskBatch · introduced test · go.temporal.io/server/service/matching/TestMatchingEngine_Classic_Suite/TestTaskQueueManagerGetTaskBatchTestTaskQueueManagerGetT…TestLesserNumberOfPollersThanTasksNoDBErrors · introduced test · go.temporal.io/server/service/matching/TestMatchingEngine_Fair_Suite/TestLesserNumberOfPollersThanTasksNoDBErrorsTestLesserNumberOfPoller…TestPollWorkflowTaskQueues · introduced test · go.temporal.io/server/service/matching/TestMatchingEngine_Fair_Suite/TestPollWorkflowTaskQueuesTestPollWorkflowTaskQueu…TestPollWorkflowTaskQueues · introduced test · go.temporal.io/server/service/matching/TestMatchingEngine_Pri_Suite/TestPollWorkflowTaskQueuesTestPollWorkflowTaskQueu…TestForwardPollRetriesOnResourceExhausted · introduced test · go.temporal.io/server/service/matching/TestPriMatcherSuite/TestForwardPollRetriesOnResourceExhaustedTestForwardPollRetriesOn…TestValidatorWorksOnRoot · introduced test · go.temporal.io/server/service/matching/TestPriMatcherSuite/TestValidatorWorksOnRootTestValidatorWorksOnRootTestUserData_CheckPropagation · introduced test · go.temporal.io/server/service/matching/TestUserData_CheckPropagationTestUserData_CheckPropag…Test_ClearTombstones · introduced test · go.temporal.io/server/service/matching/Test_ClearTombstonesTest_ClearTombstonesTestSpecStartTime · introduced test · go.temporal.io/server/service/worker/scheduler/TestSpec/TestSpecStartTimeTestSpecStartTimeTestCANByIterations · introduced test · go.temporal.io/server/service/worker/scheduler/TestWorkflow/TestCANByIterationsTestCANByIterationsTestCANBySuggested · introduced test · go.temporal.io/server/service/worker/scheduler/TestWorkflow/TestCANBySuggestedTestCANBySuggestedTestCompileError · introduced test · go.temporal.io/server/service/worker/scheduler/TestWorkflow/TestCompileErrorTestCompileErrorFocused file · go.temporal.io/server/common/util/util.go · 197 LOCutil/util.go

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 // util contains small standalone utility functions. This should have no
2 // dependencies on other server packages.
3 package util
4
5 import (
6 "cmp"
7 "context"
8 "maps"
9 "slices"
10 "time"
11 )
12
13 // MinTime returns the earlier of two given time.Time
14 > func MinTime(a, b time.Time) time.Time { util.go ×1
15 > if a.Before(b) {
16 > return a util.go ×1
17 > }
18 > return b util.go ×1
19 }
20
21 // MaxTime returns the latest of the given time.Time values.
22 > func MaxTime(first time.Time, rest ...time.Time) time.Time { util.go ×2
23 > latest := first
24 > for _, t := range rest {
25 > if t.After(latest) {
26 > latest = t util.go ×1
27 > }
28 }
29 > return latest util.go ×2
30 }
31
32 // NextAlignedTime returns the earliest time after `t` that is aligned to an integer multiple
33 // of `align` since the unix epoch.
34 func NextAlignedTime(t time.Time, align time.Duration) time.Time {
35 return time.Unix(0, (t.UnixNano()/int64(align)+1)*int64(align))
36 }
37
38 // SortSlice sorts the given slice of an ordered type.
39 // Sort is not guaranteed to be stable.
40 func SortSlice[S ~[]E, E cmp.Ordered](slice S) {
41 slices.Sort(slice)
42 }
43
44 // SliceHead returns the first n elements of s. n may be greater than len(s).
45 > func SliceHead[S ~[]E, E any](s S, n int) S { util.go ×2
46 > if n < len(s) {
47 return s[:n]
48 }
49 > return s util.go ×2
50 }
51
52 // SliceTail returns the last n elements of s. n may be greater than len(s).
53 > func SliceTail[S ~[]E, E any](s S, n int) S { util.go ×2
54 > if extra := len(s) - n; extra > 0 {
55 > return s[extra:] util.go ×1
56 > }
57 > return s util.go ×2
58 }
59
60 // CloneMapNonNil is like maps.Clone except it can't return nil, it will return an empty map instead.
61 > func CloneMapNonNil[M ~map[K]V, K comparable, V any](m M) M { util.go ×2
62 > m = maps.Clone(m)
63 > if m == nil {
64 > m = make(M) util.go ×1
65 > }
66 > return m util.go ×2
67 }
68
69 // InverseMap creates the inverse map, ie., for a key-value map, it builds the value-key map.
70 > func InverseMap[M ~map[K]V, K, V comparable](m M) map[V]K { namespace.go ×3
71 > if m == nil {
72 > return nil
73 > }
74 > invm := make(map[V]K, len(m)) util.go ×2
75 > for k, v := range m {
76 > invm[v] = k util.go ×1
77 > }
78 > return invm util.go ×2
79 }
80
81 // GetOrSetNew looks up k in m and returns the result. If it's not present, it uses `new` to
82 // allocate an new value type and sets that in the map, then returns it.
83 > func GetOrSetNew[M ~map[K]*V, K comparable, V any](m M, k K) *V { util.go ×1
84 > if v, ok := m[k]; ok {
85 > return v util.go ×1
86 > }
87 > v := new(V) util.go ×1
88 > m[k] = v
89 > return v
90 }
91
92 // GetOrSetMap looks up k in m, a two-level map, and returns the result. If it's not present,
93 // it uses `make` to allocate new second-level map and sets that in the first map, then returns it.
94 > func GetOrSetMap[M ~map[K]M2, M2 ~map[K2]V, K, K2 comparable, V any](m M, k K) M2 { util.go ×1
95 > if m2, ok := m[k]; ok {
96 > return m2 util.go ×1
97 > }
98 > m2 := make(M2) util.go ×1
99 > m[k] = m2
100 > return m2
101 }
102
103 // DeleteFromMap deletes k2 from the nested map m[k]. If the inner map becomes empty
104 // after deletion, k is also removed from m to prevent memory leaks.
105 > func DeleteFromMap[M ~map[K]M2, M2 ~map[K2]V, K, K2 comparable, V any](m M, k K, k2 K2) { util.go ×1
106 > if m2, ok := m[k]; ok {
107 > delete(m2, k2) util.go ×1
108 > if len(m2) == 0 {
109 > delete(m, k) util.go ×1
110 > }
111 }
112 }
113
114 // MapConcurrent concurrently maps a function over input and fails fast on error.
115 func MapConcurrent[IN any, OUT any](input []IN, mapper func(IN) (OUT, error)) ([]OUT, error) {
116 errorsCh := make(chan error, len(input))
117 results := make([]OUT, len(input))
118
119 for i, in := range input {
120 go func() {
121 var err error
122 results[i], err = mapper(in)
123 errorsCh <- err
124 }()
125 }
126 for range input {
127 if err := <-errorsCh; err != nil {
128 return nil, err
129 }
130 }
131 return results, nil
132 }
133
134 // MapSlice given slice xs []T and f(T) S produces slice []S by applying f to every element of xs
135 > func MapSlice[T, S any](xs []T, f func(T) S) []S { util.go ×1
136 > if xs == nil {
137 > return nil util.go ×1
138 > }
139 > result := make([]S, len(xs)) util.go ×2
140 > for i, s := range xs {
141 > result[i] = f(s) util.go ×1
142 > }
143 > return result util.go ×2
144 }
145
146 // FilterSlice iterates over elements of a slice, returning a new slice of all elements predicate returns true for.
147 > func FilterSlice[T any](in []T, predicate func(T) bool) []T { util.go ×2
148 > var out []T
149 > for _, elem := range in {
150 > if predicate(elem) { util.go ×1
151 > out = append(out, elem) util.go ×1
152 > }
153 }
154 > return out util.go ×2
155 }
156
157 // FoldSlice folds left a slice using given reducer function and initial value.
158 func FoldSlice[T any, A any](in []T, initializer A, reducer func(A, T) A) A {
159 acc := initializer
160 for _, val := range in {
161 acc = reducer(acc, val)
162 }
163 return acc
164 }
165
166 // RepeatSlice given slice and a number (n) produces a new slice containing original slice n times
167 // if n is non-positive will produce nil
168 > func RepeatSlice[T any](xs []T, n int) []T { util.go ×1
169 > if xs == nil || n <= 0 {
170 > return nil util.go ×1
171 > }
172 > ys := make([]T, n*len(xs)) util.go ×1
173 > for i := range n {
174 > copy(ys[i*len(xs):], xs)
175 > }
176 > return ys
177 }
178
179 // Ptr returns a pointer to a copy of v.
180 //
181 //go:fix inline
182 func Ptr[T any](v T) *T {
183 return new(v)
184 }
185
186 // InterruptibleSleep is like time.Sleep but can be interrupted by a context.
187 // Returns context error if interrupted, otherwise nil.
188 > func InterruptibleSleep(ctx context.Context, timeout time.Duration) error { util.go ×1
189 > timer := time.NewTimer(timeout)
190 > defer timer.Stop()
191 > select {
192 > case <-timer.C: util.go ×1
193 > return nil
194 > case <-ctx.Done(): util.go ×1
195 > return ctx.Err()
196 }
197 }