Skip to content

Commit

Permalink
2.2.3 - Refactor some extension methods
Browse files Browse the repository at this point in the history
  • Loading branch information
monoman committed Nov 25, 2019
1 parent 67ad493 commit 5840a58
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 25 deletions.
54 changes: 54 additions & 0 deletions InterlockLedger.Tags.UnitTests/TagHelpersTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/******************************************************************************************************************************
*
* Copyright (c) 2017-2019 InterlockLedger Network
*
******************************************************************************************************************************/

using System;
using NUnit.Framework;

namespace InterlockLedger.Tags
{
[TestFixture]
public class TagHelpersTests
{
[Test]
public void DecodedTest() {
var tag = new ILTagILInt(14);
var decoded = tag.EncodedBytes.Decoded();
Assert.AreEqual(tag, decoded);
}

[Test]
public void ToBytesHelperTest() {
var tag = new ILTagILInt(23);
var bytes = TagHelpers.ToBytesHelper((s) => s.EncodeILInt(23));
Assert.IsNotNull(bytes);
Assert.That(tag.EncodedBytes, Is.EquivalentTo(bytes));
var e = Assert.Throws<ArgumentNullException>(() => TagHelpers.ToBytesHelper(null));
Assert.AreEqual("serialize", e.ParamName);
}

[Test]
public void ToTagArrayFromTest() {
var data = new string[] { "1", "2", "3", "5", "7", "11" };
var tagArray = data.ToTagArrayFrom(v => new ILTagString(v));
Assert.IsNotNull(tagArray);
Assert.AreEqual(data.Length, tagArray.Value.Length);
Assert.That(tagArray.GetValues<string>(), Is.EquivalentTo(data));
var e = Assert.Throws<ArgumentNullException>(() => data.ToTagArrayFrom(null));
Assert.AreEqual("convert", e.ParamName);
}

[Test]
public void ToTagArrayTest() {
var data = new ulong[] { 1, 2, 3, 5, 7, 11 };
var tagArray = data.ToTagArray(v => new ILTagILInt(v));
Assert.IsNotNull(tagArray);
Assert.AreEqual(data.Length, tagArray.Value.Length);
Assert.That(tagArray.GetValues<ulong>(), Is.EquivalentTo(data));
var e = Assert.Throws<ArgumentNullException>(() => data.ToTagArray<ulong, ILTagILInt>(null));
Assert.AreEqual("convert", e.ParamName);
}
}
}
2 changes: 2 additions & 0 deletions InterlockLedger.Tags/Core/Tags/ILTagArrayOfILTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public ILTagArrayOfILTag(object json) : base(ILTagId.ILTagArray, Elicit(json)) {

public T this[int i] => Value?[i];

public IEnumerable<TV> GetValues<TV>() => (Value ?? Enumerable.Empty<T>()).Select(t => t is ILTagImplicit<TV> tv ? tv.Value : default);

internal ILTagArrayOfILTag(Stream s) : base(ILTagId.ILTagArray, s) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public override byte[] AsSessionState {
ms.EncodeTag(_value);
ms.EncodeString(_password);
ms.EncodeByteArray(_certificateBytes);
ms.Flush();
return ms.GetBuffer();
return ms.ToArray();
}
}

Expand All @@ -43,7 +42,7 @@ public override byte[] AsSessionState {
}

public override byte[] Decrypt(byte[] bytes) {
using RSA rsa = _certificateBytes.OpenCertificate(_password).GetRSAPrivateKey();
using var rsa = _certificateBytes.OpenCertificate(_password).GetRSAPrivateKey();
return rsa.Decrypt(bytes, RSAEncryptionPadding.Pkcs1);
}

