Skip to content

Commit c39f829

Browse files
committed
v3.0.0 - Bring some more things from main app - ABI Breaks
1 parent e5b9145 commit c39f829

38 files changed

+414
-60
lines changed

InterlockLedger.Commons.NUnit.Tests/System.Collections.Generic/Types/SingleEnumerableTests.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@
3030
//
3131
// ******************************************************************************************************************************
3232

33-
using NUnit.Framework;
34-
3533
using System.Linq;
34+
using NUnit.Framework;
3635

3736
namespace System.Collections.Generic.Tests;
3837

@@ -61,4 +60,4 @@ public void SingleEnumerableTest() {
6160
Assert.IsFalse(enumerator.MoveNext());
6261
Assert.AreEqual(0, enumerator.Current);
6362
}
64-
}
63+
}

InterlockLedger.Commons.NUnit.Tests/System.IO/Types/StreamSpanTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,4 @@ public NonSeekMemoryStream(byte[] buffer) : base(buffer, writable: true) {
122122

123123
public override bool CanSeek => false;
124124
}
125-
}
125+
}

InterlockLedger.Commons.NUnit.Tests/System/StringExtensionsTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@ private static bool TestRequiredUsing(string? value, string name) =>
6161
&& AssertArgumentException<ArgumentNullException>(nameof(value), () => value.RequiredUsing(n => new ArgumentNullException(n, _expectedExceptionMessageStart)))
6262
&& AssertArgumentException<ArgumentNullException>(nameof(value), () => value.RequiredUsing(ArgNullRequired));
6363

64-
}
64+
}

InterlockLedger.Commons.NUnit.Tests/System/StringSuffixExtensionsTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,4 @@ public void WithSuffixReplaced() {
7272
Assert.AreEqual(".file.txt", ".file.doc ".WithSuffixReplaced("txt", '.'));
7373
Assert.AreEqual("file.txt", "file. ".WithSuffixReplaced("txt"));
7474
}
75-
}
75+
}

InterlockLedger.Commons/Exceptions/System/InterlockLedgerException.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ protected InterlockLedgerException(string message) : base(message) {
4141

4242
protected InterlockLedgerException(string message, Exception innerException) : base(message, innerException) {
4343
}
44-
}
44+
}

InterlockLedger.Commons/Exceptions/System/TooFewBytesException.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ public TooFewBytesException() : base("Insuficcient bytes to read") {
4141

4242
public TooFewBytesException(int length) : base($"Not able to read {length} bytes from stream") {
4343
}
44-
}
44+
}

InterlockLedger.Commons/Extensions/System.Buffers/ReadOnlySequenceExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,4 @@ private static IEnumerable<ReadOnlyMemory<byte>> PrependMemory(this ReadOnlySequ
100100
while (sequence.TryGet(ref current, out var segment))
101101
yield return segment;
102102
}
103-
}
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// ******************************************************************************************************************************
2+
//
3+
// Copyright (c) 2018-2022 InterlockLedger Network
4+
// All rights reserved.
5+
//
6+
// Redistribution and use in source and binary forms, with or without
7+
// modification, are permitted provided that the following conditions are met
8+
//
9+
// * Redistributions of source code must retain the above copyright notice, this
10+
// list of conditions and the following disclaimer.
11+
//
12+
// * Redistributions in binary form must reproduce the above copyright notice,
13+
// this list of conditions and the following disclaimer in the documentation
14+
// and/or other materials provided with the distribution.
15+
//
16+
// * Neither the name of the copyright holder nor the names of its
17+
// contributors may be used to endorse or promote products derived from
18+
// this software without specific prior written permission.
19+
//
20+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
// SERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER
27+
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
//
31+
// ******************************************************************************************************************************
32+
33+
namespace System.Collections.Generic;
34+
35+
public static class IEnumerableOfClassExtensions
36+
{
37+
public static IEnumerable<T> NonNulls<T>(this IEnumerable<T?>? values) where T : class
38+
=> values.Safe().Skip(item => item is null)!;
39+
40+
public static T[] NonEmpty<T>([NotNull] this T[] items, [CallerArgumentExpression("items")] string? parameterName = null) =>
41+
items is null || items.Length == 0 ? throw new ArgumentException("Should not be empty", parameterName) : items;
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// ******************************************************************************************************************************
2+
//
3+
// Copyright (c) 2018-2022 InterlockLedger Network
4+
// All rights reserved.
5+
//
6+
// Redistribution and use in source and binary forms, with or without
7+
// modification, are permitted provided that the following conditions are met
8+
//
9+
// * Redistributions of source code must retain the above copyright notice, this
10+
// list of conditions and the following disclaimer.
11+
//
12+
// * Redistributions in binary form must reproduce the above copyright notice,
13+
// this list of conditions and the following disclaimer in the documentation
14+
// and/or other materials provided with the distribution.
15+
//
16+
// * Neither the name of the copyright holder nor the names of its
17+
// contributors may be used to endorse or promote products derived from
18+
// this software without specific prior written permission.
19+
//
20+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
// SERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER
27+
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
//
31+
// ******************************************************************************************************************************
32+
33+
namespace System.Collections.Generic;
34+
public static class IEnumerableOfStringExtensions
35+
{
36+
public static string AsLines(this IEnumerable<string>? source) =>
37+
source.JoinedBy(Environment.NewLine);
38+
}

InterlockLedger.Commons/Extensions/System.Collections.Generic/ListExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ public static class ListExtensions
3838
list?.AddRange(itens);
3939
return list;
4040
}
41-
}
41+
}

InterlockLedger.Commons/Extensions/System.IO/FileInfoExtensions.cs

+21-6
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,37 @@ namespace System.IO;
3434

3535
public static class FileInfoExtensions
3636
{
37-
public static FileStream GetWritingStream(this FileInfo fileInfo, Action<FileInfo> use) => new FbbaInputStream(fileInfo, use);
37+
public static FileStream GetWritingStream(this FileInfo fileInfo, Action<FileInfo> onDispose) => new FbbaInputStream(fileInfo, onDispose);
38+
39+
public static string? ReadAllText(this FileInfo file) {
40+
if (!file.Exists)
41+
return null;
42+
using var s = file.OpenText();
43+
return s.ReadToEnd();
44+
}
45+
46+
public static async Task<string> ReadToEndAsync(this FileInfo file, string missingFileMessageMask) {
47+
if (file.Required().Exists) {
48+
using var reader = file.OpenText();
49+
return await reader.ReadToEndAsync().ConfigureAwait(false);
50+
}
51+
return string.Format(missingFileMessageMask.Required(), file.Name);
52+
}
3853

3954
private class FbbaInputStream : FileStream
4055
{
41-
public FbbaInputStream(FileInfo fileInfo, Action<FileInfo> use) : base(fileInfo.Required().FullName, FileMode.CreateNew, FileAccess.Write) {
56+
public FbbaInputStream(FileInfo fileInfo, Action<FileInfo> onDispose) : base(fileInfo.Required().FullName, FileMode.CreateNew, FileAccess.Write) {
4257
_fileInfo = fileInfo.Required();
43-
_use = use.Required();
58+
_onDispose = onDispose.Required();
4459
}
4560

4661
protected override void Dispose(bool disposing) {
4762
base.Dispose(disposing);
4863
if (disposing)
49-
_use(_fileInfo);
64+
_onDispose(_fileInfo);
5065
}
5166

5267
private readonly FileInfo _fileInfo;
53-
private readonly Action<FileInfo> _use;
68+
private readonly Action<FileInfo> _onDispose;
5469
}
55-
}
70+
}

InterlockLedger.Commons/Extensions/System.IO/StreamExtensions-EndianNative.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,4 @@ public static void LittleEndianWriteLong(this Stream s, long value) =>
101101

102102
public static void LittleEndianWriteShort(this Stream s, short value) =>
103103
WriteBytes(s, value.ToBytes());
104-
}
104+
}

InterlockLedger.Commons/Extensions/System.Linq/IEnumerableOfBoolTExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ public static class IEnumerableOfBoolTExtensions
4141
public static bool AnyFalse(this IEnumerable<bool> items) => items.Safe().Any(b => !b);
4242

4343
public static bool AnyTrue(this IEnumerable<bool> items) => items.Safe().Any(b => b);
44-
}
44+
}

InterlockLedger.Commons/Extensions/System.Linq/IEnumerableOfReadOnlyMemoryExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ public static T[] ToArray<T>(this IEnumerable<ReadOnlyMemory<T>> buffers) {
4747

4848
return result;
4949
}
50-
}
50+
}

