Skip to content

Commit 5efcf6b

Browse files
authored
feat: rename refactorings
Implement renaming refactoring: const→con, 3-letter Type classes, and schema field updates
2 parents 5b92e12 + 94254ae commit 5efcf6b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+480
-482
lines changed

src/codegen/capacity/estimators.ts

Lines changed: 8 additions & 7 deletions
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,22 +60,22 @@ 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; // ConstType
63+
const constType = type as ConType;
6364
ctx.inc(maxEncodingCapacity(constType.value()));
6465
};
6566

6667
export const arr = (ctx: CapacityEstimatorCodegenContext, value: JsExpression, type: Type): void => {
6768
const codegen = ctx.codegen;
6869
ctx.inc(MaxEncodingOverhead.Array);
6970
const rLen = codegen.var(`${value.use()}.length`);
70-
const arrayType = type as any; // ArrayType
71+
const arrayType = type as any; // ArrType
7172
const elementType = arrayType.type;
7273
codegen.js(`size += ${MaxEncodingOverhead.ArrayElement} * ${rLen}`);
7374
const fn = elementType.compileCapacityEstimator({
7475
system: ctx.options.system,
7576
name: ctx.options.name,
7677
});
77-
const isConstantSizeType = ['const', 'bool', 'num'].includes(elementType.getTypeName());
78+
const isConstantSizeType = ['con', 'bool', 'num'].includes(elementType.getTypeName());
7879
if (isConstantSizeType) {
7980
const rFn = codegen.linkDependency(fn);
8081
codegen.js(`size += ${rLen} * ${rFn}(${elementType.random()});`);
@@ -90,7 +91,7 @@ export const arr = (ctx: CapacityEstimatorCodegenContext, value: JsExpression, t
9091
export const tup = (ctx: CapacityEstimatorCodegenContext, value: JsExpression, type: Type): void => {
9192
const codegen = ctx.codegen;
9293
const r = codegen.var(value.use());
93-
const tupleType = type as any; // TupleType
94+
const tupleType = type as any; // TupType
9495
const types = tupleType.types;
9596
const overhead = MaxEncodingOverhead.Array + MaxEncodingOverhead.ArrayElement * types.length;
9697
ctx.inc(overhead);
@@ -113,7 +114,7 @@ export const obj = (
113114
): void => {
114115
const codegen = ctx.codegen;
115116
const r = codegen.var(value.use());
116-
const objectType = type as any; // ObjectType
117+
const objectType = type as any; // ObjType
117118
const encodeUnknownFields = !!objectType.schema.encodeUnknownFields;
118119
if (encodeUnknownFields) {
119120
codegen.js(`size += maxEncodingCapacity(${r});`);
@@ -142,7 +143,7 @@ export const map = (ctx: CapacityEstimatorCodegenContext, value: JsExpression, t
142143
const rLen = codegen.var(`${rKeys}.length`);
143144
codegen.js(`size += ${MaxEncodingOverhead.ObjectElement} * ${rLen}`);
144145
const mapType = type as any; // MapType
145-
const valueType = mapType.type;
146+
const valueType = mapType.valueType;
146147
const fn = valueType.compileCapacityEstimator({
147148
system: ctx.options.system,
148149
name: ctx.options.name,
@@ -209,7 +210,7 @@ export const generate = (ctx: CapacityEstimatorCodegenContext, value: JsExpressi
209210
case 'bin':
210211
bin(ctx, value);
211212
break;
212-
case 'const':
213+
case 'con':
213214
const_(ctx, value, type);
214215
break;
215216
case 'arr':

src/codegen/validator/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export const canSkipObjectKeyUndefinedCheck = (type: string): boolean => {
22
switch (type) {
3-
case 'const':
3+
case 'con':
44
case 'bool':
55
case 'num':
66
case 'str':

src/json-schema/converter.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import type {AbstractType} from '../type/classes/AbstractType';
1+
import type {AbsType} from '../type/classes/AbsType';
22
import type {AnyType} from '../type/classes/AnyType';
3-
import type {ArrayType} from '../type/classes/ArrayType';
4-
import type {BinaryType} from '../type/classes/BinaryType';
5-
import type {BooleanType} from '../type/classes/BooleanType';
6-
import type {ConstType} from '../type/classes/ConstType';
3+
import type {ArrType} from '../type/classes/ArrType';
4+
import type {BinType} from '../type/classes/BinType';
5+
import type {BoolType} from '../type/classes/BoolType';
6+
import type {ConType} from '../type/classes/ConType';
77
import type {MapType} from '../type/classes/MapType';
8-
import type {NumberType} from '../type/classes/NumberType';
9-
import type {ObjectType} from '../type/classes/ObjectType';
8+
import type {NumType} from '../type/classes/NumType';
9+
import type {ObjType} from '../type/classes/ObjType';
1010
import type {OrType} from '../type/classes/OrType';
1111
import type {RefType} from '../type/classes/RefType';
12-
import type {StringType} from '../type/classes/StringType';
13-
import type {TupleType} from '../type/classes/TupleType';
12+
import type {StrType} from '../type/classes/StrType';
13+
import type {TupType} from '../type/classes/TupType';
1414
import type {TypeExportContext} from '../system/TypeExportContext';
1515
import type * as schema from '../schema';
1616
import type {
@@ -30,9 +30,9 @@ import type {
3030

3131
/**
3232
* Extracts the base JSON Schema properties that are common to all types.
33-
* This replaces the logic from AbstractType.toJsonSchema().
33+
* This replaces the logic from AbsType.toJsonSchema().
3434
*/
35-
function getBaseJsonSchema(type: AbstractType<any>, ctx?: TypeExportContext): JsonSchemaGenericKeywords {
35+
function getBaseJsonSchema(type: AbsType<any>, ctx?: TypeExportContext): JsonSchemaGenericKeywords {
3636
const typeSchema = type.getSchema();
3737
const jsonSchema: JsonSchemaGenericKeywords = {};
3838

@@ -49,34 +49,34 @@ function getBaseJsonSchema(type: AbstractType<any>, ctx?: TypeExportContext): Js
4949
* Main router function that converts a type to JSON Schema using a switch statement.
5050
* This replaces the individual toJsonSchema() methods on each type class.
5151
*/
52-
export function typeToJsonSchema(type: AbstractType<any>, ctx?: TypeExportContext): JsonSchemaNode {
52+
export function typeToJsonSchema(type: AbsType<any>, ctx?: TypeExportContext): JsonSchemaNode {
5353
const typeName = type.getTypeName();
5454

5555
switch (typeName) {
5656
case 'any':
5757
return anyToJsonSchema(type as AnyType, ctx);
5858
case 'arr':
59-
return arrayToJsonSchema(type as ArrayType<any>, ctx);
59+
return arrayToJsonSchema(type as ArrType<any>, ctx);
6060
case 'bin':
61-
return binaryToJsonSchema(type as BinaryType<any>, ctx);
61+
return binaryToJsonSchema(type as BinType<any>, ctx);
6262
case 'bool':
63-
return booleanToJsonSchema(type as BooleanType, ctx);
64-
case 'const':
65-
return constToJsonSchema(type as ConstType<any>, ctx);
63+
return booleanToJsonSchema(type as BoolType, ctx);
64+
case 'con':
65+
return constToJsonSchema(type as ConType<any>, ctx);
6666
case 'map':
6767
return mapToJsonSchema(type as MapType<any>, ctx);
6868
case 'num':
69-
return numberToJsonSchema(type as NumberType, ctx);
69+
return numberToJsonSchema(type as NumType, ctx);
7070
case 'obj':
71-
return objectToJsonSchema(type as ObjectType<any>, ctx);
71+
return objectToJsonSchema(type as ObjType<any>, ctx);
7272
case 'or':
7373
return orToJsonSchema(type as OrType<any>, ctx);
7474
case 'ref':
7575
return refToJsonSchema(type as RefType<any>, ctx);
7676
case 'str':
77-
return stringToJsonSchema(type as StringType, ctx);
77+
return stringToJsonSchema(type as StrType, ctx);
7878
case 'tup':
79-
return tupleToJsonSchema(type as TupleType<any>, ctx);
79+
return tupleToJsonSchema(type as TupType<any>, ctx);
8080
default:
8181
// Fallback to base implementation for unknown types
8282
return getBaseJsonSchema(type, ctx);
@@ -97,7 +97,7 @@ function anyToJsonSchema(type: AnyType, ctx?: TypeExportContext): JsonSchemaAny
9797
return result;
9898
}
9999

100-
function arrayToJsonSchema(type: ArrayType<any>, ctx?: TypeExportContext): JsonSchemaArray {
100+
function arrayToJsonSchema(type: ArrType<any>, ctx?: TypeExportContext): JsonSchemaArray {
101101
const schema = type.getSchema();
102102
const baseSchema = getBaseJsonSchema(type, ctx);
103103
const result: JsonSchemaArray = {
@@ -114,7 +114,7 @@ function arrayToJsonSchema(type: ArrayType<any>, ctx?: TypeExportContext): JsonS
114114
return result;
115115
}
116116

117-
function binaryToJsonSchema(type: BinaryType<any>, ctx?: TypeExportContext): JsonSchemaBinary {
117+
function binaryToJsonSchema(type: BinType<any>, ctx?: TypeExportContext): JsonSchemaBinary {
118118
const baseSchema = getBaseJsonSchema(type, ctx);
119119
const result: JsonSchemaBinary = {
120120
type: 'binary' as any,
@@ -126,7 +126,7 @@ function binaryToJsonSchema(type: BinaryType<any>, ctx?: TypeExportContext): Jso
126126
return result;
127127
}
128128

129-
function booleanToJsonSchema(type: BooleanType, ctx?: TypeExportContext): JsonSchemaBoolean {
129+
function booleanToJsonSchema(type: BoolType, ctx?: TypeExportContext): JsonSchemaBoolean {
130130
const baseSchema = getBaseJsonSchema(type, ctx);
131131
const result: JsonSchemaBoolean = {
132132
type: 'boolean',
@@ -138,7 +138,7 @@ function booleanToJsonSchema(type: BooleanType, ctx?: TypeExportContext): JsonSc
138138
return result;
139139
}
140140

141-
function constToJsonSchema(type: ConstType<any>, ctx?: TypeExportContext): JsonSchemaNode {
141+
function constToJsonSchema(type: ConType<any>, ctx?: TypeExportContext): JsonSchemaNode {
142142
const schema = type.getSchema();
143143
const baseSchema = getBaseJsonSchema(type, ctx);
144144
const value = schema.value;
@@ -204,7 +204,7 @@ function mapToJsonSchema(type: MapType<any>, ctx?: TypeExportContext): JsonSchem
204204
const result: JsonSchemaObject = {
205205
type: 'object',
206206
patternProperties: {
207-
'.*': typeToJsonSchema((type as any).type, ctx),
207+
'.*': typeToJsonSchema((type as any).valueType, ctx),
208208
},
209209
};
210210

@@ -214,7 +214,7 @@ function mapToJsonSchema(type: MapType<any>, ctx?: TypeExportContext): JsonSchem
214214
return result;
215215
}
216216

217-
function numberToJsonSchema(type: NumberType, ctx?: TypeExportContext): JsonSchemaNumber {
217+
function numberToJsonSchema(type: NumType, ctx?: TypeExportContext): JsonSchemaNumber {
218218
const schema = type.getSchema();
219219
const baseSchema = getBaseJsonSchema(type, ctx);
220220
const result: JsonSchemaNumber = {
@@ -238,7 +238,7 @@ function numberToJsonSchema(type: NumberType, ctx?: TypeExportContext): JsonSche
238238
return result;
239239
}
240240

241-
function objectToJsonSchema(type: ObjectType<any>, ctx?: TypeExportContext): JsonSchemaObject {
241+
function objectToJsonSchema(type: ObjType<any>, ctx?: TypeExportContext): JsonSchemaObject {
242242
const schema = type.getSchema();
243243
const baseSchema = getBaseJsonSchema(type, ctx);
244244
const result: JsonSchemaObject = {
@@ -294,7 +294,7 @@ function refToJsonSchema(type: RefType<any>, ctx?: TypeExportContext): JsonSchem
294294
return result;
295295
}
296296

297-
function stringToJsonSchema(type: StringType, ctx?: TypeExportContext): JsonSchemaString {
297+
function stringToJsonSchema(type: StrType, ctx?: TypeExportContext): JsonSchemaString {
298298
const schema = type.getSchema();
299299
const baseSchema = getBaseJsonSchema(type, ctx);
300300
const result: JsonSchemaString = {
@@ -323,7 +323,7 @@ function stringToJsonSchema(type: StringType, ctx?: TypeExportContext): JsonSche
323323
return result;
324324
}
325325

326-
function tupleToJsonSchema(type: TupleType<any>, ctx?: TypeExportContext): JsonSchemaArray {
326+
function tupleToJsonSchema(type: TupType<any>, ctx?: TypeExportContext): JsonSchemaArray {
327327
const baseSchema = getBaseJsonSchema(type, ctx);
328328
const types = (type as any).types;
329329
const result: JsonSchemaArray = {

src/jtd/converter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function toJtdForm(schema: schema.Schema): jtd.JtdForm {
2727
const form: jtd.JtdTypeForm = {type: 'boolean'};
2828
return form;
2929
}
30-
case 'const': {
30+
case 'con': {
3131
const constSchema = schema as schema.ConstSchema;
3232
const value = constSchema.value;
3333
const valueType = typeof value;
@@ -77,7 +77,7 @@ export function toJtdForm(schema: schema.Schema): jtd.JtdForm {
7777

7878
for (const field of objSchema.fields) {
7979
const fieldName = field.key;
80-
const fieldType = field.type;
80+
const fieldType = field.value;
8181

8282
if (fieldType) {
8383
const fieldJtd = toJtdForm(fieldType);
@@ -107,7 +107,7 @@ export function toJtdForm(schema: schema.Schema): jtd.JtdForm {
107107
case 'map': {
108108
const mapSchema = schema as schema.MapSchema;
109109
return {
110-
values: toJtdForm(mapSchema.type),
110+
values: toJtdForm(mapSchema.value),
111111
};
112112
}
113113
case 'ref': {

src/random/__tests__/random.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,10 @@ describe('random generators', () => {
240240

241241
test('handles edge cases and constraints', () => {
242242
// Empty array constraint
243-
const emptyArrayType = t.Array(t.String(), {max: 0});
244-
const emptyArray = emptyArrayType.random();
243+
const emptyArrType = t.Array(t.String(), {max: 0});
244+
const emptyArray = emptyArrType.random();
245245
expect(emptyArray).toEqual([]);
246-
emptyArrayType.validate(emptyArray);
246+
emptyArrType.validate(emptyArray);
247247

248248
// Single item array constraint
249249
const singleItemType = t.Array(t.Number(), {min: 1, max: 1});

src/random/generator.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
1-
import type {AbstractType} from '../type/classes/AbstractType';
1+
import type {AbsType} from '../type/classes/AbsType';
22
import type {AnyType} from '../type/classes/AnyType';
3-
import type {ArrayType} from '../type/classes/ArrayType';
4-
import type {BinaryType} from '../type/classes/BinaryType';
5-
import type {BooleanType} from '../type/classes/BooleanType';
6-
import type {ConstType} from '../type/classes/ConstType';
7-
import type {FunctionType} from '../type/classes/FunctionType';
3+
import type {ArrType} from '../type/classes/ArrType';
4+
import type {BinType} from '../type/classes/BinType';
5+
import type {BoolType} from '../type/classes/BoolType';
6+
import type {ConType} from '../type/classes/ConType';
7+
import type {FnType} from '../type/classes/FnType';
88
import type {MapType} from '../type/classes/MapType';
9-
import type {NumberType} from '../type/classes/NumberType';
10-
import type {ObjectType} from '../type/classes/ObjectType';
9+
import type {NumType} from '../type/classes/NumType';
10+
import type {ObjType} from '../type/classes/ObjType';
1111
import type {OrType} from '../type/classes/OrType';
1212
import type {RefType} from '../type/classes/RefType';
13-
import type {StringType} from '../type/classes/StringType';
14-
import type {TupleType} from '../type/classes/TupleType';
13+
import type {StrType} from '../type/classes/StrType';
14+
import type {TupType} from '../type/classes/TupType';
1515

1616
import * as gen from './generators';
1717

1818
/**
1919
* Main router function that dispatches to the correct random generator based on the type's kind.
2020
* This replaces the individual random() methods in each type class.
2121
*/
22-
export function random(type: AbstractType<any>): unknown {
22+
export function random(type: AbsType<any>): unknown {
2323
const kind = type.getTypeName();
2424

2525
switch (kind) {
2626
case 'any':
2727
return gen.any(type as AnyType);
2828
case 'arr':
29-
return gen.arr(type as ArrayType<any>);
29+
return gen.arr(type as ArrType<any>);
3030
case 'bin':
31-
return gen.bin(type as BinaryType<any>);
31+
return gen.bin(type as BinType<any>);
3232
case 'bool':
33-
return gen.bool(type as BooleanType);
34-
case 'const':
35-
return gen.const_(type as ConstType);
33+
return gen.bool(type as BoolType);
34+
case 'con':
35+
return gen.const_(type as ConType);
3636
case 'fn':
3737
case 'fn$':
38-
return gen.fn(type as FunctionType<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':
42-
return gen.num(type as NumberType);
42+
return gen.num(type as NumType);
4343
case 'obj':
44-
return gen.obj(type as ObjectType<any>);
44+
return gen.obj(type as ObjType<any>);
4545
case 'or':
4646
return gen.or(type as OrType<any>);
4747
case 'ref':
4848
return gen.ref(type as RefType<any>);
4949
case 'str':
50-
return gen.str(type as StringType);
50+
return gen.str(type as StrType);
5151
case 'tup':
52-
return gen.tup(type as TupleType<any>);
52+
return gen.tup(type as TupType<any>);
5353
default:
5454
// Fallback to generic random JSON for unknown types
5555
return gen.any(type as AnyType);

0 commit comments

Comments
 (0)