Improve PathString <-> string logic

This commit is contained in:
Hao Kung 2017-06-20 10:49:17 -07:00
parent 835fb60380
commit 12f89f66a6
2 changed files with 37 additions and 11 deletions

View File

@ -443,25 +443,35 @@ namespace Microsoft.AspNetCore.Http
/// </summary> /// </summary>
/// <param name="s"></param> /// <param name="s"></param>
public static implicit operator PathString(string s) public static implicit operator PathString(string s)
{ => ConvertFromString(s);
return new PathString(s);
}
/// <summary> /// <summary>
/// Implicitly calls ToString(). /// Implicitly calls ToString().
/// </summary> /// </summary>
/// <param name="path"></param> /// <param name="path"></param>
public static implicit operator string(PathString path) public static implicit operator string(PathString path)
{ => path.ToString();
return path.ToString();
} internal static PathString ConvertFromString(string s)
=> string.IsNullOrEmpty(s) ? new PathString(s) : FromUriComponent(s);
} }
internal class PathStringConverter : TypeConverter internal class PathStringConverter : TypeConverter
{ {
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
=> sourceType == typeof(string)
? true
: base.CanConvertFrom(context, sourceType);
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{ => value is string
return new PathString((string)value); ? PathString.ConvertFromString((string)value)
} : base.ConvertFrom(context, culture, value);
public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, object value, Type destinationType)
=> destinationType == typeof(string)
? value.ToString()
: base.ConvertTo(context, culture, value, destinationType);
} }
} }

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
@ -208,11 +208,27 @@ namespace Microsoft.AspNetCore.Http
} }
[Fact] [Fact]
public void PathStringConvertsFromString() public void PathStringConvertsOnlyToAndFromString()
{ {
var converter = TypeDescriptor.GetConverter(typeof(PathString)); var converter = TypeDescriptor.GetConverter(typeof(PathString));
PathString result = (PathString)converter.ConvertFromInvariantString("/foo"); PathString result = (PathString)converter.ConvertFromInvariantString("/foo");
Assert.Equal("/foo", result.ToString()); Assert.Equal("/foo", result.ToString());
Assert.Equal("/foo", converter.ConvertTo(result, typeof(string)));
Assert.True(converter.CanConvertFrom(typeof(string)));
Assert.False(converter.CanConvertFrom(typeof(int)));
Assert.False(converter.CanConvertFrom(typeof(bool)));
Assert.True(converter.CanConvertTo(typeof(string)));
Assert.False(converter.CanConvertTo(typeof(int)));
Assert.False(converter.CanConvertTo(typeof(bool)));
}
[Fact]
public void PathStringStaysEqualAfterAssignments()
{
PathString p1 = "/?";
string s1 = p1;
PathString p2 = s1;
Assert.Equal(p1, p2);
} }
} }
} }