Skip to content

Commit 67aba85

Browse files
author
Umed Khudoiberdiev
committed
added targetMap functionality
1 parent 09d09ee commit 67aba85

File tree

4 files changed

+1713
-1
lines changed

4 files changed

+1713
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "class-transformer",
3-
"version": "0.1.0-beta.9",
3+
"version": "0.1.0-beta.10",
44
"description": "Proper decorator-based transformation / serialization / deserialization of plain javascript objects to class constructors",
55
"license": "MIT",
66
"readmeFilename": "README.md",

src/ClassTransformOptions.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/**
2+
* Allows to specify a map of Types in the object without using @Type decorator.
3+
* This is useful when you have external classes.
4+
*/
5+
export interface TargetMap {
6+
7+
/**
8+
* Target which Types are being specified.
9+
*/
10+
target: Function;
11+
12+
/**
13+
* List of properties and their Types.
14+
*/
15+
properties: { [key: string]: Function };
16+
}
17+
118
/**
219
* Options to be passed during transformation.
320
*/
@@ -32,4 +49,11 @@ export interface ClassTransformOptions {
3249
*/
3350
ignoreDecorators?: boolean;
3451

52+
/**
53+
* Target maps allows to set a Types of the transforming object without using @Type decorator.
54+
* This is useful when you are transforming external classes, or if you already have type metadata for
55+
* objects and you don't want to set it up again.
56+
*/
57+
targetMaps?: TargetMap[];
58+
3559
}

src/TransformOperationExecutor.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ export class TransformOperationExecutor {
129129
const options: TypeOptions = { newObject: newValue, object: value, property: propertyName };
130130
type = metadata.typeFunction(options);
131131
isSubValueMap = isSubValueMap || metadata.reflectedType === Map;
132+
} else if (this.options.targetMaps) { // try to find a type in target maps
133+
this.options.targetMaps
134+
.filter(map => map.target === targetType && !!map.properties[propertyName])
135+
.forEach(map => type = map.properties[propertyName]);
132136
}
133137
}
134138

0 commit comments

Comments
 (0)