InterlockLedger.Commons/Extensions/System.Security.Claims/ClaimsIdentityExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ namespace System.Security.Claims;
3535
public static class ClaimsIdentityExtensions
3636
{
3737
public static bool HasRole(this ClaimsIdentity identity, string roleName) => identity?.HasClaim(ClaimTypes.Role, roleName) ?? false;
38-
}
38+
}

InterlockLedger.Commons/Extensions/System.Security.Cryptography.X509Certificates/PublicKeyExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ namespace System.Security.Cryptography.X509Certificates;
3535
public static class PublicKeyExtensions
3636
{
3737
public static byte[]? ToBytes(this PublicKey? publicKey) => publicKey?.EncodedKeyValue?.RawData;
38-
}
38+
}

InterlockLedger.Commons/Extensions/System.Security.Cryptography.X509Certificates/X509Certificate2Extensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ public static string SimpleName(this X509Certificate2 certificate) =>
4242

4343
private static string DottedName(this X509Certificate2 certificate) =>
4444
certificate.Required().SubjectName.Name.Split(',').Select(part => part.Split('=').Last()).Reverse().JoinedBy(".");
45-
}
45+
}

InterlockLedger.Commons/Extensions/System/ArrayOfByteExtensions.cs

+8
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ public static byte[] PartOf(this byte[] bytes, int length, int offset = 0) {
157157
Array.Copy(bytes, offset, part, 0, length);
158158
return part;
159159
}
160+
public static byte[] RandomBytes(this int size) {
161+
byte[] buffer = new byte[size];
162+
_rnd.NextBytes(buffer);
163+
return buffer;
164+
}
160165

161166
public static int SafeGetHashCode(this byte[] bytes) => bytes?.ToSafeBase64().GetHashCode(StringComparison.InvariantCulture) ?? 0;
162167

@@ -179,4 +184,7 @@ private static string PadBase64(string base64) {
179184
base64 += "=";
180185
return base64;
181186
}
187+
188+
private static readonly Random _rnd = new();
189+
182190
}

InterlockLedger.Commons/Extensions/System/ConsoleExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ******************************************************************************************************************************
1+
// ******************************************************************************************************************************
22
//
33
// Copyright (c) 2018-2022 InterlockLedger Network
44
// All rights reserved.

InterlockLedger.Commons/Extensions/System/DateOnlyConverter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ******************************************************************************************************************************
1+
// ******************************************************************************************************************************
22
//
33
// Copyright (c) 2018-2022 InterlockLedger Network
44
// All rights reserved.

InterlockLedger.Commons/Extensions/System/IntegerExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ public static byte[] ToBytes(this int value) {
4242
Array.Reverse(bytes);
4343
return bytes;
4444
}
45-
}
45+
}

InterlockLedger.Commons/Extensions/System/LongExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ public static byte[] ToBytes(this long value) {
4040
Array.Reverse(bytes);
4141
return bytes;
4242
}
43-
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// ******************************************************************************************************************************
2+
//
3+
// Copyright (c) 2018-2022 InterlockLedger Network
4+
// All rights reserved.
5+
//
6+
// Redistribution and use in source and binary forms, with or without
7+
// modification, are permitted provided that the following conditions are met
8+
//
9+
// * Redistributions of source code must retain the above copyright notice, this
10+
// list of conditions and the following disclaimer.
11+
//
12+
// * Redistributions in binary form must reproduce the above copyright notice,
13+
// this list of conditions and the following disclaimer in the documentation
14+
// and/or other materials provided with the distribution.
15+
//
16+
// * Neither the name of the copyright holder nor the names of its
17+
// contributors may be used to endorse or promote products derived from
18+
// this software without specific prior written permission.
19+
//
20+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
// SERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER
27+
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
//
31+
// ******************************************************************************************************************************
32+
33+
namespace System;
34+
35+
public static class NullableOfStructExtensions
36+
{
37+
public static T Required<T>(this T? value, [CallerArgumentExpression("value")] string? name = null) where T : struct
38+
=> value ?? throw new ArgumentException("Required", name);
39+
}

InterlockLedger.Commons/Extensions/System/ObjectExtensions.cs

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
namespace System;
3434

35+
36+
3537
public static class ObjectExtensions
3638
{
3739
public static IEnumerable<T> AsSingle<T>(this T s) =>

0 commit comments

Comments
 (0)