20
20
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
21
21
*/
22
22
23
+ using BH . Engine . Base ;
24
+ using BH . oM . Base ;
25
+ using BH . oM . Base . Attributes ;
26
+ using BH . oM . Diffing ;
23
27
using System ;
28
+ using System . Collections ;
24
29
using System . Collections . Generic ;
25
- using System . Linq ;
26
- using System . Text ;
27
- using System . Threading . Tasks ;
28
30
using System . ComponentModel ;
29
- using BH . oM . Base . Attributes ;
30
- using BH . oM . Diffing ;
31
- using BH . oM . Base ;
31
+ using System . Linq ;
32
32
using kellerman = KellermanSoftware . CompareNetObjects ;
33
- using System . Reflection ;
34
- using BH . Engine . Base ;
35
- using BH . Engine . Reflection ;
36
- using System . Collections ;
37
33
38
34
namespace BH . Engine . Diffing
39
35
{
@@ -51,13 +47,28 @@ public static partial class Query
51
47
"\n If no difference was found, returns null." ) ]
52
48
public static ObjectDifferences ObjectDifferences ( this object pastObject , object followingObject , BaseComparisonConfig comparisonConfig = null )
53
49
{
54
- // Result object.
55
50
ObjectDifferences result = new ObjectDifferences ( ) { PastObject = pastObject , FollowingObject = followingObject } ;
56
-
51
+
57
52
// Null check. At least one of the objects must be not null.
58
53
if ( pastObject == null && followingObject == null )
59
54
return result ;
60
55
56
+ result . Differences . AddRange ( pastObject . Differences ( followingObject , comparisonConfig ) ) ;
57
+ return result ;
58
+ }
59
+
60
+
61
+ /***************************************************/
62
+ /**** Private Methods ****/
63
+ /***************************************************/
64
+
65
+ [ Description ( "Finds the actual differences between the objects." ) ]
66
+ private static IEnumerable < PropertyDifference > Differences ( this object pastObject , object followingObject , BaseComparisonConfig comparisonConfig = null )
67
+ {
68
+ // Result object.
69
+ List < PropertyDifference > returned = new List < PropertyDifference > ( ) ;
70
+ PropertyDifference toReturn ;
71
+
61
72
// Set ComparisonConfig if null. Clone it for immutability in the UI.
62
73
BaseComparisonConfig cc = comparisonConfig == null ? new ComparisonConfig ( ) : comparisonConfig . DeepClone ( ) ;
63
74
@@ -122,8 +133,12 @@ public static ObjectDifferences ObjectDifferences(this object pastObject, object
122
133
{
123
134
ComparisonInclusion comparisonInclusion = comparisonInclusionFromExtensionMethod as ComparisonInclusion ;
124
135
if ( comparisonInclusion != null && comparisonInclusion . Include )
136
+ {
125
137
// Add to the final result.
126
- result . AddPropertyDifference ( comparisonInclusion . DisplayName , kellermanPropertyDifference . Object1 , kellermanPropertyDifference . Object2 , propertyFullName ) ;
138
+ toReturn = PropertyDifference ( pastObject , followingObject , comparisonInclusion . DisplayName , kellermanPropertyDifference . Object1 , kellermanPropertyDifference . Object2 , propertyFullName ) ;
139
+ returned . Add ( toReturn ) ;
140
+ yield return toReturn ;
141
+ }
127
142
128
143
// Because a `ComparisonInclusion()` extension method was found, we've already determined if this difference was to be considered or not. Continue.
129
144
continue ;
@@ -142,7 +157,7 @@ public static ObjectDifferences ObjectDifferences(this object pastObject, object
142
157
propertyFullName = propertyFullName . Replace ( ".Value" , "" ) ;
143
158
144
159
// Workaround for Kellerman duplicating the CustomData differences.
145
- if ( result . Differences . Any ( d => d . FullName == propertyFullName ) )
160
+ if ( returned . Any ( d => d . FullName == propertyFullName ) )
146
161
continue ;
147
162
148
163
// Check if we are talking about CustomObjects.
@@ -185,40 +200,37 @@ public static ObjectDifferences ObjectDifferences(this object pastObject, object
185
200
continue ;
186
201
187
202
// Add to the final result.
188
- result . AddPropertyDifference ( propertyDisplayName , kellermanPropertyDifference . Object1 , kellermanPropertyDifference . Object2 , propertyFullName ) ;
203
+ toReturn = PropertyDifference ( pastObject , followingObject , propertyDisplayName , kellermanPropertyDifference . Object1 , kellermanPropertyDifference . Object2 , propertyFullName ) ;
204
+ returned . Add ( toReturn ) ;
205
+ yield return toReturn ;
189
206
}
190
-
191
- if ( result . Differences . Count == 0 )
192
- return null ;
193
-
194
- return result ;
195
207
}
196
208
197
- /***************************************************/
198
- /**** Private Methods ****/
199
209
/***************************************************/
200
210
201
211
[ Description ( "Removes square bracket indexing from property paths, e.g. `Bar.Fragments[0].Something` is returned as `Bar.Fragments.Something`." ) ]
202
- private static void AddPropertyDifference ( this ObjectDifferences objectDifferences , string propertyDiffDisplayName , object pastValue , object follValue , string fullName , string description = null )
212
+ private static PropertyDifference PropertyDifference ( this object pastObject , object followingObject , string propertyDiffDisplayName , object pastValue , object follValue , string fullName , string description = null )
203
213
{
204
214
description = string . IsNullOrWhiteSpace ( description ) ?
205
- PropertyDifferenceDescription ( objectDifferences , propertyDiffDisplayName , pastValue , follValue )
215
+ PropertyDifferenceDescription ( pastObject , followingObject , propertyDiffDisplayName , pastValue , follValue )
206
216
: description ;
207
217
208
- objectDifferences . Differences . Add ( new PropertyDifference ( )
218
+ return new PropertyDifference ( )
209
219
{
210
220
Name = propertyDiffDisplayName ,
211
221
Description = description ,
212
222
PastValue = pastValue ,
213
223
FollowingValue = follValue ,
214
224
FullName = fullName
215
- } ) ; ;
225
+ } ;
216
226
}
217
227
218
- private static string PropertyDifferenceDescription ( ObjectDifferences objectDifferences , string propertyDiffDisplayName , object pastValue , object follValue , bool includeObjName = true , bool includeObjType = false , bool includeObjGuid = false )
228
+ /***************************************************/
229
+
230
+ private static string PropertyDifferenceDescription ( this object pastObject , object followingObject , string propertyDiffDisplayName , object pastValue , object follValue , bool includeObjName = true , bool includeObjType = false , bool includeObjGuid = false )
219
231
{
220
232
if ( pastValue is IEnumerable && follValue is IEnumerable )
221
- return $ "The collection stored in the property `{ propertyDiffDisplayName } ` of the `{ objectDifferences . PastObject . GetType ( ) . FullName } ` was modified.";
233
+ return $ "The collection stored in the property `{ propertyDiffDisplayName } ` of the `{ pastObject . GetType ( ) . FullName } ` was modified.";
222
234
223
235
Type t = pastValue ? . GetType ( ) ?? follValue ? . GetType ( ) ?? typeof ( object ) ;
224
236
@@ -228,12 +240,12 @@ private static string PropertyDifferenceDescription(ObjectDifferences objectDiff
228
240
{
229
241
bool addedObjectDescription = false ;
230
242
231
- objectDescription = objectDifferences . PastObject is CustomObject ? "CustomObject " : "Object " ;
243
+ objectDescription = pastObject is CustomObject ? "CustomObject " : "Object " ;
232
244
233
245
if ( includeObjName )
234
246
{
235
- string pastObjName = ( objectDifferences . PastObject as IBHoMObject ) ? . Name ;
236
- string follObjName = ( objectDifferences . FollowingObject as IBHoMObject ) ? . Name ;
247
+ string pastObjName = ( pastObject as IBHoMObject ) ? . Name ;
248
+ string follObjName = ( followingObject as IBHoMObject ) ? . Name ;
237
249
238
250
if ( ! string . IsNullOrWhiteSpace ( pastObjName ) && pastObjName == follObjName )
239
251
{
@@ -242,16 +254,16 @@ private static string PropertyDifferenceDescription(ObjectDifferences objectDiff
242
254
}
243
255
}
244
256
245
- if ( includeObjType && ! ( objectDifferences . PastObject is CustomObject ) )
257
+ if ( includeObjType && ! ( pastObject is CustomObject ) )
246
258
{
247
- objectDescription += $ "of type `{ objectDifferences . PastObject . GetType ( ) . FullName } ` ";
259
+ objectDescription += $ "of type `{ pastObject . GetType ( ) . FullName } ` ";
248
260
addedObjectDescription = true ;
249
261
}
250
262
251
263
if ( includeObjGuid )
252
264
{
253
- Guid ? pastObjGuid = ( objectDifferences . PastObject as IBHoMObject ) ? . BHoM_Guid ;
254
- Guid ? follObjGuid = ( objectDifferences . FollowingObject as IBHoMObject ) ? . BHoM_Guid ;
265
+ Guid ? pastObjGuid = ( pastObject as IBHoMObject ) ? . BHoM_Guid ;
266
+ Guid ? follObjGuid = ( followingObject as IBHoMObject ) ? . BHoM_Guid ;
255
267
256
268
if ( pastObjGuid != null && pastObjGuid == follObjGuid )
257
269
{
@@ -301,10 +313,7 @@ private static bool PropertyNameWildcardMatch(this string propertyName, string w
301
313
302
314
return propertyName . WildcardMatch ( wildcardPattern ) ;
303
315
}
316
+
317
+ /***************************************************/
304
318
}
305
319
}
306
-
307
-
308
-
309
-
310
-
0 commit comments