Skip to content

Commit 3450fee

Browse files
authored
Merge pull request #21 from erikmav/dev/erikmav/nullable
Enable nullable globally and fix issues
2 parents 0b30f98 + 0721fb8 commit 3450fee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1652
-1894
lines changed

Directory.Build.props

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project>
2+
3+
<PropertyGroup>
4+
<Nullable>enable</Nullable>
5+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
6+
<langversion>10.0</langversion>
7+
</PropertyGroup>
8+
9+
</Project>

src/Cid.cs

Lines changed: 42 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
using Google.Protobuf;
2-
using Newtonsoft.Json;
3-
using System;
4-
using System.Collections.Generic;
1+
using System;
52
using System.IO;
6-
using System.Runtime.Serialization;
73
using System.Text;
4+
using Google.Protobuf;
5+
using Newtonsoft.Json;
86

97
namespace Ipfs
108
{
@@ -32,11 +30,11 @@ public class Cid : IEquatable<Cid>
3230
/// </summary>
3331
public const string DefaultContentType = "dag-pb";
3432

35-
string encodedValue;
33+
string? encodedValue;
3634
int version;
3735
string encoding = MultiBase.DefaultAlgorithmName;
3836
string contentType = DefaultContentType;
39-
MultiHash hash;
37+
MultiHash? hash;
4038

4139
/// <summary>
4240
/// Throws if a property cannot be set.
@@ -50,7 +48,7 @@ public class Cid : IEquatable<Cid>
5048
/// </remarks>
5149
void EnsureMutable()
5250
{
53-
if (encodedValue != null)
51+
if (encodedValue is not null)
5452
{
5553
throw new NotSupportedException("CID cannot be changed.");
5654
}
@@ -170,13 +168,13 @@ public MultiHash Hash
170168
{
171169
get
172170
{
173-
return hash;
171+
return hash ?? throw new InvalidDataException("Hash has not been set");
174172
}
175173
set
176174
{
177175
EnsureMutable();
178176
hash = value;
179-
if (Version == 0 && Hash.Algorithm.Name != "sha2-256")
177+
if (Version == 0 && value.Algorithm.Name != "sha2-256")
180178
{
181179
Version = 1;
182180
}
@@ -243,13 +241,10 @@ public string ToString(string format)
243241
sb.Append(Version);
244242
sb.Append(' ');
245243
sb.Append(ContentType);
246-
if (Hash != null)
247-
{
248-
sb.Append(' ');
249-
sb.Append(Hash.Algorithm.Name);
250-
sb.Append(' ');
251-
sb.Append(MultiBase.Encode(Hash.ToArray(), Encoding).Substring(1));
252-
}
244+
sb.Append(' ');
245+
sb.Append(Hash.Algorithm.Name);
246+
sb.Append(' ');
247+
sb.Append(MultiBase.Encode(Hash.ToArray(), Encoding).Substring(1));
253248
return sb.ToString();
254249

255250
default:
@@ -265,14 +260,14 @@ public string ToString(string format)
265260
/// The string representation of the <see cref="Cid"/>.
266261
/// </returns>
267262
/// <remarks>
268-
/// For <see cref="Version"/> 0, this is equalivalent to the
263+
/// For <see cref="Version"/> 0, this is equivalent to the
269264
/// <see cref="MultiHash.ToBase58()">base58btc encoding</see>
270265
/// of the <see cref="Hash"/>.
271266
/// </remarks>
272267
/// <seealso cref="Decode"/>
273268
public string Encode()
274269
{
275-
if (encodedValue != null)
270+
if (encodedValue is not null)
276271
{
277272
return encodedValue;
278273
}
@@ -293,22 +288,22 @@ public string Encode()
293288
return encodedValue;
294289
}
295290

296-
/// <summary>
297-
/// Converts the specified <see cref="string"/>
298-
/// to an equivalent <see cref="Cid"/> object.
299-
/// </summary>
300-
/// <param name="input">
301-
/// The <see cref="Cid.Encode">CID encoded</see> string.
302-
/// </param>
303-
/// <returns>
304-
/// A new <see cref="Cid"/> that is equivalent to <paramref name="input"/>.
305-
/// </returns>
306-
/// <exception cref="FormatException">
307-
/// When the <paramref name="input"/> can not be decoded.
308-
/// </exception>
309-
/// <seealso cref="Encode"/>
310-
public static Cid Decode(string input)
311-
{
291+
/// <summary>
292+
/// Converts the specified <see cref="string"/>
293+
/// to an equivalent <see cref="Cid"/> object.
294+
/// </summary>
295+
/// <param name="input">
296+
/// The <see cref="Cid.Encode">CID encoded</see> string.
297+
/// </param>
298+
/// <returns>
299+
/// A new <see cref="Cid"/> that is equivalent to <paramref name="input"/>.
300+
/// </returns>
301+
/// <exception cref="FormatException">
302+
/// When the <paramref name="input"/> can not be decoded.
303+
/// </exception>
304+
/// <seealso cref="Encode"/>
305+
public static Cid Decode(string input)
306+
{
312307
try
313308
{
314309
// SHA2-256 MultiHash is CID v0.
@@ -332,11 +327,11 @@ public static Cid Decode(string input)
332327
Hash = new MultiHash(ms)
333328
};
334329
}
335-
}
330+
}
336331
catch (Exception e)
337332
{
338333
throw new FormatException($"Invalid CID '{input}'.", e);
339-
}
334+
}
340335
}
341336

342337
/// <summary>
@@ -532,10 +527,10 @@ public override int GetHashCode()
532527
}
533528

534529
/// <inheritdoc />
535-
public override bool Equals(object obj)
530+
public override bool Equals(object? obj)
536531
{
537532
var that = obj as Cid;
538-
return (that == null)
533+
return (that is null)
539534
? false
540535
: this.Encode() == that.Encode();
541536
}
@@ -549,17 +544,17 @@ public bool Equals(Cid that)
549544
/// <summary>
550545
/// Value equality.
551546
/// </summary>
552-
public static bool operator ==(Cid a, Cid b)
547+
public static bool operator ==(Cid? a, Cid? b)
553548
{
554549
if (object.ReferenceEquals(a, b))
555550
{
556551
return true;
557552
}
558-
if (object.ReferenceEquals(a, null))
553+
if (a is null)
559554
{
560555
return false;
561556
}
562-
if (object.ReferenceEquals(b, null))
557+
if (b is null)
563558
{
564559
return false;
565560
}
@@ -569,21 +564,9 @@ public bool Equals(Cid that)
569564
/// <summary>
570565
/// Value inequality.
571566
/// </summary>
572-
public static bool operator !=(Cid a, Cid b)
567+
public static bool operator !=(Cid? a, Cid? b)
573568
{
574-
if (object.ReferenceEquals(a, b))
575-
{
576-
return false;
577-
}
578-
if (object.ReferenceEquals(a, null))
579-
{
580-
return true;
581-
}
582-
if (object.ReferenceEquals(b, null))
583-
{
584-
return true;
585-
}
586-
return !a.Equals(b);
569+
return !(a == b);
587570
}
588571

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

643626
/// <inheritdoc />
644-
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
627+
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
645628
{
646629
var cid = value as Cid;
647630
writer.WriteValue(cid?.Encode());
648631
}
649632

650633
/// <inheritdoc />
651-
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
634+
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
652635
{
653-
var s = reader.Value as string;
654-
return s == null ? null : Cid.Decode(s);
636+
return reader.Value is string s ? Cid.Decode(s) : null;
655637
}
656638
}
657-
658639
}
659640
}

src/CoreApi/AddFileOptions.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
42

