From 9a017e76068e269e3634415cbe21dea237d93847 Mon Sep 17 00:00:00 2001 From: zhl Date: Fri, 23 Apr 2021 18:04:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9noJson=E7=9A=84=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/decorators/nojson.ts | 24 +++++++++++++++++++----- src/models/Base.ts | 2 +- src/models/content/Puzzle.ts | 1 - 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/decorators/nojson.ts b/src/decorators/nojson.ts index 8d49fb1..e4c5518 100644 --- a/src/decorators/nojson.ts +++ b/src/decorators/nojson.ts @@ -1,18 +1,32 @@ import 'reflect-metadata' +import { singleton } from './singleton' const noJsonMetadataKey = ('noJsonSet') +@singleton +export class NoJsonClass { + private noJsonPropSet: Set = new Set() + + public addKey(className: string, propertyKey: string) { + this.noJsonPropSet.add(className+'_'+propertyKey) + } + + public checkExist(className: string, propertyKey: string) { + return this.noJsonPropSet.has(className+'_'+propertyKey) + } +} /** - * 这方法无效 * 在不需要toJson方法输出的字段上加上 @noJson * @return {{(target: Function): void, (target: Object, propertyKey: (string | symbol)): void}} */ export function noJson() { - return Reflect.metadata(noJsonMetadataKey, !0) + // return Reflect.metadata(noJsonMetadataKey, !0) + return function(target: Object, propertyKey: string) { + console.log(target) + new NoJsonClass().addKey(target.constructor.name, propertyKey) + } } export function checkJson(target: any, propertyKey: string) { - let val = Reflect.getMetadata(noJsonMetadataKey, target, propertyKey) - let s = Reflect.getMetadata('typegoose:properties', target, propertyKey) - return !val + return !new NoJsonClass().checkExist(target.constructor.modelName, propertyKey) } diff --git a/src/models/Base.ts b/src/models/Base.ts index e57640b..58a11a0 100644 --- a/src/models/Base.ts +++ b/src/models/Base.ts @@ -28,7 +28,7 @@ export abstract class BaseModule extends FindOrCreate { let result: any = {} // @ts-ignore for (let key in this._doc) { - if (jsonExcludeKeys.indexOf(key) == -1) { + if (checkJson(this, key+'' ) && jsonExcludeKeys.indexOf(key) == -1) { result[key] = this[key] } } diff --git a/src/models/content/Puzzle.ts b/src/models/content/Puzzle.ts index 6d21ab4..14eb80e 100644 --- a/src/models/content/Puzzle.ts +++ b/src/models/content/Puzzle.ts @@ -13,7 +13,6 @@ import { noJson } from '../../decorators/nojson' class PuzzleClass extends BaseModule { @prop() public question: string - @prop() public a1: string @prop()