Skip to content

Commit

Permalink
Annotate System.Resources.ResourceWriter for nullable ref types (dotn…
Browse files Browse the repository at this point in the history
…et/corefx#41880)

* Annotate System.Resources.ResourceWriter for nullable ref types

* ref

* Mark common as nullable enable explicitly

* _resourceList


Commit migrated from dotnet/corefx@1ed115a
  • Loading branch information
danmoseley authored and stephentoub committed Oct 19, 2019
1 parent aa7a953 commit dd7d333
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 62 deletions.
84 changes: 38 additions & 46 deletions src/libraries/Common/src/System/Resources/ResourceWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*============================================================
**
** Class: ResourceWriter
**
**
**
** Purpose: Default way to write strings to a CLR resource
** file.
**
**
===========================================================*/
#nullable enable

using System.IO;
using System.Text;
Expand Down Expand Up @@ -49,10 +39,10 @@ public sealed partial class
private const string ResSetTypeName = "System.Resources.RuntimeResourceSet";
private const int ResSetVersion = 2;

private SortedDictionary<string, object> _resourceList;
private SortedDictionary<string, object?>? _resourceList;
private Stream _output;
private Dictionary<string, object> _caseInsensitiveDups;
private Dictionary<string, PrecannedResource> _preserializedData;
private Dictionary<string, object?> _caseInsensitiveDups;
private Dictionary<string, PrecannedResource>? _preserializedData;

public
#if RESOURCES_EXTENSIONS
Expand All @@ -65,8 +55,8 @@ public sealed partial class
throw new ArgumentNullException(nameof(fileName));

_output = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
_resourceList = new SortedDictionary<string, object>(FastResourceComparer.Default);
_caseInsensitiveDups = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
_resourceList = new SortedDictionary<string, object?>(FastResourceComparer.Default);
_caseInsensitiveDups = new Dictionary<string, object?>(StringComparer.OrdinalIgnoreCase);
}

public
Expand All @@ -82,14 +72,14 @@ public sealed partial class
throw new ArgumentException(SR.Argument_StreamNotWritable);

_output = stream;
_resourceList = new SortedDictionary<string, object>(FastResourceComparer.Default);
_caseInsensitiveDups = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
_resourceList = new SortedDictionary<string, object?>(FastResourceComparer.Default);
_caseInsensitiveDups = new Dictionary<string, object?>(StringComparer.OrdinalIgnoreCase);
}

