Skip to content

Commit 94254ae

Browse files
Copilotstreamich
andcommitted
Fix test failures, rename FunType to FnType, and fix type casting
Co-authored-by: streamich <[email protected]>
1 parent 6cfc906 commit 94254ae

File tree

15 files changed

+45
-44
lines changed

15 files changed

+45
-44
lines changed

src/codegen/capacity/estimators.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
CompiledCapacityEstimator,
77
} from './CapacityEstimatorCodegenContext';
88
import type {Type} from '../../type';
9+
import type {ConType} from '../../type/classes/ConType';
910

1011
type EstimatorFunction = (ctx: CapacityEstimatorCodegenContext, value: JsExpression, type: Type) => void;
1112

@@ -59,7 +60,7 @@ export const bin = (ctx: CapacityEstimatorCodegenContext, value: JsExpression):
5960
};
6061

6162
export const const_ = (ctx: CapacityEstimatorCodegenContext, value: JsExpression, type: Type): void => {
62-
const constType = type as any; // ConType
63+
const constType = type as ConType;
6364
ctx.inc(maxEncodingCapacity(constType.value()));
6465
};
6566

src/random/generator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type {ArrType} from '../type/classes/ArrType';
44
import type {BinType} from '../type/classes/BinType';
55
import type {BoolType} from '../type/classes/BoolType';
66
import type {ConType} from '../type/classes/ConType';
7-
import type {FunType} from '../type/classes/FunType';
7+
import type {FnType} from '../type/classes/FnType';
88
import type {MapType} from '../type/classes/MapType';
99
import type {NumType} from '../type/classes/NumType';
1010
import type {ObjType} from '../type/classes/ObjType';
@@ -35,7 +35,7 @@ export function random(type: AbsType<any>): unknown {
3535
return gen.const_(type as ConType);
3636
case 'fn':
3737
case 'fn$':
38-
return gen.fn(type as FunType<any, any>);
38+
return gen.fn(type as FnType<any, any>);
3939
case 'map':
4040
return gen.map(type as MapType<any>);
4141
case 'num':

src/random/generators.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {ArrType} from '../type/classes/ArrType';
55
import type {BinType} from '../type/classes/BinType';
66
import type {BoolType} from '../type/classes/BoolType';
77
import type {ConType} from '../type/classes/ConType';
8-
import type {FunType} from '../type/classes/FunType';
8+
import type {FnType} from '../type/classes/FnType';
99
import type {MapType} from '../type/classes/MapType';
1010
import type {NumType} from '../type/classes/NumType';
1111
import type {ObjType} from '../type/classes/ObjType';
@@ -46,7 +46,7 @@ export const const_ = (type: ConType): unknown => {
4646
return cloneBinary(type.getSchema().value);
4747
};
4848

49-
export const fn = (type: FunType<any, any>): unknown => {
49+
export const fn = (type: FnType<any, any>): unknown => {
5050
return async () => type.res.random();
5151
};
5252

src/schema/SchemaBuilder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,9 @@ export class SchemaBuilder {
232232
}
233233

234234
public Map<V extends Schema, K extends Schema = StringSchema>(
235-
value: V,
235+
value: V,
236236
key?: K,
237-
options?: Omit<NoT<MapSchema<V, K>>, 'value' | 'key'>
237+
options?: Omit<NoT<MapSchema<V, K>>, 'value' | 'key'>,
238238
): MapSchema<V, K> {
239239
return {kind: 'map', value, ...(key && {key}), ...options};
240240
}

src/schema/__tests__/validate.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ describe('validateSchema', () => {
340340
expect(() =>
341341
validateSchema({
342342
kind: 'bin',
343-
type: {kind: 'str'},
343+
value: {kind: 'str'},
344344
format: 'invalid',
345345
} as any),
346346
).toThrow('FORMAT');
@@ -396,7 +396,7 @@ describe('validateSchema', () => {
396396
{
397397
kind: 'field',
398398
key: 'name',
399-
type: {kind: 'str'},
399+
value: {kind: 'str'},
400400
},
401401
],
402402
};
@@ -428,7 +428,7 @@ describe('validateSchema', () => {
428428
const schema: Schema = {
429429
kind: 'field',
430430
key: 'test',
431-
type: {kind: 'str'},
431+
value: {kind: 'str'},
432432
};
433433
expect(() => validateSchema(schema)).not.toThrow();
434434
});
@@ -437,7 +437,7 @@ describe('validateSchema', () => {
437437
const schema: Schema = {
438438
kind: 'field',
439439
key: 'test',
440-
type: {kind: 'str'},
440+
value: {kind: 'str'},
441441
optional: true,
442442
};
443443
expect(() => validateSchema(schema)).not.toThrow();
@@ -448,7 +448,7 @@ describe('validateSchema', () => {
448448
validateSchema({
449449
kind: 'field',
450450
key: 123,
451-
type: {kind: 'str'},
451+
value: {kind: 'str'},
452452
} as any),
453453
).toThrow('KEY_TYPE');
454454
});
@@ -458,7 +458,7 @@ describe('validateSchema', () => {
458458
validateSchema({
459459
kind: 'field',
460460
key: 'test',
461-
type: {kind: 'str'},
461+
value: {kind: 'str'},
462462
optional: 'true',
463463
} as any),
464464
).toThrow('OPTIONAL_TYPE');
@@ -469,7 +469,7 @@ describe('validateSchema', () => {
469469
test('validates valid map schema', () => {
470470
const schema: Schema = {
471471
kind: 'map',
472-
type: {kind: 'str'},
472+
value: {kind: 'str'},
473473
};
474474
expect(() => validateSchema(schema)).not.toThrow();
475475
});

src/schema/schema.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,9 @@ export interface ObjectOptionalFieldSchema<K extends string = string, V extends
374374
* Represents an object, which is treated as a map. All keys are strings and all
375375
* values are of the same type.
376376
*/
377-
export interface MapSchema<V extends TType = any, K extends TType = any> extends TType<Record<string, unknown>>, WithValidator {
377+
export interface MapSchema<V extends TType = any, K extends TType = any>
378+
extends TType<Record<string, unknown>>,
379+
WithValidator {
378380
kind: 'map';
379381
/**
380382
* Type of all keys in the map. Defaults to string type.

src/type/TypeBuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ export class TypeBuilder {
251251
res: Res,
252252
options?: schema.Optional<schema.FunctionSchema>,
253253
) {
254-
const fn = new classes.FunType<Req, Res>(req, res, options);
254+
const fn = new classes.FnType<Req, Res>(req, res, options);
255255
fn.system = this.system;
256256
return fn;
257257
}

src/type/__tests__/toTypeScriptAst.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ describe('fn', () => {
277277
const type = system.t.Function(t.str, t.num);
278278
expect(type.toTypeScriptAst()).toMatchInlineSnapshot(`
279279
{
280-
"node": "FunType",
280+
"node": "FnType",
281281
"parameters": [
282282
{
283283
"name": {
@@ -314,7 +314,7 @@ describe('fn$', () => {
314314
const type = system.t.Function$(t.str, t.num);
315315
expect(type.toTypeScriptAst()).toMatchInlineSnapshot(`
316316
{
317-
"node": "FunType",
317+
"node": "FnType",
318318
"parameters": [
319319
{
320320
"name": {

src/type/classes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {ObjType, ObjectFieldType, ObjectOptionalFieldType} from './classes/ObjTy
1111
import {MapType} from './classes/MapType';
1212
import {RefType} from './classes/RefType';
1313
import {OrType} from './classes/OrType';
14-
import {FunType, FunctionStreamingType} from './classes/FunType';
14+
import {FnType, FunctionStreamingType} from './classes/FnType';
1515

1616
export {
1717
AbsType,
@@ -29,6 +29,6 @@ export {
2929
MapType,
3030
RefType,
3131
OrType,
32-
FunType,
32+
FnType,
3333
FunctionStreamingType,
3434
};

src/type/classes/FunType.ts renamed to src/type/classes/FnType.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const fnNotImplemented: schema.FunctionValue<any, any> = async () => {
1010
throw new Error('NOT_IMPLEMENTED');
1111
};
1212

13-
const toStringTree = (tab: string = '', type: FunType<Type, Type> | FunctionStreamingType<Type, Type>) => {
13+
const toStringTree = (tab: string = '', type: FnType<Type, Type> | FunctionStreamingType<Type, Type>) => {
1414
return printTree(tab, [
1515
(tab) => 'req: ' + type.req.toString(tab + ' '),
1616
(tab) => 'res: ' + type.res.toString(tab + ' '),
@@ -22,7 +22,7 @@ type FunctionImpl<Req extends Type, Res extends Type, Ctx = unknown> = (
2222
ctx: Ctx,
2323
) => Promise<ResolveType<Res>>;
2424

25-
export class FunType<Req extends Type, Res extends Type> extends AbsType<
25+
export class FnType<Req extends Type, Res extends Type> extends AbsType<
2626
schema.FunctionSchema<SchemaOf<Req>, SchemaOf<Res>>
2727
> {
2828
protected schema: schema.FunctionSchema<SchemaOf<Req>, SchemaOf<Res>>;
@@ -41,21 +41,21 @@ export class FunType<Req extends Type, Res extends Type> extends AbsType<
4141
} as any;
4242
}
4343

44-
public request<T extends Type>(req: T): FunType<T, Res> {
44+
public request<T extends Type>(req: T): FnType<T, Res> {
4545
(this as any).req = req;
4646
return this as any;
4747
}
4848

49-
public inp<T extends Type>(req: T): FunType<T, Res> {
49+
public inp<T extends Type>(req: T): FnType<T, Res> {
5050
return this.request(req);
5151
}
5252

53-
public response<T extends Type>(res: T): FunType<Req, T> {
53+
public response<T extends Type>(res: T): FnType<Req, T> {
5454
(this as any).res = res;
5555
return this as any;
5656
}
5757

58-
public out<T extends Type>(res: T): FunType<Req, T> {
58+
public out<T extends Type>(res: T): FnType<Req, T> {
5959
return this.response(res);
6060
}
6161

@@ -102,21 +102,21 @@ export class FunctionStreamingType<Req extends Type, Res extends Type> extends A
102102
} as any;
103103
}
104104

105-
public request<T extends Type>(req: T): FunType<T, Res> {
105+
public request<T extends Type>(req: T): FnType<T, Res> {
106106
(this as any).req = req;
107107
return this as any;
108108
}
109109

110-
public inp<T extends Type>(req: T): FunType<T, Res> {
110+
public inp<T extends Type>(req: T): FnType<T, Res> {
111111
return this.request(req);
112112
}
113113

114-
public response<T extends Type>(res: T): FunType<Req, T> {
114+
public response<T extends Type>(res: T): FnType<Req, T> {
115115
(this as any).res = res;
116116
return this as any;
117117
}
118118

119-
public out<T extends Type>(res: T): FunType<Req, T> {
119+
public out<T extends Type>(res: T): FnType<Req, T> {
120120
return this.response(res);
121121
}
122122

0 commit comments

Comments
 (0)