1
- using Google . Protobuf ;
2
- using Newtonsoft . Json ;
3
- using System ;
4
- using System . Collections . Generic ;
1
+ using System ;
5
2
using System . IO ;
6
- using System . Runtime . Serialization ;
7
3
using System . Text ;
4
+ using Google . Protobuf ;
5
+ using Newtonsoft . Json ;
8
6
9
7
namespace Ipfs
10
8
{
@@ -32,11 +30,11 @@ public class Cid : IEquatable<Cid>
32
30
/// </summary>
33
31
public const string DefaultContentType = "dag-pb" ;
34
32
35
- string encodedValue ;
33
+ string ? encodedValue ;
36
34
int version ;
37
35
string encoding = MultiBase . DefaultAlgorithmName ;
38
36
string contentType = DefaultContentType ;
39
- MultiHash hash ;
37
+ MultiHash ? hash ;
40
38
41
39
/// <summary>
42
40
/// Throws if a property cannot be set.
@@ -50,7 +48,7 @@ public class Cid : IEquatable<Cid>
50
48
/// </remarks>
51
49
void EnsureMutable ( )
52
50
{
53
- if ( encodedValue != null )
51
+ if ( encodedValue is not null )
54
52
{
55
53
throw new NotSupportedException ( "CID cannot be changed." ) ;
56
54
}
@@ -170,13 +168,13 @@ public MultiHash Hash
170
168
{
171
169
get
172
170
{
173
- return hash ;
171
+ return hash ?? throw new InvalidDataException ( "Hash has not been set" ) ;
174
172
}
175
173
set
176
174
{
177
175
EnsureMutable ( ) ;
178
176
hash = value ;
179
- if ( Version == 0 && Hash . Algorithm . Name != "sha2-256" )
177
+ if ( Version == 0 && value . Algorithm . Name != "sha2-256" )
180
178
{
181
179
Version = 1 ;
182
180
}
@@ -243,13 +241,10 @@ public string ToString(string format)
243
241
sb . Append ( Version ) ;
244
242
sb . Append ( ' ' ) ;
245
243
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 ) ) ;
253
248
return sb . ToString ( ) ;
254
249
255
250
default :
@@ -265,14 +260,14 @@ public string ToString(string format)
265
260
/// The string representation of the <see cref="Cid"/>.
266
261
/// </returns>
267
262
/// <remarks>
268
- /// For <see cref="Version"/> 0, this is equalivalent to the
263
+ /// For <see cref="Version"/> 0, this is equivalent to the
269
264
/// <see cref="MultiHash.ToBase58()">base58btc encoding</see>
270
265
/// of the <see cref="Hash"/>.
271
266
/// </remarks>
272
267
/// <seealso cref="Decode"/>
273
268
public string Encode ( )
274
269
{
275
- if ( encodedValue != null )
270
+ if ( encodedValue is not null )
276
271
{
277
272
return encodedValue ;
278
273
}
@@ -293,22 +288,22 @@ public string Encode()
293
288
return encodedValue ;
294
289
}
295
290
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
+ {
312
307
try
313
308
{
314
309
// SHA2-256 MultiHash is CID v0.
@@ -332,11 +327,11 @@ public static Cid Decode(string input)
332
327
Hash = new MultiHash ( ms )
333
328
} ;
334
329
}
335
- }
330
+ }
336
331
catch ( Exception e )
337
332
{
338
333
throw new FormatException ( $ "Invalid CID '{ input } '.", e ) ;
339
- }
334
+ }
340
335
}
341
336
342
337
/// <summary>
@@ -532,10 +527,10 @@ public override int GetHashCode()
532
527
}
533
528
534
529
/// <inheritdoc />
535
- public override bool Equals ( object obj )
530
+ public override bool Equals ( object ? obj )
536
531
{
537
532
var that = obj as Cid ;
538
- return ( that == null )
533
+ return ( that is null )
539
534
? false
540
535
: this . Encode ( ) == that . Encode ( ) ;
541
536
}
@@ -549,17 +544,17 @@ public bool Equals(Cid that)
549
544
/// <summary>
550
545
/// Value equality.
551
546
/// </summary>
552
- public static bool operator == ( Cid a , Cid b )
547
+ public static bool operator == ( Cid ? a , Cid ? b )
553
548
{
554
549
if ( object . ReferenceEquals ( a , b ) )
555
550
{
556
551
return true ;
557
552
}
558
- if ( object . ReferenceEquals ( a , null ) )
553
+ if ( a is null )
559
554
{
560
555
return false ;
561
556
}
562
- if ( object . ReferenceEquals ( b , null ) )
557
+ if ( b is null )
563
558
{
564
559
return false ;
565
560
}
@@ -569,21 +564,9 @@ public bool Equals(Cid that)
569
564
/// <summary>
570
565
/// Value inequality.
571
566
/// </summary>
572
- public static bool operator != ( Cid a , Cid b )
567
+ public static bool operator != ( Cid ? a , Cid ? b )
573
568
{
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 ) ;
587
570
}
588
571
589
572
/// <summary>
@@ -641,19 +624,17 @@ public override bool CanConvert(Type objectType)
641
624
public override bool CanWrite => true ;
642
625
643
626
/// <inheritdoc />
644
- public override void WriteJson ( JsonWriter writer , object value , JsonSerializer serializer )
627
+ public override void WriteJson ( JsonWriter writer , object ? value , JsonSerializer serializer )
645
628
{
646
629
var cid = value as Cid ;
647
630
writer . WriteValue ( cid ? . Encode ( ) ) ;
648
631
}
649
632
650
633
/// <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 )
652
635
{
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 ;
655
637
}
656
638
}
657
-
658
639
}
659
640
}
0 commit comments