-
Notifications
You must be signed in to change notification settings - Fork 3
/
FingerprintBuilder.cs
90 lines (70 loc) · 3.45 KB
/
FingerprintBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Serialization.Formatters.Binary;
using JetBrains.Annotations;
namespace Reusable.Cryptography
{
public delegate byte[] ComputeHashCallback(byte[] buffer);
public delegate byte[] ComputeFingerprintCallback<in T>(T obj);
[PublicAPI]
public class FingerprintBuilder<T>
{
// private static readonly ValidationRuleCollection<FingerprintBuilder<T>, object> FingerprintBuilderValidator =
// ValidationRuleCollection
// .For<FingerprintBuilder<T>>()
// .Accept(b => b.When(x => x._valueSelectors.Any()).Message($"{nameof(FingerprintBuilder<T>)} requires at least one value-selector."));
private readonly SortedDictionary<string, Func<T, object>> _valueSelectors;
public FingerprintBuilder([NotNull] IComparer<string> propertyComparer)
{
if (propertyComparer == null) throw new ArgumentNullException(nameof(propertyComparer));
_valueSelectors = new SortedDictionary<string, Func<T, object>>(StringComparer.OrdinalIgnoreCase);
}
public FingerprintBuilder()
: this(StringComparer.OrdinalIgnoreCase) { }
public FingerprintBuilder<T> Add<TInput, TOutput>([NotNull] Expression<Func<T, TInput>> getMemberExpression, [NotNull] Expression<Func<TInput, TOutput>> transformValueExpression)
{
if (getMemberExpression == null) throw new ArgumentNullException(nameof(getMemberExpression));
if (transformValueExpression == null) throw new ArgumentNullException(nameof(transformValueExpression));
if (!(getMemberExpression.Body is MemberExpression memberExpression))
{
throw new ArgumentException("Expression must be a member expression.");
}
if (_valueSelectors.ContainsKey(memberExpression.Member.Name))
{
throw new ArgumentException($"Member '{memberExpression.Member.Name}' has already been added.");
}
var getValue = getMemberExpression.Compile();
var transform = transformValueExpression.Compile();
_valueSelectors[memberExpression.Member.Name] = obj => transform(getValue(obj));
return this;
}
public ComputeFingerprintCallback<T> Build([NotNull] ComputeHashCallback computeHashCallback)
{
if (computeHashCallback == null) throw new ArgumentNullException(nameof(computeHashCallback));
//this.ValidateWith(FingerprintBuilderValidator).ThrowOnFailure();
if (_valueSelectors.Any() == false) throw new InvalidOperationException("You need to specify at least one selector.");
var binaryFormatter = new BinaryFormatter();
return obj =>
{
using var memory = new MemoryStream();
foreach (var item in _valueSelectors)
{
if (item.Value(obj) is {} value)
{
binaryFormatter.Serialize(memory, value);
}
}
return computeHashCallback(memory.ToArray());
};
}
}
[PublicAPI]
public class FingerprintBuilder
{
public static FingerprintBuilder<T> For<T>() => new FingerprintBuilder<T>();
public static FingerprintBuilder<T> For<T>(T obj) => For<T>();
}
}