-
-
Notifications
You must be signed in to change notification settings - Fork 532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to get empty value from nullable instance like TextPrompt<Uri?> #714
Comments
@danielklecha Could you create a minimal reproducable example for this? |
@patriksvensson Sure 😄 |
The "problem" is the
This makes I feel this is a bug. E.g. given the following really far-fetched example: using System.ComponentModel;
using System.Globalization;
using Spectre.Console;
var thing = await new TextPrompt<Thing>("thing")
.AllowEmpty()
.ShowAsync(AnsiConsole.Console, CancellationToken.None);
if (thing == null)
AnsiConsole.WriteLine("You selected a null value");
else
AnsiConsole.WriteLine($"You selected {thing.Content}");
[TypeConverter(typeof(ThingConverter))]
public class Thing
{
public Thing(string content)
{
Content = content;
}
public string Content { get; }
}
public class ThingConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
{
if (value is string strVal)
{
if (string.IsNullOrEmpty(strVal))
{
return null;
}
if (strVal.EndsWith("a"))
{
return null;
}
return new Thing(strVal);
}
return base.ConvertFrom(context, culture, value);
}
} The |
Could someone modify TextPrompt and update one line? It generates issues with all nullable types.
|
@danielklecha simply removing that We're certainly open to suggestions, though. /cc @iraklikhitarishvili - this answers your question in #998, probably. |
I want to get AbsoluteUri or null but it's not supported by TextPrompt even with AllowEmpty=true.
If I press enter then I get "Invalid input".
I suppose that input is string.Empty when we call TryConvertFromString.
UriConverter allow to convert from string.Empty and return null.
In that case condition in line 146 in TextPrompt should be modified and allow result to be null if conversion was successfull.
This issue could happen for other types if allow to convert from string.Empty and return null.
What do you think?
Please upvote 👍 this issue if you are interested in it.
The text was updated successfully, but these errors were encountered: