Skip to content

Commit

Permalink
feat(json-mapper): implement deserialize compilation method
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Sep 6, 2023
1 parent a626133 commit e457d70
Show file tree
Hide file tree
Showing 25 changed files with 1,731 additions and 1,323 deletions.
6 changes: 3 additions & 3 deletions packages/orm/mongoose/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ module.exports = {
},
coverageThreshold: {
global: {
statements: 99.06,
branches: 95.4,
statements: 98.95,
branches: 95,
functions: 99,
lines: 99.06
lines: 98.95
}
}
};
2 changes: 1 addition & 1 deletion packages/orm/mongoose/src/decorators/virtualRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function VirtualRef(options: string | MongooseVirtualRefOptions, foreignF

return useDecorators(
StoreMerge(MONGOOSE_SCHEMA, schema),
schema.count ? Property(Number) : type && (schema.justOne ? Property(type) : CollectionOf(type))
schema.count ? Property(Number) : type && (schema.justOne ? Property(type) : CollectionOf(type, Array))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ Object {
},
"properties": Object {
"galaxies": Object {
"$ref": "#/definitions/GalaxiesModel",
"items": Object {
"$ref": "#/definitions/GalaxiesModel",
},
"type": "array",
},
"galaxyCount": Object {
"multipleOf": 1,
Expand Down
4 changes: 2 additions & 2 deletions packages/orm/mongoose/test/virtualRef.integration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Controller, Get, Inject, PlatformTest} from "@tsed/common";
import {getValue} from "@tsed/core";
import {serialize} from "@tsed/json-mapper";
import {Model, MongooseModel, ObjectID, VirtualRef, VirtualRefs} from "@tsed/mongoose";
import {Model, MongooseModel, ObjectID, VirtualRef} from "@tsed/mongoose";
import {PlatformExpress} from "@tsed/platform-express";
import {CollectionOf, getJsonSchema, Groups, Integer, Required} from "@tsed/schema";
import {TestMongooseContext} from "@tsed/testing-mongoose";
Expand Down Expand Up @@ -36,7 +36,7 @@ export class SpacesModel {
justOne: false
})
@CollectionOf(GalaxiesModel)
galaxies?: VirtualRefs<GalaxiesModel>;
galaxies?: GalaxiesModel[];

@VirtualRef({
ref: GalaxiesModel,
Expand Down
6 changes: 3 additions & 3 deletions packages/specs/json-mapper/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ module.exports = {
roots: ["<rootDir>/src", "<rootDir>/test"],
coverageThreshold: {
global: {
statements: 99.57,
branches: 97.89,
statements: 99.66,
branches: 97.94,
functions: 100,
lines: 99.57
lines: 99.66
}
},
moduleNameMapper: {
Expand Down
38 changes: 0 additions & 38 deletions packages/specs/json-mapper/src/components/ArrayMapper.spec.ts

This file was deleted.

18 changes: 0 additions & 18 deletions packages/specs/json-mapper/src/components/ArrayMapper.ts

This file was deleted.

46 changes: 0 additions & 46 deletions packages/specs/json-mapper/src/components/MapMapper.spec.ts

This file was deleted.

29 changes: 0 additions & 29 deletions packages/specs/json-mapper/src/components/MapMapper.ts

This file was deleted.

49 changes: 27 additions & 22 deletions packages/specs/json-mapper/src/components/PrimitiveMapper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {nameOf} from "@tsed/core";
import {JsonMapper} from "../decorators/jsonMapper";
import {JsonMapperCtx, JsonMapperMethods} from "../interfaces/JsonMapperMethods";

Expand All @@ -21,37 +22,41 @@ export class CastError extends Error {
@JsonMapper(String, Number, Boolean, BigInt)
export class PrimitiveMapper implements JsonMapperMethods {
deserialize<T>(data: any, ctx: JsonMapperCtx): string | number | boolean | void | null | BigInt {
switch (ctx.type) {
case String:
return data === null ? null : "" + data;

case BigInt:
if (isNullish(data)) return null;
return (this as any)[nameOf(ctx.type)] ? (this as any)[nameOf(ctx.type)](data, ctx) : undefined;
}

return BigInt(data);
serialize(object: string | number | boolean | BigInt): string | number | boolean | BigInt {
return object;
}

case Number:
if (isNullish(data)) return null;
protected String(data: any) {
return data === null ? null : "" + data;
}

const n = +data;
protected Boolean(data: any) {
if (["true", "1", true].includes(data)) return true;
if (["false", "0", false].includes(data)) return false;
if (isNullish(data)) return null;
if (data === undefined) return undefined;

if (isNaN(n)) {
throw new CastError("Expression value is not a number.");
}
return !!data;
}

return n;
protected Number(data: any) {
if (isNullish(data)) return null;

case Boolean:
if (["true", "1", true].includes(data)) return true;
if (["false", "0", false].includes(data)) return false;
if (isNullish(data)) return null;
if (data === undefined) return undefined;
const n = +data;

return !!data;
if (isNaN(n)) {
throw new CastError("Expression value is not a number.");
}

return n;
}

serialize(object: string | number | boolean | BigInt): string | number | boolean | BigInt {
return object;
protected BigInt(data: any) {
if (isNullish(data)) return null;

return BigInt(data);
}
}
38 changes: 0 additions & 38 deletions packages/specs/json-mapper/src/components/SetMapper.spec.ts

This file was deleted.

29 changes: 0 additions & 29 deletions packages/specs/json-mapper/src/components/SetMapper.ts

This file was deleted.

Loading

0 comments on commit e457d70

Please sign in to comment.