53
namespace Ipfs.CoreApi
64
{
@@ -14,8 +12,8 @@ public class AddFileOptions
1412
/// Determines if the data is pinned to local storage.
1513
/// </summary>
1614
/// <value>
17-
/// If <b>true</b> the data is pinned to local storage and will not be
18-
/// garbage collected. The default is <b>true</b>.
15+
/// If <b>true</b> the data is pinned to local storage and will not be
16+
/// garbage collected. The default is <b>true</b>.
1917
/// </value>
2018
public bool Pin { get; set; } = true;
2119

@@ -58,7 +56,7 @@ public class AddFileOptions
5856
/// The hashing algorithm name to use.
5957
/// </summary>
6058
/// <value>
61-
/// The <see cref="MultiHash"/> algorithm name used to produce the <see cref="Cid"/>.
59+
/// The <see cref="MultiHash"/> algorithm name used to produce the <see cref="Cid"/>.
6260
/// Defaults to <see cref="MultiHash.DefaultAlgorithmName"/>.
6361
/// </value>
6462
/// <seealso cref="MultiHash"/>
@@ -68,7 +66,7 @@ public class AddFileOptions
6866
/// The encoding algorithm name to use.
6967
/// </summary>
7068
/// <value>
71-
/// The <see cref="MultiBase"/> algorithm name used to produce the <see cref="Cid"/>.
69+
/// The <see cref="MultiBase"/> algorithm name used to produce the <see cref="Cid"/>.
7270
/// Defaults to <see cref="MultiBase.DefaultAlgorithmName"/>.
7371
/// </value>
7472
/// <seealso cref="MultiBase"/>
@@ -92,11 +90,11 @@ public class AddFileOptions
9290
/// <b>ProtectionKey</b> and <see cref="RawLeaves"/> are mutually exclusive.
9391
/// </remarks>
9492
/// <seealso cref="IKeyApi"/>
95-
public string ProtectionKey { get; set; }
93+
public string? ProtectionKey { get; set; }
9694

9795
/// <summary>
9896
/// Used to report the progress of a file transfer.
9997
/// </summary>
100-
public IProgress<TransferProgress> Progress { get; set; }
98+
public IProgress<TransferProgress>? Progress { get; set; }
10199
}
102100
}

src/CoreApi/BitswapData.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
1+
using System.Collections.Generic;
42

53
namespace Ipfs.CoreApi
64
{
@@ -17,12 +15,12 @@ public class BitswapData
1715
/// <summary>
1816
/// The content that is wanted.
1917
/// </summary>
20-
public IEnumerable<Cid> Wantlist;
18+
public IEnumerable<Cid>? Wantlist;
2119

2220
/// <summary>
2321
/// The known peers.
2422
/// </summary>
25-
public IEnumerable<MultiHash> Peers;
23+
public IEnumerable<MultiHash>? Peers;
2624

2725
/// <summary>
2826
/// The number of blocks sent by other peers.

src/CoreApi/BitswapLedger.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
4-
5-
namespace Ipfs.CoreApi
1+
namespace Ipfs.CoreApi
62
{
73
/// <summary>
84
/// Statistics on the <see cref="IBitswapApi">bitswap</see> blocks exchanged with another <see cref="Peer"/>.
@@ -16,7 +12,7 @@ public class BitswapLedger
1612
/// <value>
1713
/// The peer that is being monitored.
1814
/// </value>
19-
public Peer Peer { get; set; }
15+
public Peer? Peer { get; set; }
2016

2117
/// <summary>
2218
/// The number of blocks exchanged with the <see cref="Peer"/>.

src/CoreApi/FileStat.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
42

53
namespace Ipfs.CoreApi
64
{
@@ -13,7 +11,7 @@ public class FileStatResult
1311
/// <summary>
1412
/// The <see cref="Cid"/> of the file or directory.
1513
/// </summary>
16-
public Cid Hash { get; set; }
14+
public Cid? Hash { get; set; }
1715

1816
/// <summary>
1917
/// The serialised size (in bytes) of the linked node.

0 commit comments

Comments
 (0)