Skip to content

Commit 75dda85

Browse files
committed
Fix view signature
1 parent 8411789 commit 75dda85

File tree

3 files changed

+46
-19
lines changed

3 files changed

+46
-19
lines changed

crates/bindings-typescript/src/lib/option.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { AlgebraicType } from './algebraic_type';
22

3-
export type OptionAlgebraicType = {
3+
export type OptionAlgebraicType<T extends AlgebraicType = AlgebraicType> = {
44
tag: 'Sum';
55
value: {
66
variants: [
7-
{ name: 'some'; algebraicType: AlgebraicType },
7+
{ name: 'some'; algebraicType: T },
88
{
99
name: 'none';
1010
algebraicType: { tag: 'Product'; value: { elements: [] } };
@@ -14,9 +14,13 @@ export type OptionAlgebraicType = {
1414
};
1515

1616
export const Option: {
17-
getAlgebraicType(innerType: AlgebraicType): OptionAlgebraicType;
17+
getAlgebraicType<T extends AlgebraicType = AlgebraicType>(
18+
innerType: T
19+
): OptionAlgebraicType<T>;
1820
} = {
19-
getAlgebraicType(innerType: AlgebraicType): OptionAlgebraicType {
21+
getAlgebraicType<T extends AlgebraicType = AlgebraicType>(
22+
innerType: T
23+
): OptionAlgebraicType<T> {
2024
return AlgebraicType.Sum({
2125
variants: [
2226
{ name: 'some', algebraicType: innerType },

crates/bindings-typescript/src/server/type_builders.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,18 +1101,21 @@ export class ArrayBuilder<Element extends TypeBuilder<any, any>>
11011101
export class OptionBuilder<Value extends TypeBuilder<any, any>>
11021102
extends TypeBuilder<
11031103
InferTypeOfTypeBuilder<Value> | undefined,
1104-
OptionAlgebraicType
1104+
OptionAlgebraicType<InferSpacetimeTypeOfTypeBuilder<Value>>
11051105
>
11061106
implements
1107-
Defaultable<InferTypeOfTypeBuilder<Value> | undefined, OptionAlgebraicType>
1107+
Defaultable<
1108+
InferTypeOfTypeBuilder<Value> | undefined,
1109+
OptionAlgebraicType<InferSpacetimeTypeOfTypeBuilder<Value>>
1110+
>
11081111
{
11091112
/**
11101113
* The phantom value type of the option for TypeScript
11111114
*/
11121115
readonly value!: Value;
11131116

11141117
constructor(value: Value) {
1115-
let innerType: AlgebraicType;
1118+
let innerType: InferSpacetimeTypeOfTypeBuilder<Value>;
11161119
if (value instanceof ColumnBuilder) {
11171120
innerType = value.typeBuilder.algebraicType;
11181121
} else {
@@ -2294,11 +2297,14 @@ export class OptionColumnBuilder<
22942297
>
22952298
extends ColumnBuilder<
22962299
InferTypeOfTypeBuilder<Value> | undefined,
2297-
OptionAlgebraicType,
2300+
OptionAlgebraicType<InferSpacetimeTypeOfTypeBuilder<Value>>,
22982301
M
22992302
>
23002303
implements
2301-
Defaultable<InferTypeOfTypeBuilder<Value> | undefined, OptionAlgebraicType>
2304+
Defaultable<
2305+
InferTypeOfTypeBuilder<Value> | undefined,
2306+
OptionAlgebraicType<InferSpacetimeTypeOfTypeBuilder<Value>>
2307+
>
23022308
{
23032309
default(
23042310
value: InferTypeOfTypeBuilder<Value> | undefined

crates/bindings-typescript/src/server/views.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import type {
1+
import {
22
AlgebraicType,
3-
AlgebraicTypeVariants,
4-
ProductType,
3+
type AlgebraicTypeVariants,
4+
type ProductType,
55
} from '../lib/algebraic_type';
66
import type { Identity } from '../lib/identity';
7+
import type { OptionAlgebraicType } from '../lib/option';
78
import type { ParamsObj } from './reducers';
89
import { MODULE_DEF, type UntypedSchemaDef } from './schema';
910
import type { ReadonlyTable } from './table';
@@ -35,11 +36,15 @@ export type AnonymousViewFn<
3536
Ret extends ViewReturnTypeBuilder,
3637
> = (ctx: AnonymousViewCtx<S>, params: InferTypeOfRow<Params>) => Infer<Ret>;
3738

38-
export type ViewReturnTypeBuilder = TypeBuilder<
39-
any,
40-
| { tag: 'Array'; value: AlgebraicTypeVariants.Product }
41-
| AlgebraicTypeVariants.Product
42-
>;
39+
export type ViewReturnTypeBuilder =
40+
| TypeBuilder<
41+
readonly object[],
42+
{ tag: 'Array'; value: AlgebraicTypeVariants.Product }
43+
>
44+
| TypeBuilder<
45+
object | undefined,
46+
OptionAlgebraicType<AlgebraicTypeVariants.Product>
47+
>;
4348

4449
export function defineView<
4550
S extends UntypedSchemaDef,
@@ -74,11 +79,23 @@ export function defineView<
7479
},
7580
});
7681

82+
let returnType = ret.algebraicType;
83+
if (returnType.tag == 'Sum') {
84+
const originalFn = fn;
85+
fn = ((ctx: ViewCtx<S>, args: InferTypeOfRow<Params>) => {
86+
const ret = originalFn(ctx, args);
87+
return ret == null ? [] : [ret];
88+
}) as any;
89+
returnType = AlgebraicType.Array(
90+
returnType.value.variants[0].algebraicType
91+
);
92+
}
93+
7794
(anon ? ANON_VIEWS : VIEWS).push({
7895
fn,
7996
params: paramType,
80-
returnType: ret.algebraicType,
81-
returnTypeBaseSize: bsatnBaseSize(MODULE_DEF.typespace, ret.algebraicType),
97+
returnType,
98+
returnTypeBaseSize: bsatnBaseSize(MODULE_DEF.typespace, returnType),
8299
});
83100
}
84101

0 commit comments

Comments
 (0)