修改noJson的实现逻辑
This commit is contained in:
parent
6c274bb7c6
commit
9a017e7606
@ -1,18 +1,32 @@
|
||||
import 'reflect-metadata'
|
||||
import { singleton } from './singleton'
|
||||
|
||||
const noJsonMetadataKey = ('noJsonSet')
|
||||
|
||||
@singleton
|
||||
export class NoJsonClass {
|
||||
private noJsonPropSet: Set<string> = 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)
|
||||
}
|
||||
|
@ -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]
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,6 @@ import { noJson } from '../../decorators/nojson'
|
||||
class PuzzleClass extends BaseModule {
|
||||
@prop()
|
||||
public question: string
|
||||
|
||||
@prop()
|
||||
public a1: string
|
||||
@prop()
|
||||
|
Loading…
x
Reference in New Issue
Block a user