Skip to content

Commit

Permalink
fix(core): treat null metadata/Date same as null value aka just map t…
Browse files Browse the repository at this point in the history
…he value. add logic for isNullMetadata to MappingPropert

y
  • Loading branch information
nartc committed Feb 19, 2021
1 parent 8e17527 commit 97f260d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function createMapForMember<
// initialize MappingProperty
const mappingProperty: MappingProperty = [
[memberPath, sourcePath],
[mapMemberFn, preCond as PreConditionReturn],
[mapMemberFn, false, preCond as PreConditionReturn],
];

// check existProp on mapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface CreateInitialMappingOptions {
sourceObj: unknown,
sourcePath: string
) => boolean;
isMetadataNullAtKey?: (key: string) => boolean;
prePropertiesLoop?: (mapping: Mapping) => void;
}

Expand All @@ -52,6 +53,7 @@ export function createInitialMapping(
const {
isMultipartSourcePathsInSource = defaultIsMultipartSourcePathsInSource,
isDestinationPathOnSource = defaultIsDestinationPathOnSource,
isMetadataNullAtKey = () => false,
prePropertiesLoop,
} = createInitialMappingOptions ?? {};
const { extends: bases = [], namingConventions: conventions } = options ?? {};
Expand Down Expand Up @@ -105,7 +107,13 @@ export function createInitialMapping(

mapping[MappingClassId.properties].push([
destinationPath,
[[destinationPath], [mapInitialize(...sourcePaths!)]],
[
[destinationPath],
[
mapInitialize(...sourcePaths!),
isMetadataNullAtKey(destinationPath),
],
],
destinationNestedMetadataAtPath,
]);
continue;
Expand All @@ -118,7 +126,10 @@ export function createInitialMapping(

mapping[MappingClassId.properties].push([
destinationPath,
[[destinationPath, sourcePath], [mapInitialize(sourcePath)]],
[
[destinationPath, sourcePath],
[mapInitialize(sourcePath), isMetadataNullAtKey(destinationPath)],
],
destinationNestedMetadataAtPath,
]);
}
Expand Down
17 changes: 9 additions & 8 deletions packages/core/src/lib/map/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ function map<
,
[
transformationMapFn,
isMetadataNull = false,
[
transformationPreCondPredicate,
preCondDefaultValue = undefined,
Expand Down Expand Up @@ -272,17 +273,17 @@ Original error: ${originalError}`;
) {
const mapInitializedValue = (transformationMapFn[
MapFnClassId.fn
] as MapInitializeReturn[1])(sourceObj);
] as MapInitializeReturn[MapFnClassId.fn])(sourceObj);

// if null/undefined
if (mapInitializedValue == null) {
setMember(() => mapInitializedValue);
continue;
}

// if isDate
if (mapInitializedValue instanceof Date) {
setMember(() => new Date(mapInitializedValue));
// if metadata is null, treat as-is
if (
mapInitializedValue == null ||
mapInitializedValue instanceof Date ||
isMetadataNull
) {
setMember(() => mapInitializedValue);
continue;
}

Expand Down

0 comments on commit 97f260d

Please sign in to comment.