forked from NuGet/NuGet.Client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(NuGet#9) Add TypeConverter for NuGetVersion class
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
Showing
3 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |