Skip to content

Commit 1a80d21

Browse files
authored
Merge pull request #344 from livingspec/RDBC-661
Refactor createEmptyObject as a protected method to allow customization
2 parents b694564 + 2c2edde commit 1a80d21

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

src/Mapping/ObjectMapper.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ObjectTypeDescriptor, ClassConstructor, ObjectLiteralDescriptor, EntityConstructor } from "../Types";
1+
import { ObjectTypeDescriptor, ObjectLiteralDescriptor, EntityConstructor } from "../Types";
22
import { throwError } from "../Exceptions";
33
import { TypeUtil } from "../Utility/TypeUtil";
44
import { getLogger } from "../Utility/LogUtil";
@@ -338,8 +338,7 @@ export class TypesAwareObjectMapper implements ITypesAwareObjectMapper {
338338
if (!ctorOrTypeDescriptor) {
339339
instance = Object.assign({}, rawValue);
340340
} else if (TypeUtil.isClass(ctorOrTypeDescriptor)) {
341-
instance = this._createEmptyObject(ctorOrTypeDescriptor as ClassConstructor);
342-
instance = Object.assign(instance, rawValue);
341+
instance = this.createEmptyObject(ctorOrTypeDescriptor, rawValue);
343342
} else if (TypeUtil.isObjectLiteralTypeDescriptor(ctorOrTypeDescriptor)) {
344343
instance = (ctorOrTypeDescriptor as ObjectLiteralDescriptor).construct(rawValue);
345344
} else {
@@ -367,12 +366,12 @@ export class TypesAwareObjectMapper implements ITypesAwareObjectMapper {
367366
return ctorOrTypeDescriptor;
368367
}
369368

370-
private _createEmptyObject<TResult extends object>(ctor: EntityConstructor<TResult>) {
369+
protected createEmptyObject<TResult extends object>(ctor: EntityConstructor<TResult>, rawValue: object) {
371370
if (!ctor) {
372371
throwError("InvalidArgumentException", "ctor argument must not be null or undefined.");
373372
}
374373

375-
return new ctor() as TResult;
374+
return Object.assign(new ctor(), rawValue) as TResult;
376375
}
377376

378377
private _makeObjectLiteral(

src/Utility/TypeUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class TypeUtil {
6363
return value === true || value === false;
6464
}
6565

66-
public static isClass(value: any): boolean {
66+
public static isClass(value: any): value is ClassConstructor {
6767
return this.isFunction(value) && ("name" in value) && value.name !== ""
6868
&& ("Object" !== value.name)
6969
&& (!!value.prototype && !!value.prototype.constructor.name);

0 commit comments

Comments
 (0)