Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable nullable globally and fix issues #21

Merged
merged 8 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project>

<PropertyGroup>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<langversion>10.0</langversion>
</PropertyGroup>

</Project>
103 changes: 42 additions & 61 deletions src/Cid.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using Google.Protobuf;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using Google.Protobuf;
using Newtonsoft.Json;

namespace Ipfs
{
Expand Down Expand Up @@ -32,11 +30,11 @@ public class Cid : IEquatable<Cid>
/// </summary>
public const string DefaultContentType = "dag-pb";

string encodedValue;
string? encodedValue;
int version;
string encoding = MultiBase.DefaultAlgorithmName;
string contentType = DefaultContentType;
MultiHash hash;
MultiHash? hash;

/// <summary>
/// Throws if a property cannot be set.
Expand All @@ -50,7 +48,7 @@ public class Cid : IEquatable<Cid>
/// </remarks>
void EnsureMutable()
{
if (encodedValue != null)
if (encodedValue is not null)
{
throw new NotSupportedException("CID cannot be changed.");
}
Expand Down Expand Up @@ -170,13 +168,13 @@ public MultiHash Hash
{
get
{
return hash;
return hash ?? throw new InvalidDataException("Hash has not been set");
}
set
{
EnsureMutable();
hash = value;
if (Version == 0 && Hash.Algorithm.Name != "sha2-256")
if (Version == 0 && value.Algorithm.Name != "sha2-256")
{
Version = 1;
}
Expand Down Expand Up @@ -243,13 +241,10 @@ public string ToString(string format)
sb.Append(Version);
sb.Append(' ');
sb.Append(ContentType);
if (Hash != null)
{
sb.Append(' ');
sb.Append(Hash.Algorithm.Name);
sb.Append(' ');
sb.Append(MultiBase.Encode(Hash.ToArray(), Encoding).Substring(1));
}
sb.Append(' ');
sb.Append(Hash.Algorithm.Name);
sb.Append(' ');
sb.Append(MultiBase.Encode(Hash.ToArray(), Encoding).Substring(1));
return sb.ToString();

default:
Expand All @@ -265,14 +260,14 @@ public string ToString(string format)
/// The string representation of the <see cref="Cid"/>.
/// </returns>
/// <remarks>
/// For <see cref="Version"/> 0, this is equalivalent to the
/// For <see cref="Version"/> 0, this is equivalent to the
/// <see cref="MultiHash.ToBase58()">base58btc encoding</see>
/// of the <see cref="Hash"/>.
/// </remarks>
/// <seealso cref="Decode"/>
public string Encode()
{
if (encodedValue != null)
if (encodedValue is not null)
{
return encodedValue;
}
Expand All @@ -293,22 +288,22 @@ public string Encode()
return encodedValue;
}

/// <summary>
/// Converts the specified <see cref="string"/>
/// to an equivalent <see cref="Cid"/> object.
/// </summary>
/// <param name="input">
/// The <see cref="Cid.Encode">CID encoded</see> string.
/// </param>
/// <returns>
/// A new <see cref="Cid"/> that is equivalent to <paramref name="input"/>.
/// </returns>
/// <exception cref="FormatException">
/// When the <paramref name="input"/> can not be decoded.
/// </exception>
/// <seealso cref="Encode"/>
public static Cid Decode(string input)
{
/// <summary>
/// Converts the specified <see cref="string"/>
/// to an equivalent <see cref="Cid"/> object.
/// </summary>
/// <param name="input">
/// The <see cref="Cid.Encode">CID encoded</see> string.
/// </param>
/// <returns>
/// A new <see cref="Cid"/> that is equivalent to <paramref name="input"/>.
/// </returns>
/// <exception cref="FormatException">
/// When the <paramref name="input"/> can not be decoded.
/// </exception>
/// <seealso cref="Encode"/>
public static Cid Decode(string input)
{
try
{
// SHA2-256 MultiHash is CID v0.
Expand All @@ -332,11 +327,11 @@ public static Cid Decode(string input)
Hash = new MultiHash(ms)
};
}
}
}
catch (Exception e)
{
throw new FormatException($"Invalid CID '{input}'.", e);
}
}
}

/// <summary>
Expand Down Expand Up @@ -532,10 +527,10 @@ public override int GetHashCode()
}

