Skip to content

Commit

Permalink
fix: proxy changelog of maps and sets
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximvdw committed Jun 17, 2024
1 parent ac927ee commit f6a3775
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/data/decorators/ChangeLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,22 @@ export function createChangeLog<T extends Object>(target: T): T & SerializableCh
const metadata = DataSerializerUtils.getOwnMetadata(target.constructor);
if (metadata) {
metadata.dataMembers.forEach((member) => {
if (target[member.key] && target[member.key] instanceof Object) {
target[member.key] = createChangeLog(target[member.key]);
if (target[member.key]) {
if (Array.isArray(target[member.key])) {
target[member.key].forEach((element) => {
if (element instanceof Object) {
element = createChangeLog(element);
}
});
} else if (target[member.key] instanceof Map || target[member.key] instanceof Set) {
target[member.key].forEach((element) => {
if (element instanceof Object) {
element = createChangeLog(element);
}
});
} else if (target[member.key] instanceof Object) {
target[member.key] = createChangeLog(target[member.key]);
}
}
});
}
Expand Down
1 change: 1 addition & 0 deletions src/nodes/processing/CalibrationNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ObjectProcessingNode, ObjectProcessingNodeOptions } from '../ObjectProc
/**
* Calibration node for sensors. This node allows intercepts data frames when
* performing user-aided calibration.
* @type {@link http://purl.org/poso/CalibrationProcedure}
*/
export class CalibrationNode<T extends DataObject = DataObject> extends ObjectProcessingNode {
protected state: CalibrationState = CalibrationState.IDLE;
Expand Down
18 changes: 18 additions & 0 deletions test/specs/data/data.object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
SerializableMember,
createChangeLog,
CHANGELOG_METADATA_KEY,
SerializableObject,
SerializableMapMember,
} from '../../../src';
import { DummySensorObject } from '../../mock/data/object/DummySensorObject';

Expand Down Expand Up @@ -309,5 +311,21 @@ describe('DataObject', () => {
expect(objectWithChangelog[CHANGELOG_METADATA_KEY].getDeletedProperties().length).to.equal(0);
expect(objectWithChangelog[CHANGELOG_METADATA_KEY].getAddedProperties().length).to.equal(1);
});

it('should not break maps', () => {
@SerializableObject()
class MyObject {
@SerializableMapMember(String, String)
features: Map<string, string>;
}

const object = new MyObject();
object.features = new Map();
object.features.set('test', '123');
const objectWithChangelog = createChangeLog(object);
objectWithChangelog.features.forEach((value, key) => {
});
});

});
});

0 comments on commit f6a3775

Please sign in to comment.