Skip to content

Commit

Permalink
feat: add cvModelsAreEquals
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Jul 19, 2023
1 parent 3c79a82 commit e404c46
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/cv/lib/cv.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ export 'src/typedefs.dart'
newModel,
newModelList,
CvBuilderFunction;
export 'src/utils.dart' show cvModelsAreEquals;
2 changes: 1 addition & 1 deletion packages/cv/lib/src/cv_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'field.dart';
/// If 2 values are equals, entering nested list/map if any.
bool cvValuesAreEqual(dynamic v1, dynamic v2) {
try {
return DeepCollectionEquality().equals(v1, v2);
return const DeepCollectionEquality().equals(v1, v2);
} catch (_) {
return v1 == v2;
}
Expand Down
3 changes: 1 addition & 2 deletions packages/cv/lib/src/cv_model_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'builder.dart';
import 'cv_field.dart';
import 'env_utils.dart';
import 'field.dart';
import 'utils.dart';

/// For dev only
var debugContent = false; // devWarning(true);
Expand Down Expand Up @@ -84,7 +83,7 @@ mixin CvModelMixin implements CvModel {
@override
bool operator ==(other) {
if (other is CvModelRead) {
return cvModelAreEquals(this, other);
return cvModelsAreEquals(this, other);
}
return false;
}
Expand Down
30 changes: 23 additions & 7 deletions packages/cv/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,31 @@ bool isBasicTypeOrNull(dynamic value) {
return false;
}

/// If 2 content are equals
bool cvModelAreEquals(CvModelRead model1, CvModelRead model2) {
if (model1.fields.length != model2.fields.length) {
return false;
}
for (var cvField in model2.fields) {
if (model1.dynamicField(cvField.name) != cvField) {
/// If 2 models are equals
@Deprecated('Use cvModelsAreEquals')
bool cvModelAreEquals(CvModelRead model1, CvModelRead model2) =>
cvModelsAreEquals(model1, model2);

/// If 2 models are equals
bool cvModelsAreEquals(CvModelRead model1, CvModelRead model2,
{List<String>? columns}) {
if (columns == null) {
if (model1.fields.length != model2.fields.length) {
return false;
}
for (var cvField in model2.fields) {
if (model1.dynamicField(cvField.name) != cvField) {
return false;
}
}
} else {
for (var column in columns) {
var field1 = model1.dynamicField(column);
var field2 = model2.dynamicField(column);
if (field1 != field2) {
return false;
}
}
}
return true;
}
29 changes: 29 additions & 0 deletions packages/cv/test/cv_model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ class IntContent extends CvModelBase {
List<CvField> get fields => [value];
}

class TwoFieldsContent extends CvModelBase {
final value1 = CvField<int>('value1');
final value2 = CvField<int>('value2');

@override
List<CvField> get fields => [value1, value2];
}

/// Builder
IntContent intContentBuilder(Map map) => IntContent();

Expand Down Expand Up @@ -510,6 +518,27 @@ void main() {
expect(model.test.v, '2');
expect(model.toMap(), {'dep': 'pre', 'test': 'pre,2'});
});
test('cvModelAreEquals', () {
var content1 = TwoFieldsContent()
..value1.v = 1
..value2.v = 2;
var content2 = TwoFieldsContent()
..value1.v = 1
..value2.v = 2;
expect(cvModelsAreEquals(content1, content2), true);
content1.value1.v = 3;
expect(cvModelsAreEquals(content1, content2), false);
expect(
cvModelsAreEquals(content1, content2, columns: [content1.value1.key]),
false);
expect(
cvModelsAreEquals(content1, content2, columns: [content1.value2.key]),
true);
expect(
cvModelsAreEquals(content1, content2,
columns: [content1.value1.key, content1.value2.key]),
false);
});
});
}

Expand Down

0 comments on commit e404c46

Please sign in to comment.