Skip to content

Commit

Permalink
Enable nullable reference types for simple values (#840)
Browse files Browse the repository at this point in the history
  • Loading branch information
twsouthwick authored Jan 9, 2021
1 parent f6206d7 commit f81ee1d
Show file tree
Hide file tree
Showing 33 changed files with 637 additions and 804 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private void Validate(StringValue str, ValidationContext context, in ValidationE
errorType: ValidationErrorType.Schema);
}
}
else if (current.Value.InnerText.Length > 255)
else if (current.Value.InnerText != null && current.Value.InnerText.Length > 255)
{
context.CreateError(
id: id,
Expand Down
8 changes: 4 additions & 4 deletions src/DocumentFormat.OpenXml/Resources/SR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ namespace DocumentFormat.OpenXml
{
internal static class SR
{
public static string Format(string str, params object[] args)
public static string Format(string str, params object?[] args)
{
return string.Format(CultureInfo.CurrentUICulture, str, args);
}

public static string Format(string str, object arg0)
public static string Format(string str, object? arg0)
{
return string.Format(CultureInfo.CurrentUICulture, str, arg0);
}

public static string Format(string str, object arg0, object arg1)
public static string Format(string str, object? arg0, object? arg1)
{
return string.Format(CultureInfo.CurrentUICulture, str, arg0, arg1);
}

public static string Format(string str, object arg0, object arg1, object arg2)
public static string Format(string str, object? arg0, object? arg1, object? arg2)
{
return string.Format(CultureInfo.CurrentUICulture, str, arg0, arg1, arg2);
}
Expand Down
54 changes: 26 additions & 28 deletions src/DocumentFormat.OpenXml/SimpleTypes/Base64BinaryValue.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#nullable disable

using System;
using System.Diagnostics;

Expand All @@ -15,30 +13,30 @@ namespace DocumentFormat.OpenXml
public class Base64BinaryValue : StringValue
{
/// <summary>
/// Initializes a new instance of the Base64BinaryValue class.
/// Initializes a new instance of the <see cref="Base64BinaryValue"/> class.
/// </summary>
public Base64BinaryValue()
{
}

/// <summary>
/// Initializes a new instance of the Base64BinaryValue class by deep copying
/// the supplied String value.
/// Initializes a new instance of the <see cref="Base64BinaryValue"/> class by deep copying
/// the supplied <see cref="string"/> value.
/// </summary>
/// <param name="base64Binary">
/// The String value.
/// The <see cref="string"/> value.
/// </param>
public Base64BinaryValue(string base64Binary)
{
TextValue = base64Binary;
}

/// <summary>
/// Initializes a new instance of the Base64BinaryValue class by deep copying
/// the supplied Base64BinaryValue class.
/// Initializes a new instance of the <see cref="Base64BinaryValue"/> class by deep copying
/// the supplied <see cref="Base64BinaryValue"/> class.
/// </summary>
/// <param name="source">
/// The source Base64BinaryValue class.
/// The source <see cref="Base64BinaryValue"/> class.
/// </param>
public Base64BinaryValue(Base64BinaryValue source)
: base(source?.InnerText)
Expand All @@ -48,7 +46,7 @@ public Base64BinaryValue(Base64BinaryValue source)
/// <summary>
/// Gets or sets the Base64 binary string value.
/// </summary>
public override string Value
public override string? Value
{
get => TextValue;
set => TextValue = value;
Expand Down Expand Up @@ -93,50 +91,50 @@ internal override int Length
}

/// <summary>
/// Implicitly converts the specified value to a String value.
/// Implicitly converts the specified value to a <see cref="string"/> value.
/// </summary>
/// <param name="xmlAttribute">The Base64BinaryValue object to convert.</param>
/// <returns>The base64Binary string. Returns null when xmlAttribute is null.</returns>
public static implicit operator string(Base64BinaryValue xmlAttribute)
/// <param name="value">The <see cref="Base64BinaryValue"/> object to convert.</param>
/// <returns>The base64Binary string. Returns null when <paramref name="value"/> is <c>null</c>.</returns>
public static implicit operator string?(Base64BinaryValue value)
{
if (xmlAttribute == null)
if (value is null)
{
return null;
}

return ToString(xmlAttribute);
return ToString(value);
}

/// <summary>
/// Initializes a new instance of a Base64BinaryValue class using the
/// Initializes a new instance of a <see cref="Base64BinaryValue"/> class using the
/// supplied string.
/// </summary>
/// <param name="value">The specified base64Binary value.</param>
/// <returns>A new Base64BinaryValue instance with the value.</returns>
/// <returns>A new <see cref="Base64BinaryValue"/> instance with the value.</returns>
public static implicit operator Base64BinaryValue(string value) => FromString(value);

/// <summary>
/// Returns a new Base64BinaryValue object that was created from a String value.
/// Returns a new <see cref="Base64BinaryValue"/> object that was created from a <see cref="string"/> value.
/// </summary>
/// <param name="value">A String value to use to create a new Base64BinaryValue object.</param>
/// <returns>A Base64BinaryValue that corresponds to the value parameter.</returns>
/// <param name="value">A <see cref="string"/> value to use to create a new <see cref="Base64BinaryValue"/> object.</param>
/// <returns>A <see cref="Base64BinaryValue"/> that corresponds to the value parameter.</returns>
public new static Base64BinaryValue FromString(string value) => new Base64BinaryValue(value);

/// <summary>
/// Returns the String value representation of a Base64BinaryValue object.
/// Returns the <see cref="string"/> value representation of a <see cref="Base64BinaryValue"/> object.
/// </summary>
/// <param name="xmlAttribute">
/// A Base64BinaryValue object used to retrieve a String value representation.
/// <param name="value">
/// A <see cref="Base64BinaryValue"/> object used to retrieve a <see cref="string"/> value representation.
/// </param>
/// <returns>A String value that represents a Base64BinaryValue object.</returns>
public static string ToString(Base64BinaryValue xmlAttribute)
/// <returns>A <see cref="string"/> value that represents a <see cref="Base64BinaryValue"/> object.</returns>
public static string? ToString(Base64BinaryValue value)
{
if (xmlAttribute == null)
if (value is null)
{
throw new InvalidOperationException(ExceptionMessages.ImplicitConversionExceptionOnNull);
}

return xmlAttribute.Value;
return value.Value;
}

private protected override OpenXmlSimpleType CloneImpl() => new Base64BinaryValue(this);
Expand Down
70 changes: 31 additions & 39 deletions src/DocumentFormat.OpenXml/SimpleTypes/BooleanValue.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#nullable disable

using System;
using System.Diagnostics;
using System.Xml;

namespace DocumentFormat.OpenXml
{
/// <summary>
/// Represents the Boolean value for attributes.
/// Represents the <see cref="bool"/> value for attributes.
/// </summary>
[DebuggerDisplay("{InnerText}")]
public class BooleanValue : OpenXmlComparableSimpleValue<bool>
{
/// <summary>
/// Initializes a new instance of the BooleanValue class.
/// Initializes a new instance of the <see cref="BooleanValue"/> class.
/// </summary>
public BooleanValue()
{
}

/// <summary>
/// Initializes a new instance of the BooleanValue class using the supplied Boolean value.
/// Initializes a new instance of the <see cref="BooleanValue"/> class using the supplied <see cref="bool"/> value.
/// </summary>
/// <param name="value">The Boolean value.</param>
/// <param name="value">The <see cref="bool"/> value.</param>
public BooleanValue(bool value)
: base(value)
{
}

/// <summary>
/// Initializes a new instance of the BooleanValue class by deep copying
/// the supplied BooleanValue class.
/// Initializes a new instance of the <see cref="BooleanValue"/> class by deep copying
/// the supplied <see cref="BooleanValue"/> class.
/// </summary>
/// <param name="source">
/// The source BooleanValue class.
/// The source <see cref="BooleanValue"/> class.
/// </param>
public BooleanValue(BooleanValue source)
: base(source)
Expand All @@ -45,60 +43,54 @@ public BooleanValue(BooleanValue source)

private protected override string GetText(bool input) => input ? "1" : "0";

private protected override bool Parse(string input) => XmlConvert.ToBoolean(input);
private protected override bool Parse(string? input) => XmlConvert.ToBoolean(input);

/// <summary>
/// Implicitly converts the specified value to a Boolean value.
/// Implicitly converts the specified value to a <see cref="bool"/> value.
/// </summary>
/// <param name="xmlAttribute">The BooleanValue to convert.</param>
/// <returns>The converted Boolean value.</returns>
/// <exception cref="InvalidOperationException">Thrown when xmlAttribute is null.</exception>
public static implicit operator bool(BooleanValue xmlAttribute)
/// <param name="value">The <see cref="BooleanValue"/> to convert.</param>
/// <returns>The converted <see cref="bool"/> value.</returns>
/// <exception cref="InvalidOperationException">Thrown when <paramref name="value"/> is null.</exception>
public static implicit operator bool(BooleanValue value)
{
if (xmlAttribute == null)
if (value is null)
{
throw new InvalidOperationException(ExceptionMessages.ImplicitConversionExceptionOnNull);
}

return ToBoolean(xmlAttribute);
return ToBoolean(value);
}

/// <summary>
/// Initializes a new instance of the BooleanValue class by implicitly
/// converting the supplied Boolean value.
/// Initializes a new instance of the <see cref="BooleanValue"/> class by implicitly
/// converting the supplied <see cref="bool"/> value.
/// </summary>
/// <param name="value">The Boolean value.</param>
/// <returns>A new BooleanValue instance with the value.</returns>
public static implicit operator BooleanValue(bool value)
{
return FromBoolean(value);
}
/// <param name="value">The <see cref="bool"/> value.</param>
/// <returns>A new <see cref="BooleanValue"/> instance with the value.</returns>
public static implicit operator BooleanValue(bool value) => FromBoolean(value);

/// <summary>
/// Returns a new BooleanValue object that was created by using the supplied Boolean value.
/// Returns a new <see cref="BooleanValue"/> object that was created by using the supplied <see cref="bool"/> value.
/// </summary>
/// <param name="value">A Boolean value to use to create a new BooleanValue object.</param>
/// <returns>A BooleanValue that corresponds to the value parameter.</returns>
public static BooleanValue FromBoolean(bool value)
{
return new BooleanValue(value);
}
/// <param name="value">A <see cref="bool"/> value to use to create a new <see cref="BooleanValue"/> object.</param>
/// <returns>A <see cref="BooleanValue"/> that corresponds to the value parameter.</returns>
public static BooleanValue FromBoolean(bool value) => new BooleanValue(value);

/// <summary>
/// Returns the Boolean representation of a BooleanValue object.
/// Returns the <see cref="bool"/> representation of a <see cref="BooleanValue"/> object.
/// </summary>
/// <param name="xmlAttribute">
/// A BooleanValue object to retrieve a Boolean representation.
/// <param name="value">
/// A <see cref="BooleanValue"/> object to retrieve a <see cref="bool"/> representation.
/// </param>
/// <returns>A Boolean value that represents a BooleanValue object.</returns>
public static bool ToBoolean(BooleanValue xmlAttribute)
/// <returns>A <see cref="bool"/> value that represents a <see cref="BooleanValue"/> object.</returns>
public static bool ToBoolean(BooleanValue value)
{
if (xmlAttribute == null)
if (value is null)
{
throw new InvalidOperationException(ExceptionMessages.ImplicitConversionExceptionOnNull);
}

return xmlAttribute.Value;
return value.Value;
}

private protected override OpenXmlSimpleType CloneImpl() => new BooleanValue(this);
Expand Down
Loading

0 comments on commit f81ee1d

Please sign in to comment.