|
1 | 1 | import { z } from 'zod'; |
2 | 2 |
|
| 3 | +import { type ActiveClusterSelectionPolicy } from '@/__generated__/proto-ts/uber/cadence/api/v1/ActiveClusterSelectionPolicy'; |
3 | 4 | import { ActiveClusterSelectionStrategy } from '@/__generated__/proto-ts/uber/cadence/api/v1/ActiveClusterSelectionStrategy'; |
4 | 5 | import { CancelExternalWorkflowExecutionFailedCause } from '@/__generated__/proto-ts/uber/cadence/api/v1/CancelExternalWorkflowExecutionFailedCause'; |
5 | 6 | import { ChildWorkflowExecutionFailedCause } from '@/__generated__/proto-ts/uber/cadence/api/v1/ChildWorkflowExecutionFailedCause'; |
@@ -146,39 +147,46 @@ const clusterAttributeSchema = z.object({ |
146 | 147 | name: z.string(), |
147 | 148 | }); |
148 | 149 |
|
149 | | -// TODO @adhitya.mamallan - this needs to be removed as part of active-active's redesign |
| 150 | +// TODO @adhitya.mamallan - this needs to be removed as part of active-active's redesign, once the IDL has removed them |
150 | 151 | const activeClusterSelectionStrategySchema = z.enum([ |
151 | 152 | ActiveClusterSelectionStrategy.ACTIVE_CLUSTER_SELECTION_STRATEGY_INVALID, |
152 | 153 | ActiveClusterSelectionStrategy.ACTIVE_CLUSTER_SELECTION_STRATEGY_REGION_STICKY, |
153 | 154 | ActiveClusterSelectionStrategy.ACTIVE_CLUSTER_SELECTION_STRATEGY_EXTERNAL_ENTITY, |
154 | 155 | ]); |
155 | 156 |
|
156 | | -// TODO @adhitya.mamallan - this needs to be modified as part of active-active's redesign, |
157 | | -// so that we only check for clusterAttributes |
158 | | -const activeClusterSelectionPolicySchema = z.discriminatedUnion( |
159 | | - 'strategyConfig', |
160 | | - [ |
161 | | - z.object({ |
162 | | - strategy: activeClusterSelectionStrategySchema, |
163 | | - strategyConfig: z.literal('activeClusterStickyRegionConfig'), |
164 | | - activeClusterStickyRegionConfig: z.object({ |
165 | | - stickyRegion: z.string(), |
166 | | - }), |
167 | | - activeClusterExternalEntityConfig: z.nullable(z.undefined()), |
168 | | - clusterAttribute: clusterAttributeSchema.nullable(), |
169 | | - }), |
170 | | - z.object({ |
171 | | - strategy: activeClusterSelectionStrategySchema, |
172 | | - strategyConfig: z.literal('activeClusterExternalEntityConfig'), |
173 | | - activeClusterStickyRegionConfig: z.nullable(z.undefined()), |
174 | | - activeClusterExternalEntityConfig: z.object({ |
175 | | - externalEntityType: z.string(), |
176 | | - externalEntityKey: z.string(), |
177 | | - }), |
178 | | - clusterAttribute: clusterAttributeSchema.nullable(), |
179 | | - }), |
180 | | - ] |
181 | | -); |
| 157 | +// The IDL still contains the strategy and strategyConfig fields, but they are absent |
| 158 | +// in the new active-active design. The preprocess step corrects this and allows us to |
| 159 | +// parse activeClusterSelectionPolicy even if the backend does not pass the deprecated fields. |
| 160 | +// |
| 161 | +// TODO @adhitya.mamallan - update this once the IDL has been updated |
| 162 | +const activeClusterSelectionPolicySchema = z.preprocess( |
| 163 | + (data) => { |
| 164 | + if (typeof data === 'object' && data !== null) { |
| 165 | + const dataWithDefaults = { |
| 166 | + ...data, |
| 167 | + strategy: |
| 168 | + 'strategy' in data && data.strategy |
| 169 | + ? data.strategy |
| 170 | + : 'ACTIVE_CLUSTER_SELECTION_STRATEGY_INVALID', |
| 171 | + strategyConfig: |
| 172 | + 'strategyConfig' in data && data.strategyConfig |
| 173 | + ? data.strategyConfig |
| 174 | + : 'activeClusterStickyRegionConfig', |
| 175 | + }; |
| 176 | + return dataWithDefaults; |
| 177 | + } |
| 178 | + return data; |
| 179 | + }, |
| 180 | + z.object({ |
| 181 | + clusterAttribute: clusterAttributeSchema, |
| 182 | + // TODO: remove the below fields once the IDL has removed them |
| 183 | + strategy: activeClusterSelectionStrategySchema, |
| 184 | + strategyConfig: z.enum([ |
| 185 | + 'activeClusterStickyRegionConfig', |
| 186 | + 'activeClusterExternalEntityConfig', |
| 187 | + ]), |
| 188 | + }) |
| 189 | +) as z.ZodType<ActiveClusterSelectionPolicy>; |
182 | 190 |
|
183 | 191 | const failureSchema = z.object({ |
184 | 192 | reason: z.string(), |
|
0 commit comments