/// <inheritdoc />
public override bool Equals(object obj)
public override bool Equals(object? obj)
{
var that = obj as Cid;
return (that == null)
return (that is null)
? false
: this.Encode() == that.Encode();
}
Expand All @@ -549,17 +544,17 @@ public bool Equals(Cid that)
/// <summary>
/// Value equality.
/// </summary>
public static bool operator ==(Cid a, Cid b)
public static bool operator ==(Cid? a, Cid? b)
{
if (object.ReferenceEquals(a, b))
{
return true;
}
if (object.ReferenceEquals(a, null))
if (a is null)
{
return false;
}
if (object.ReferenceEquals(b, null))
if (b is null)
{
return false;
}
Expand All @@ -569,21 +564,9 @@ public bool Equals(Cid that)
/// <summary>
/// Value inequality.
/// </summary>
public static bool operator !=(Cid a, Cid b)
public static bool operator !=(Cid? a, Cid? b)
{
if (object.ReferenceEquals(a, b))
{
return false;
}
if (object.ReferenceEquals(a, null))
{
return true;
}
if (object.ReferenceEquals(b, null))
{
return true;
}
return !a.Equals(b);
return !(a == b);
}

/// <summary>
Expand Down Expand Up @@ -641,19 +624,17 @@ public override bool CanConvert(Type objectType)
public override bool CanWrite => true;

/// <inheritdoc />
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
var cid = value as Cid;
writer.WriteValue(cid?.Encode());
}

/// <inheritdoc />
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
var s = reader.Value as string;
return s == null ? null : Cid.Decode(s);
return reader.Value is string s ? Cid.Decode(s) : null;
}
}

}
}
14 changes: 6 additions & 8 deletions src/CoreApi/AddFileOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Ipfs.CoreApi
{
Expand All @@ -14,8 +12,8 @@ public class AddFileOptions
/// Determines if the data is pinned to local storage.
/// </summary>
/// <value>
/// If <b>true</b> the data is pinned to local storage and will not be
/// garbage collected. The default is <b>true</b>.
/// If <b>true</b> the data is pinned to local storage and will not be
/// garbage collected. The default is <b>true</b>.
/// </value>
public bool Pin { get; set; } = true;

Expand Down Expand Up @@ -58,7 +56,7 @@ public class AddFileOptions
/// The hashing algorithm name to use.
/// </summary>
/// <value>
/// The <see cref="MultiHash"/> algorithm name used to produce the <see cref="Cid"/>.
/// The <see cref="MultiHash"/> algorithm name used to produce the <see cref="Cid"/>.
/// Defaults to <see cref="MultiHash.DefaultAlgorithmName"/>.
/// </value>
/// <seealso cref="MultiHash"/>
Expand All @@ -68,7 +66,7 @@ public class AddFileOptions
/// The encoding algorithm name to use.
/// </summary>
/// <value>
/// The <see cref="MultiBase"/> algorithm name used to produce the <see cref="Cid"/>.
/// The <see cref="MultiBase"/> algorithm name used to produce the <see cref="Cid"/>.
/// Defaults to <see cref="MultiBase.DefaultAlgorithmName"/>.
/// </value>
/// <seealso cref="MultiBase"/>
Expand All @@ -92,11 +90,11 @@ public class AddFileOptions
/// <b>ProtectionKey</b> and <see cref="RawLeaves"/> are mutually exclusive.
/// </remarks>
/// <seealso cref="IKeyApi"/>
public string ProtectionKey { get; set; }
public string? ProtectionKey { get; set; }

/// <summary>
/// Used to report the progress of a file transfer.
/// </summary>
public IProgress<TransferProgress> Progress { get; set; }
public IProgress<TransferProgress>? Progress { get; set; }
}
}
8 changes: 3 additions & 5 deletions src/CoreApi/BitswapData.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;

namespace Ipfs.CoreApi
{
Expand All @@ -17,12 +15,12 @@ public class BitswapData
/// <summary>
/// The content that is wanted.
/// </summary>
public IEnumerable<Cid> Wantlist;
public IEnumerable<Cid>? Wantlist;

/// <summary>
/// The known peers.
/// </summary>
public IEnumerable<MultiHash> Peers;
public IEnumerable<MultiHash>? Peers;

/// <summary>
/// The number of blocks sent by other peers.
Expand Down
8 changes: 2 additions & 6 deletions src/CoreApi/BitswapLedger.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Ipfs.CoreApi
namespace Ipfs.CoreApi
{
/// <summary>
/// Statistics on the <see cref="IBitswapApi">bitswap</see> blocks exchanged with another <see cref="Peer"/>.
Expand All @@ -16,7 +12,7 @@ public class BitswapLedger
/// <value>
/// The peer that is being monitored.
/// </value>
public Peer Peer { get; set; }
public Peer? Peer { get; set; }

/// <summary>
/// The number of blocks exchanged with the <see cref="Peer"/>.
Expand Down
4 changes: 1 addition & 3 deletions src/CoreApi/FileStat.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Ipfs.CoreApi
{
Expand All @@ -13,7 +11,7 @@ public class FileStatResult
/// <summary>
/// The <see cref="Cid"/> of the file or directory.
/// </summary>
public Cid Hash { get; set; }
public Cid? Hash { get; set; }

/// <summary>
/// The serialised size (in bytes) of the linked node.
Expand Down
Loading