Expand All @@ -53,7 +52,7 @@ public override byte[] Decrypt(byte[] bytes) {
private readonly string _password;

private byte[] HashAndSign(byte[] dataToSign) {
using RSA rsa = _certificateBytes.OpenCertificate(_password).GetRSAPrivateKey();
using var rsa = _certificateBytes.OpenCertificate(_password).GetRSAPrivateKey();
return rsa.SignData(dataToSign, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public override byte[] AsSessionState {
using var ms = new MemoryStream();
ms.EncodeTag(_value);
ms.EncodeTag(_keyParameters);
ms.Flush();
return ms.GetBuffer();
return ms.ToArray();
}
}

Expand Down
6 changes: 5 additions & 1 deletion InterlockLedger.Tags/Extensions/IEnumerableOfTExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public static IEnumerable<T> IfAnyDo<T>(this IEnumerable<T> values, Action actio
return values;
}

public static string JoinedBy<T>(this IEnumerable<T> list, string joiner) => list == null ? string.Empty : string.Join(joiner, list);

public static bool None<T>(this IEnumerable<T> items) => !items.SafeAny();

public static bool None<T>(this IEnumerable<T> items, Func<T, bool> predicate) => !items.SafeAny(predicate);
Expand All @@ -50,6 +52,8 @@ public static IEnumerable<TResult> SelectSkippingNulls<TSource, TResult>(this IE

public static IEnumerable<T> SkipNulls<T>(this IEnumerable<T> values) where T : class => EmptyIfNull(values?.Where(item => item != null));

public static string WithCommas<T>(this IEnumerable<T> list, bool noSpaces = false) => JoinedBy(list, noSpaces ? "," : ", ");

public static IEnumerable<T> WithDefault<T>(this IEnumerable<T> values, Func<IEnumerable<T>> alternativeValues)
=> values.SafeAny() ? values : EmptyIfNull(alternativeValues?.Invoke());

Expand All @@ -69,4 +73,4 @@ private static IEnumerable<T> InnerAppend<T>(T item, IEnumerable<T> remainingIte
private static IEnumerable<T> InnerConcat<T>(IEnumerable<T> items, IEnumerable<T> remainingItems)
=> remainingItems.SafeAny() ? items.Concat(remainingItems) : items;
}
}
}
4 changes: 0 additions & 4 deletions InterlockLedger.Tags/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ public static class StringExtensions

public static bool IsEmptyOrMatches(this string s, params string[] matches) => string.IsNullOrWhiteSpace(s) || matches.Any(m => s.Trim().Equals(m, StringComparison.InvariantCultureIgnoreCase));

public static string JoinedBy<T>(this IEnumerable<T> list, string joiner) => list == null ? string.Empty : string.Join(joiner, list);

public static string Parenthesize(this string s) => s.ParenthesizeIf(!s.Parenthesized());

public static bool Parenthesized(this string s) {
Expand Down Expand Up @@ -96,8 +94,6 @@ public static string SimplifyAsFileName(this string name) {
public static string ValidateNonEmpty(this string parameter, string parameterName)
=> string.IsNullOrWhiteSpace(parameter) ? throw new ArgumentNullException(parameterName) : parameter.Trim();

public static string WithCommas<T>(this IEnumerable<T> list, bool noSpaces = false) => JoinedBy(list, noSpaces ? "," : ", ");

public static string WithDefault(this string s, string @default) => string.IsNullOrWhiteSpace(s) ? @default : s.Trim();

public static string WithDefault(this string s, Func<string> resolver) {
Expand Down
20 changes: 8 additions & 12 deletions InterlockLedger.Tags/Extensions/TagHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/******************************************************************************************************************************
Copyright (c) 2018-2019 InterlockLedger Network
All rights reserved.
Expand Down Expand Up @@ -42,21 +42,17 @@ public static class TagHelpers
public static ILTag Decoded(this byte[] buffer) => ILTag.DeserializeFrom(buffer);

public static byte[] ToBytesHelper(Action<Stream> serialize) {
if (serialize == null) {
if (serialize == null)
throw new ArgumentNullException(nameof(serialize));
}

using var s = new MemoryStream();
serialize(s);
s.Flush();
return s.GetBuffer().PartOf((int)s.Position);
return s.ToArray();
}

public static ILTagArrayOfILTag<TA> ToTagArray<T, TA>(this IEnumerable<T> list, Func<T, TA> convert) where TA : ILTag {
if (convert == null) {
throw new ArgumentNullException(nameof(convert));
}
return new ILTagArrayOfILTag<TA>(list?.Select(st => convert(st)).ToArray());
}
public static ILTagArrayOfILTag<TA> ToTagArray<T, TA>(this IEnumerable<T> list, Func<T, TA> convert) where TA : ILTag
=> convert == null ? throw new ArgumentNullException(nameof(convert)) : new ILTagArrayOfILTag<TA>(list?.Select(st => convert(st)).ToArray());

public static ILTagArrayOfILTag<ILTagExplicit<T>> ToTagArrayFrom<T>(this IEnumerable<T> list, Func<T, ILTagExplicit<T>> convert)
=> ToTagArray<T, ILTagExplicit<T>>(list, convert);
}
}
4 changes: 2 additions & 2 deletions InterlockLedger.Tags/InterlockLedger.Tags.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
<Description>This library implements the basic TLV encoding/decoding for core types.</Description>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://interlockledger.network/</PackageProjectUrl>
<PackageReleaseNotes>Bring more extension methods and cleanup code</PackageReleaseNotes>
<PackageReleaseNotes>Refactor some extension methods</PackageReleaseNotes>
<Product>InterlockLedger</Product>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/interlockledger/interlockledger-tags.git</RepositoryUrl>
<Version>2.2.2</Version>
<Version>2.2.3</Version>
</PropertyGroup>
<ItemGroup>
<None Include="..\LICENSE">
Expand Down

0 comments on commit 5840a58

Please sign in to comment.