Skip to content

Commit

Permalink
Added 'areEqual' to PropertyConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
meirgottlieb committed Jul 26, 2016
1 parent 7dc16a8 commit edd5175
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hydrate-mongodb",
"description": "An Object Document Mapper (ODM) for MongoDB.",
"version": "0.7.2",
"version": "0.8.0",
"author": {
"name": "Artifact Health, LLC",
"url": "http://www.artifacthealth.com"
Expand Down
17 changes: 17 additions & 0 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,25 @@ export interface IdentityGenerator {
*/
export interface PropertyConverter {

/**
* Converts an object property value to a document field value.
* @param property The property value to convert.
*/
convertToDocumentField(property: any): any;

/**
* Converts a document field value to an object property value.
* @param field The field value to convert.
*/
convertToObjectProperty(field: any): any;

/**
* Returns true if the document field values are equal; otherwise, returns false. This method is only called if both values are not
* null and the values are not strictly equal.
* @param field1 First document field value.
* @param field2 Other document field value.
*/
areEqual(field1: any, field2: any): boolean;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/mapping/bufferMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@ export class BufferMapping extends MappingBase {
return new Binary(value);
}

areEqual(documentValue1: any, documentValue2: any): boolean {

if (documentValue1 === documentValue2) return true;
if (documentValue1 === null || documentValue2 === null) return false;

return documentValue1.value(true).equals(documentValue2.value(true));
}
}
9 changes: 9 additions & 0 deletions src/mapping/converterMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,13 @@ export class ConverterMapping extends MappingBase {

return result;
}

areEqual(documentValue1: any, documentValue2: any): boolean {

if (documentValue1 === documentValue2) return true;
if (documentValue1 === null || documentValue2 === null) return false;
if (documentValue1 !== documentValue1 && documentValue2 !== documentValue2) return true; // NaN === NaN

return false;
}
}
17 changes: 17 additions & 0 deletions src/mapping/mappingModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,25 @@ export namespace MappingModel {

export interface PropertyConverter {

/**
* Converts an object property value to a document field value.
* @param property The property value to convert.
*/
convertToDocumentField(property: any): any;

/**
* Converts a document field value to an object property value.
* @param field The field value to convert.
*/
convertToObjectProperty(field: any): any;

/**
* Returns true if the document field values are equal; otherwise, returns false. This method is only called if both values are not
* null and the values are not strictly equal.
* @param field1 First document field value.
* @param field2 Other document field value.
*/
areEqual(field1: any, field2: any): boolean;
}

export const enum ChangeTrackingType {
Expand Down
7 changes: 6 additions & 1 deletion tests/fixtures/annotations/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SomeConverter implements PropertyConverter {
switch(property) {
case MyEnum.value1:
return "A";
case MyEnum.value1:
case MyEnum.value2:
return "B";
case MyEnum.value3:
return "C";
Expand All @@ -33,6 +33,11 @@ class SomeConverter implements PropertyConverter {
return MyEnum.value3;
}
}

areEqual(field1: any, field2: any): boolean {

return field1 === field2;
}
}

@Entity()
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export class PhoneTypeConverter implements PropertyConverter {
return PhoneType.Home;
}
}

areEqual(field1: any, field2: any): boolean {

return field1 === field2;
}
}

@Embeddable()
Expand Down
53 changes: 53 additions & 0 deletions tests/mapping/bufferMapping.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,57 @@ describe('BufferMapping', () => {
});
});

describe("areEqual", () => {

it('returns true if both values are null', () => {

var mapping = new BufferMapping();
assert.isTrue(mapping.areEqual(null, null));
});

it('return false if one value is null and the other is not', () => {

var mapping = new BufferMapping();

assert.isFalse(mapping.areEqual(null, {}));
assert.isFalse(mapping.areEqual({}, null));
});

it("returns true if the contents of the Buffers are equal", () => {

compareBuffers("Some value", "Some value", true);
});

it("returns false if the contents of the Buffers are not equal", () => {

compareBuffers("Some value", "Some other value", false);
});

function compareBuffers(text1: string, text2: string, result: boolean): void {

var mapping = new BufferMapping();

var buffer1 = new Buffer(text1),
buffer2 = new Buffer(text2);

var context1 = new WriteContext(),
context2 = new WriteContext();

var value1 = mapping.write(context1, buffer1),
value2 = mapping.write(context2, buffer2);

assert.ok(value1);
assert.ok(value2);

if (context1.hasErrors) {
throw new Error(context1.getErrorMessage());
}

if (context2.hasErrors) {
throw new Error(context2.getErrorMessage());
}

assert.equal(mapping.areEqual(value1, value2), result);
}
});
});
10 changes: 10 additions & 0 deletions tests/mapping/providers/annotationMappingProvider.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,11 @@ class MyEnumConverter implements PropertyConverter {
return ConverterFixture.B;
}
}

areEqual(field1: any, field2: any): boolean {

return field1 === field2;
}
}

class PointConverter implements PropertyConverter {
Expand All @@ -546,4 +551,9 @@ class PointConverter implements PropertyConverter {
var parts = field.split('.');
return new ConverterOnClassFixture.Point(parts[0], parts[1]);
}

areEqual(field1: any, field2: any): boolean {

return field1 === field2;
}
}

0 comments on commit edd5175

Please sign in to comment.