Skip to content

Commit

Permalink
(NuGet#9) Add TypeConverter for NuGetVersion class
Browse files Browse the repository at this point in the history
Within the Chocolatey codebase, there is some explicit handling done of
a serialized instance of the NuGetVersion class into XML. Without a
converter to handle this work, some aspects of the codebase fails to
work correctly.

This commit adds a new NuGetVersionConverter class which is very
similar to the existing SemanticVersionConverter class, and adds an
attribute to the NuGetVersion class so that upstream users know which
class to use when converting instances of this class.
  • Loading branch information
gep13 committed Feb 3, 2023
1 parent cf5b1e5 commit 961b243
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/NuGet.Core/NuGet.Versioning/NuGetVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@

using System;
using System.Collections.Generic;
//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
using System.ComponentModel;
//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

namespace NuGet.Versioning
{
Expand All @@ -11,6 +18,13 @@ namespace NuGet.Versioning
/// not strictly enforcing it to
/// allow older 4-digit versioning schemes to continue working.
/// </summary>
//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
[TypeConverter(typeof(NuGetVersionConverter))]
//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
public partial class NuGetVersion : SemanticVersion
{
private readonly string _originalString;
Expand Down
90 changes: 90 additions & 0 deletions src/NuGet.Core/NuGet.Versioning/NuGetVersionConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) 2022-Present Chocolatey Software, Inc.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

//////////////////////////////////////////////////////////
// Chocolatey Specific Modification
//////////////////////////////////////////////////////////

using System;
using System.ComponentModel;
using System.Globalization;

namespace NuGet.Versioning
{
/// <summary>
/// Provides a type converter to convert <see cref="NuGetVersion"/> objects to and from various other representations.
/// </summary>
public class NuGetVersionConverter : TypeConverter
{
/// <summary>
/// Gets a value indicating whether this converter can convert an object in the given source type to a
/// <see cref="NuGetVersion"/> using the specified context.
/// </summary>
/// <param name="context">An <see cref="ITypeDescriptorContext"/> that provides a format context.</param>
/// <param name="sourceType">A <see cref="Type"/> that represents the type you wish to convert from.</param>
/// <returns><c>true</c> if this object can perform the conversion; otherwise, <c>false</c>.</returns>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}

/// <summary>
/// Converts the given object to the type of this converter, using the specified context and culture information.
/// </summary>
/// <param name="context">An <see cref="ITypeDescriptorContext"/> that provides a format context.</param>
/// <param name="culture">The <see cref="CultureInfo"/> to use as the current culture.</param>
/// <param name="value">The <see cref="object"/> to convert.</param>
/// <returns>An <see cref="object" /> that represents the converted value.</returns>
/// <exception cref="ArgumentException"><c>value</c> is not a valid value for the target type.</exception>
/// <exception cref="NotSupportedException">The conversion cannot be performed.</exception>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is null)
{
return null;
}

if (value is string versionString)
{
return NuGetVersion.Parse(versionString);
}

return base.ConvertFrom(context, culture, value);
}

/// <summary>
/// Gets a value indicating whether this converter can convert an object to the given destination type using the context.
/// </summary>
/// <param name="context">An <see cref="ITypeDescriptorContext"/> that provides a format context.</param>
/// <param name="destinationType">A <see cref="Type"/> that represents the type you wish to convert to.</param>
/// <returns><c>true</c> if this object can perform the conversion; otherwise, <c>false</c>.</returns>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(string) || base.CanConvertTo(context, destinationType);
}

/// <summary>
/// Converts the given value object to a <see cref="NuGetVersion"/> object using the arguments.
/// </summary>
/// <param name="context">An <see cref="ITypeDescriptorContext"/> that provides a format context.</param>
/// <param name="culture">The <see cref="CultureInfo"/> to use as the current culture.</param>
/// <param name="value">The <see cref="object"/> to convert.</param>
/// <param name="destinationType">The <see cref="Type"/> to convert the value to.</param>
/// <returns>An <see cref="object" /> that represents the converted value.</returns>
/// <exception cref="NotSupportedException">The conversion cannot be performed.</exception>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is null)
{
return null;
}

if (destinationType == typeof(string) && value is NuGetVersion version)
{
return version.ToFullString();
}

return base.ConvertTo(context, culture, value, destinationType);
}
}
}
6 changes: 6 additions & 0 deletions src/NuGet.Core/NuGet.Versioning/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
NuGet.Versioning.NuGetVersionConverter
NuGet.Versioning.NuGetVersionConverter.NuGetVersionConverter() -> void
override NuGet.Versioning.NuGetVersionConverter.CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) -> bool
override NuGet.Versioning.NuGetVersionConverter.CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type destinationType) -> bool
override NuGet.Versioning.NuGetVersionConverter.ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) -> object
override NuGet.Versioning.NuGetVersionConverter.ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) -> object

0 comments on commit 961b243

Please sign in to comment.