// Adds a string resource to the list of resources to be written to a file.
// They aren't written until Generate() is called.
//
public void AddResource(string name, string value)
public void AddResource(string name, string? value)
{
if (name == null)
throw new ArgumentNullException(nameof(name));
Expand All @@ -105,7 +95,7 @@ public void AddResource(string name, string value)
// Adds a resource of type Object to the list of resources to be
// written to a file. They aren't written until Generate() is called.
//
public void AddResource(string name, object value)
public void AddResource(string name, object? value)
{
if (name == null)
throw new ArgumentNullException(nameof(name));
Expand All @@ -130,7 +120,7 @@ public void AddResource(string name, object value)
// written to a file. They aren't written until Generate() is called.
// closeAfterWrite parameter indicates whether to close the stream when done.
//
public void AddResource(string name, Stream value, bool closeAfterWrite = false)
public void AddResource(string name, Stream? value, bool closeAfterWrite = false)
{
if (name == null)
throw new ArgumentNullException(nameof(name));
Expand All @@ -141,8 +131,10 @@ public void AddResource(string name, Stream value, bool closeAfterWrite = false)
AddResourceInternal(name, value, closeAfterWrite);
}

private void AddResourceInternal(string name, Stream value, bool closeAfterWrite)
private void AddResourceInternal(string name, Stream? value, bool closeAfterWrite)
{
Debug.Assert(_resourceList != null);

if (value == null)
{
// Check for duplicate resources whose names vary only by case.
Expand All @@ -164,7 +156,7 @@ private void AddResourceInternal(string name, Stream value, bool closeAfterWrite
// Adds a named byte array as a resource to the list of resources to
// be written to a file. They aren't written until Generate() is called.
//
public void AddResource(string name, byte[] value)
public void AddResource(string name, byte[]? value)
{
if (name == null)
throw new ArgumentNullException(nameof(name));
Expand Down Expand Up @@ -236,8 +228,8 @@ private void Dispose(bool disposing)
}
}

_output = null;
_caseInsensitiveDups = null;
_output = null!;
_caseInsensitiveDups = null!;
}

public void Dispose()
Expand Down Expand Up @@ -329,7 +321,7 @@ public void Generate()
names.Write(item.Key); // key
names.Write((int)data.Seek(0, SeekOrigin.Current)); // virtual offset of value.

object value = item.Value;
object? value = item.Value;
ResourceTypeCode typeCode = FindTypeCode(value, typeNames);

// Write out type code
Expand Down Expand Up @@ -441,7 +433,7 @@ private static void Write7BitEncodedInt(BinaryWriter store, int value)

// Finds the ResourceTypeCode for a type, or adds this type to the
// types list.
private ResourceTypeCode FindTypeCode(object value, List<string> types)
private ResourceTypeCode FindTypeCode(object? value, List<string> types)
{
if (value == null)
return ResourceTypeCode.Null;
Expand Down Expand Up @@ -514,7 +506,7 @@ private ResourceTypeCode FindTypeCode(object value, List<string> types)
return (ResourceTypeCode)(typeIndex + ResourceTypeCode.StartOfUserTypes);
}

private void WriteValue(ResourceTypeCode typeCode, object value, BinaryWriter writer)
private void WriteValue(ResourceTypeCode typeCode, object? value, BinaryWriter writer)
{
Debug.Assert(writer != null);

Expand All @@ -524,83 +516,83 @@ private void WriteValue(ResourceTypeCode typeCode, object value, BinaryWriter wr
break;

case ResourceTypeCode.String:
writer.Write((string)value);
writer.Write((string)value!);
break;

case ResourceTypeCode.Boolean:
writer.Write((bool)value);
writer.Write((bool)value!);
break;

case ResourceTypeCode.Char:
writer.Write((ushort)(char)value);
writer.Write((ushort)(char)value!);
break;

case ResourceTypeCode.Byte:
writer.Write((byte)value);
writer.Write((byte)value!);
break;

case ResourceTypeCode.SByte:
writer.Write((sbyte)value);
writer.Write((sbyte)value!);
break;

case ResourceTypeCode.Int16:
writer.Write((short)value);
writer.Write((short)value!);
break;

case ResourceTypeCode.UInt16:
writer.Write((ushort)value);
writer.Write((ushort)value!);
break;

case ResourceTypeCode.Int32:
writer.Write((int)value);
writer.Write((int)value!);
break;

case ResourceTypeCode.UInt32:
writer.Write((uint)value);
writer.Write((uint)value!);
break;

case ResourceTypeCode.Int64:
writer.Write((long)value);
writer.Write((long)value!);
break;

case ResourceTypeCode.UInt64:
writer.Write((ulong)value);
writer.Write((ulong)value!);
break;

case ResourceTypeCode.Single:
writer.Write((float)value);
writer.Write((float)value!);
break;

case ResourceTypeCode.Double:
writer.Write((double)value);
writer.Write((double)value!);
break;

case ResourceTypeCode.Decimal:
writer.Write((decimal)value);
writer.Write((decimal)value!);
break;

case ResourceTypeCode.DateTime:
// Use DateTime's ToBinary & FromBinary.
long data = ((DateTime)value).ToBinary();
long data = ((DateTime)value!).ToBinary();
writer.Write(data);
break;

case ResourceTypeCode.TimeSpan:
writer.Write(((TimeSpan)value).Ticks);
writer.Write(((TimeSpan)value!).Ticks);
break;

// Special Types
case ResourceTypeCode.ByteArray:
{
byte[] bytes = (byte[])value;
byte[] bytes = (byte[])value!;
writer.Write(bytes.Length);
writer.Write(bytes, 0, bytes.Length);
break;
}

case ResourceTypeCode.Stream:
{
StreamWrapper sw = (StreamWrapper)value;
StreamWrapper sw = (StreamWrapper)value!;
if (sw.Stream.GetType() == typeof(MemoryStream))
{
MemoryStream ms = (MemoryStream)sw.Stream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ namespace System.Resources
{
public partial interface IResourceWriter : System.IDisposable
{
void AddResource(string name, byte[] value);
void AddResource(string name, object value);
void AddResource(string name, string value);
void AddResource(string name, byte[]? value);
void AddResource(string name, object? value);
void AddResource(string name, string? value);
void Close();
void Generate();
}
public sealed partial class ResourceWriter : System.IDisposable, System.Resources.IResourceWriter
{
public ResourceWriter(System.IO.Stream stream) { }
public ResourceWriter(string fileName) { }
public System.Func<System.Type, string> TypeNameConverter { get { throw null; } set { } }
public void AddResource(string name, byte[] value) { }
public void AddResource(string name, System.IO.Stream value) { }
public void AddResource(string name, System.IO.Stream value, bool closeAfterWrite) { }
public void AddResource(string name, object value) { }
public void AddResource(string name, string value) { }
public System.Func<System.Type, string>? TypeNameConverter { get { throw null; } set { } }
public void AddResource(string name, byte[]? value) { }
public void AddResource(string name, System.IO.Stream? value) { }
public void AddResource(string name, System.IO.Stream? value, bool closeAfterWrite) { }
public void AddResource(string name, object? value) { }
public void AddResource(string name, string? value) { }
public void AddResourceData(string name, string typeName, byte[] serializedData) { }
public void Close() { }
public void Dispose() { }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Nullable>enable</Nullable>
<Configurations>netcoreapp-Debug;netcoreapp-Release</Configurations>
</PropertyGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<AssemblyName>System.Resources.Writer</AssemblyName>
<RootNamespace>System.Resources</RootNamespace>
<Nullable>enable</Nullable>
<Configurations>netcoreapp-Debug;netcoreapp-Release</Configurations>
</PropertyGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace System.Resources
{
public interface IResourceWriter : IDisposable
{
void AddResource(string name, string value);
void AddResource(string name, object value);
void AddResource(string name, byte[] value);
void AddResource(string name, string? value);
void AddResource(string name, object? value);
void AddResource(string name, byte[]? value);
void Close();
void Generate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public partial class ResourceWriter
{
// Set this delegate to allow multi-targeting for .resources files.
// not used by .NETCore since ResourceWriter doesn't support BinaryFormatted resources.
public Func<Type, string> TypeNameConverter { get; set; }
public Func<Type, string>? TypeNameConverter { get; set; }

// Adds a resource of type Stream to the list of resources to be
// written to a file. They aren't written until Generate() is called.
// Doesn't close the Stream when done.
public void AddResource(string name, Stream value)
public void AddResource(string name, Stream? value)
{
if (name == null)
throw new ArgumentNullException(nameof(name));
Expand Down Expand Up @@ -51,7 +51,7 @@ public void AddResourceData(string name, string typeName, byte[] serializedData)

private void WriteData(BinaryWriter writer, object dataContext)
{
byte[] data = dataContext as byte[];
byte[]? data = dataContext as byte[];

Debug.Assert(data != null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ internal sealed class FastResourceComparer : IComparer<string>, IEqualityCompare
{
internal static readonly FastResourceComparer Default = new FastResourceComparer();


public int GetHashCode(string key)
{
return FastResourceComparer.HashFunction(key);
Expand Down

0 comments on commit dd7d333

Please sign in to comment.