Skip to content

Commit

Permalink
v3.0.0 - Bring some more things from main app - ABI Breaks
Browse files Browse the repository at this point in the history
  • Loading branch information
monoman committed Nov 1, 2022
1 parent e5b9145 commit c39f829
Show file tree
Hide file tree
Showing 38 changed files with 414 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@
//
// ******************************************************************************************************************************

using NUnit.Framework;

using System.Linq;
using NUnit.Framework;

namespace System.Collections.Generic.Tests;

Expand Down Expand Up @@ -61,4 +60,4 @@ public void SingleEnumerableTest() {
Assert.IsFalse(enumerator.MoveNext());
Assert.AreEqual(0, enumerator.Current);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,4 @@ public NonSeekMemoryStream(byte[] buffer) : base(buffer, writable: true) {

public override bool CanSeek => false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ private static bool TestRequiredUsing(string? value, string name) =>
&& AssertArgumentException<ArgumentNullException>(nameof(value), () => value.RequiredUsing(n => new ArgumentNullException(n, _expectedExceptionMessageStart)))
&& AssertArgumentException<ArgumentNullException>(nameof(value), () => value.RequiredUsing(ArgNullRequired));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ public void WithSuffixReplaced() {
Assert.AreEqual(".file.txt", ".file.doc ".WithSuffixReplaced("txt", '.'));
Assert.AreEqual("file.txt", "file. ".WithSuffixReplaced("txt"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ protected InterlockLedgerException(string message) : base(message) {

protected InterlockLedgerException(string message, Exception innerException) : base(message, innerException) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ public TooFewBytesException() : base("Insuficcient bytes to read") {

public TooFewBytesException(int length) : base($"Not able to read {length} bytes from stream") {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,4 @@ private static IEnumerable<ReadOnlyMemory<byte>> PrependMemory(this ReadOnlySequ
while (sequence.TryGet(ref current, out var segment))
yield return segment;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// ******************************************************************************************************************************

namespace System.Collections.Generic;

public static class IEnumerableOfClassExtensions
{
public static IEnumerable<T> NonNulls<T>(this IEnumerable<T?>? values) where T : class
=> values.Safe().Skip(item => item is null)!;

public static T[] NonEmpty<T>([NotNull] this T[] items, [CallerArgumentExpression("items")] string? parameterName = null) =>
items is null || items.Length == 0 ? throw new ArgumentException("Should not be empty", parameterName) : items;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// ******************************************************************************************************************************

namespace System.Collections.Generic;
public static class IEnumerableOfStringExtensions
{
public static string AsLines(this IEnumerable<string>? source) =>
source.JoinedBy(Environment.NewLine);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ public static class ListExtensions
list?.AddRange(itens);
return list;
}
}
}
27 changes: 21 additions & 6 deletions InterlockLedger.Commons/Extensions/System.IO/FileInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,37 @@ namespace System.IO;

public static class FileInfoExtensions
{
public static FileStream GetWritingStream(this FileInfo fileInfo, Action<FileInfo> use) => new FbbaInputStream(fileInfo, use);
public static FileStream GetWritingStream(this FileInfo fileInfo, Action<FileInfo> onDispose) => new FbbaInputStream(fileInfo, onDispose);

public static string? ReadAllText(this FileInfo file) {
if (!file.Exists)
return null;
using var s = file.OpenText();
return s.ReadToEnd();
}

public static async Task<string> ReadToEndAsync(this FileInfo file, string missingFileMessageMask) {
if (file.Required().Exists) {
using var reader = file.OpenText();
return await reader.ReadToEndAsync().ConfigureAwait(false);
}
return string.Format(missingFileMessageMask.Required(), file.Name);
}

private class FbbaInputStream : FileStream
{
public FbbaInputStream(FileInfo fileInfo, Action<FileInfo> use) : base(fileInfo.Required().FullName, FileMode.CreateNew, FileAccess.Write) {
public FbbaInputStream(FileInfo fileInfo, Action<FileInfo> onDispose) : base(fileInfo.Required().FullName, FileMode.CreateNew, FileAccess.Write) {
_fileInfo = fileInfo.Required();
_use = use.Required();
_onDispose = onDispose.Required();
}

protected override void Dispose(bool disposing) {
base.Dispose(disposing);
if (disposing)
_use(_fileInfo);
_onDispose(_fileInfo);
}

private readonly FileInfo _fileInfo;
private readonly Action<FileInfo> _use;
private readonly Action<FileInfo> _onDispose;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,4 @@ public static void LittleEndianWriteLong(this Stream s, long value) =>

public static void LittleEndianWriteShort(this Stream s, short value) =>
WriteBytes(s, value.ToBytes());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ public static class IEnumerableOfBoolTExtensions
public static bool AnyFalse(this IEnumerable<bool> items) => items.Safe().Any(b => !b);

public static bool AnyTrue(this IEnumerable<bool> items) => items.Safe().Any(b => b);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ public static T[] ToArray<T>(this IEnumerable<ReadOnlyMemory<T>> buffers) {

return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ namespace System.Security.Claims;
public static class ClaimsIdentityExtensions
{
public static bool HasRole(this ClaimsIdentity identity, string roleName) => identity?.HasClaim(ClaimTypes.Role, roleName) ?? false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ namespace System.Security.Cryptography.X509Certificates;
public static class PublicKeyExtensions
{
public static byte[]? ToBytes(this PublicKey? publicKey) => publicKey?.EncodedKeyValue?.RawData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ public static string SimpleName(this X509Certificate2 certificate) =>

private static string DottedName(this X509Certificate2 certificate) =>
certificate.Required().SubjectName.Name.Split(',').Select(part => part.Split('=').Last()).Reverse().JoinedBy(".");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ public static byte[] PartOf(this byte[] bytes, int length, int offset = 0) {
Array.Copy(bytes, offset, part, 0, length);
return part;
}
public static byte[] RandomBytes(this int size) {
byte[] buffer = new byte[size];
_rnd.NextBytes(buffer);
return buffer;
}

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

Expand All @@ -179,4 +184,7 @@ private static string PadBase64(string base64) {
base64 += "=";
return base64;
}

private static readonly Random _rnd = new();

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ******************************************************************************************************************************
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// All rights reserved.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ******************************************************************************************************************************
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// All rights reserved.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ public static byte[] ToBytes(this int value) {
Array.Reverse(bytes);
return bytes;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ public static byte[] ToBytes(this long value) {
Array.Reverse(bytes);
return bytes;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// ******************************************************************************************************************************

namespace System;

public static class NullableOfStructExtensions
{
public static T Required<T>(this T? value, [CallerArgumentExpression("value")] string? name = null) where T : struct
=> value ?? throw new ArgumentException("Required", name);
}
2 changes: 2 additions & 0 deletions InterlockLedger.Commons/Extensions/System/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

namespace System;



public static class ObjectExtensions
{
public static IEnumerable<T> AsSingle<T>(this T s) =>
Expand Down
Loading

0 comments on commit c39f829

Please sign in to comment.