Skip to content

Commit

Permalink
Initial implementation of IfThenElseCondition.
Browse files Browse the repository at this point in the history
  • Loading branch information
derekpierre committed Sep 26, 2024
1 parent 3bd8816 commit 5e65311
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/taco/src/conditions/compound-condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { contractConditionSchema } from './base/contract';
import { rpcConditionSchema } from './base/rpc';
import { timeConditionSchema } from './base/time';
import { baseConditionSchema, Condition, ConditionProps } from './condition';
import { ifThenElseConditionSchema } from './if-then-else-condition';
import { maxNestedDepth } from './multi-condition';
import { sequentialConditionSchema } from './sequential';
import { OmitConditionType } from './shared';
Expand All @@ -25,6 +26,7 @@ export const compoundConditionSchema: z.ZodSchema = baseConditionSchema
contractConditionSchema,
compoundConditionSchema,
sequentialConditionSchema,
ifThenElseConditionSchema,
]),
),
)
Expand Down
71 changes: 71 additions & 0 deletions packages/taco/src/conditions/if-then-else-condition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { z } from 'zod';

import { contractConditionSchema } from './base/contract';
import { rpcConditionSchema } from './base/rpc';
import { timeConditionSchema } from './base/time';
import { compoundConditionSchema } from './compound-condition';
import { baseConditionSchema, Condition } from './condition';
import { maxNestedDepth } from './multi-condition';
import { sequentialConditionSchema } from './sequential';
import { OmitConditionType } from './shared';

export const IfThenElseConditionType = 'if-then-else';

export const ifThenElseConditionSchema: z.ZodSchema = baseConditionSchema
.extend({
conditionType: z
.literal(IfThenElseConditionType)
.default(IfThenElseConditionType),
ifCondition: z.lazy(() =>
z.union([
rpcConditionSchema,
timeConditionSchema,
contractConditionSchema,
compoundConditionSchema,
sequentialConditionSchema,
ifThenElseConditionSchema,
]),
),
thenCondition: z.lazy(() =>
z.union([
rpcConditionSchema,
timeConditionSchema,
contractConditionSchema,
compoundConditionSchema,
sequentialConditionSchema,
ifThenElseConditionSchema,
]),
),
elseCondition: z.lazy(() =>
z.union([
// Condition
rpcConditionSchema,
timeConditionSchema,
contractConditionSchema,
compoundConditionSchema,
sequentialConditionSchema,
ifThenElseConditionSchema,
// boolean
z.boolean(),
]),
),
})
.refine(
(condition) => maxNestedDepth(2)(condition),
{
message: 'Exceeded max nested depth of 2 for multi-condition type',
}, // Max nested depth of 2
);

export type IfThenElseConditionProps = z.infer<
typeof ifThenElseConditionSchema
>;

export class IfThenElseCondition extends Condition {
constructor(value: OmitConditionType<IfThenElseConditionProps>) {
super(sequentialConditionSchema, {
conditionType: IfThenElseConditionType,
...value,
});
}
}
19 changes: 16 additions & 3 deletions packages/taco/src/conditions/multi-condition.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { boolean } from 'zod';

import { CompoundConditionType } from './compound-condition';
import { ConditionProps } from './condition';
import { IfThenElseConditionType } from './if-then-else-condition';
import { ConditionVariableProps, SequentialConditionType } from './sequential';

export const maxNestedDepth =
(maxDepth: number) =>
(condition: ConditionProps, currentDepth = 1) => {
(condition: ConditionProps, currentDepth = 1): boolean => {
if (
condition.conditionType === CompoundConditionType ||
condition.conditionType === SequentialConditionType
condition.conditionType === SequentialConditionType ||
condition.conditionType === IfThenElseConditionType
) {
if (currentDepth > maxDepth) {
// no more multi-condition types allowed at this level
Expand All @@ -18,11 +22,20 @@ export const maxNestedDepth =
return condition.operands.every((child: ConditionProps) =>
maxNestedDepth(maxDepth)(child, currentDepth + 1),
);
} else {
} else if (condition.conditionType == SequentialConditionType) {
return condition.conditionVariables.every(
(child: ConditionVariableProps) =>
maxNestedDepth(maxDepth)(child.condition, currentDepth + 1),
);
} else {
// do something
const ifThenElseConditions = [];
ifThenElseConditions.push(condition.ifCondition);
ifThenElseConditions.push(condition.thenCondition);
if (!(condition.elseCondition instanceof boolean)) {
ifThenElseConditions.push(condition.elseCondition);
}
return maxNestedDepth(maxDepth)(ifThenElseConditions, currentDepth + 1);
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/taco/src/conditions/sequential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { rpcConditionSchema } from './base/rpc';
import { timeConditionSchema } from './base/time';
import { compoundConditionSchema } from './compound-condition';
import { baseConditionSchema, Condition } from './condition';
import { ifThenElseConditionSchema } from './if-then-else-condition';
import { maxNestedDepth } from './multi-condition';
import { OmitConditionType, plainStringSchema } from './shared';

Expand All @@ -19,6 +20,7 @@ export const conditionVariableSchema: z.ZodSchema = z.object({
contractConditionSchema,
compoundConditionSchema,
sequentialConditionSchema,
ifThenElseConditionSchema,
]),
),
});
Expand Down

0 comments on commit 5e65311

Please sign in to comment.