-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(classes): add logic to handle getter only properties
- Loading branch information
Showing
5 changed files
with
100 additions
and
27 deletions.
There are no files selected for viewing
107 changes: 85 additions & 22 deletions
107
packages/classes/src/lib/decorators/automap.decorator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,99 @@ | ||
import 'reflect-metadata'; | ||
import { AUTOMAP_PROPERTIES_METADATA_KEY } from '../constants'; | ||
import type { Constructible } from '../types'; | ||
|
||
export interface AutoMapOptions { | ||
/** | ||
* Type Function | ||
*/ | ||
typeFn?: () => Constructible; | ||
/** | ||
* Depth for nested models | ||
*/ | ||
depth?: number; | ||
/** | ||
* Is this property getter-only? | ||
*/ | ||
isGetterOnly?: boolean; | ||
} | ||
|
||
/** | ||
* AutoMap decorator to decorate fields in classes to store metadata of that field | ||
* deprecated - AutoMap decorator to decorate fields in classes to store metadata of that field. | ||
* | ||
* @param {() => Constructible} [typeFn] - if this is a nested model, it should have a `typeFn` | ||
* @param {number} [depth = 0] - how deep should this model gets instantiated | ||
* | ||
* @deprecated | ||
* @see Use {@link AutoMapOptions} instead | ||
*/ | ||
export const AutoMap = ( | ||
typeFn?: () => Constructible, | ||
export function AutoMap( | ||
typeFn: () => Constructible, | ||
depth?: number | ||
): PropertyDecorator; | ||
/** | ||
* AutoMap decorator to decorate fields in classes to store metadata of that field | ||
* | ||
* @param {AutoMapOptions} config | ||
*/ | ||
export function AutoMap(config: AutoMapOptions): PropertyDecorator; | ||
export function AutoMap(): PropertyDecorator; | ||
export function AutoMap( | ||
typeFnOrConfig?: AutoMapOptions | (() => Constructible), | ||
depth = 0 | ||
): PropertyDecorator => (target, propertyKey) => { | ||
// get existing metadata | ||
const existingMetadataList = | ||
Reflect.getMetadata(AUTOMAP_PROPERTIES_METADATA_KEY, target.constructor) || | ||
[]; | ||
|
||
// If `typeFn` is provided, classes plugin does not have to guess with Reflect and just `defineMetadata` | ||
if (typeFn) { | ||
Reflect.defineMetadata( | ||
AUTOMAP_PROPERTIES_METADATA_KEY, | ||
[...existingMetadataList, [propertyKey, { typeFn, depth }]], | ||
target.constructor | ||
); | ||
} else { | ||
const meta = Reflect.getMetadata('design:type', target, propertyKey); | ||
if (meta) { | ||
Reflect.defineMetadata( | ||
): PropertyDecorator { | ||
const { isGetterOnly, typeFn, depth: _depth = 0 } = getConfig( | ||
typeFnOrConfig, | ||
depth | ||
); | ||
return (target, propertyKey) => { | ||
const newMetadata = { depth: _depth }; | ||
|
||
const existingMetadataList = | ||
Reflect.getMetadata( | ||
AUTOMAP_PROPERTIES_METADATA_KEY, | ||
[...existingMetadataList, [propertyKey, { typeFn: () => meta }]], | ||
target.constructor | ||
) || []; | ||
|
||
// Getting Type | ||
if (typeFn) { | ||
newMetadata['typeFn'] = typeFn; | ||
} else { | ||
const meta = Reflect.getMetadata('design:type', target, propertyKey); | ||
if (meta) { | ||
newMetadata['typeFn'] = () => meta; | ||
} | ||
} | ||
|
||
// Getting Only-getter | ||
if (isGetterOnly != null) { | ||
newMetadata['isGetterOnly'] = isGetterOnly; | ||
} else { | ||
// paramtypes gives information about the setter. | ||
// it will be null if this is not a getter | ||
// it will be an [] if this is an getter-only | ||
const paramsType = Reflect.getMetadata( | ||
'design:paramtypes', | ||
target, | ||
propertyKey | ||
); | ||
newMetadata['isGetterOnly'] = paramsType && !(paramsType as []).length; | ||
} | ||
|
||
Reflect.defineMetadata( | ||
AUTOMAP_PROPERTIES_METADATA_KEY, | ||
[...existingMetadataList, [propertyKey, newMetadata]], | ||
target.constructor | ||
); | ||
}; | ||
} | ||
|
||
function getConfig( | ||
typeFnOrConfig?: AutoMapOptions | (() => Constructible), | ||
depth?: number | ||
): AutoMapOptions { | ||
if (typeof typeFnOrConfig === 'function') { | ||
return { typeFn: typeFnOrConfig, depth: depth || 0 }; | ||
} | ||
}; | ||
|
||
return typeFnOrConfig || {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,4 +34,5 @@ export const enum MapFnClassId { | |
export const enum MetadataClassId { | ||
propertyKey, | ||
metadataFn, | ||
isGetterOnly, | ||
} |