Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 29 additions & 19 deletions sdks/typescript/packages/sdk/src/schedule_at.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,47 @@ import type { AlgebraicValue } from './algebraic_value';
export namespace ScheduleAt {
export function getAlgebraicType(): AlgebraicType {
return AlgebraicType.createSumType([
new SumTypeVariant('Interval', AlgebraicType.createU64Type()),
new SumTypeVariant('Time', AlgebraicType.createU64Type()),
new SumTypeVariant('Interval', AlgebraicType.createTimeDurationType()),
new SumTypeVariant('Time', AlgebraicType.createTimestampType()),
]);
}

export function serialize(value: ScheduleAt): object {
switch (value.tag) {
case 'Interval':
return { Interval: value.value };
case 'Time':
return { Time: value.value };
default:
throw 'unreachable';
}
}

export type Interval = { tag: 'Interval'; value: BigInt };
export type Interval = {
tag: 'Interval';
value: { __time_duration_micros__: BigInt };
};
export const Interval = (value: BigInt): Interval => ({
tag: 'Interval',
value,
value: { __time_duration_micros__: value },
});
export type Time = {
tag: 'Time';
value: { __timestamp_micros_since_unix_epoch__: BigInt };
};
export const Time = (value: BigInt): Time => ({
tag: 'Time',
value: { __timestamp_micros_since_unix_epoch__: value },
});
export type Time = { tag: 'Time'; value: BigInt };
export const Time = (value: BigInt): Time => ({ tag: 'Time', value });

export function fromValue(value: AlgebraicValue): ScheduleAt {
let sumValue = value.asSumValue();
switch (sumValue.tag) {
case 0:
return { tag: 'Interval', value: sumValue.value.asBigInt() };
return {
tag: 'Interval',
value: {
__time_duration_micros__: sumValue.value
.asProductValue()
.elements[0].asBigInt(),
},
};
case 1:
return { tag: 'Time', value: sumValue.value.asBigInt() };
return {
tag: 'Time',
value: {
__timestamp_micros_since_unix_epoch__: sumValue.value.asBigInt(),
},
};
default:
throw 'unreachable';
}
Expand Down
Loading