mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Refactor
This commit is contained in:
@@ -21,7 +21,7 @@ namespace CliFx.Demo.Domain
|
||||
|
||||
var data = File.ReadAllText(StorageFilePath);
|
||||
|
||||
return JsonConvert.DeserializeObject<Library>(data);
|
||||
return JsonConvert.DeserializeObject<Library>(data) ?? Library.Empty;
|
||||
}
|
||||
|
||||
public Book? TryGetBook(string title) => GetLibrary().Books.FirstOrDefault(b => b.Title == title);
|
||||
|
||||
@@ -590,10 +590,10 @@ public enum CustomEnum { One, Two, Three }
|
||||
public class Command : ICommand
|
||||
{
|
||||
[CommandParameter(0)]
|
||||
public List<CustomEnum> Foo { get; set; }
|
||||
public IReadOnlyList<CustomEnum> Foo { get; set; }
|
||||
|
||||
[CommandOption(""bar"")]
|
||||
public List<CustomEnum> Bar { get; set; }
|
||||
public IReadOnlyList<CustomEnum> Bar { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
@@ -638,7 +638,7 @@ public class Command : ICommand
|
||||
public CustomEnum? Foo { get; set; }
|
||||
|
||||
[CommandOption(""bar"")]
|
||||
public List<CustomEnum?> Bar { get; set; }
|
||||
public IReadOnlyList<CustomEnum?> Bar { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
@@ -21,8 +21,20 @@ namespace CliFx.Schema
|
||||
|
||||
public IReadOnlyList<object?> GetValidValues()
|
||||
{
|
||||
var underlyingType = Type.TryGetEnumerableUnderlyingType() ?? Type;
|
||||
underlyingType = underlyingType.TryGetNullableUnderlyingType() ?? underlyingType;
|
||||
static Type GetUnderlyingType(Type type)
|
||||
{
|
||||
var enumerableUnderlyingType = type.TryGetEnumerableUnderlyingType();
|
||||
if (enumerableUnderlyingType is not null)
|
||||
return GetUnderlyingType(enumerableUnderlyingType);
|
||||
|
||||
var nullableUnderlyingType = type.TryGetNullableUnderlyingType();
|
||||
if (nullableUnderlyingType is not null)
|
||||
return GetUnderlyingType(nullableUnderlyingType);
|
||||
|
||||
return type;
|
||||
}
|
||||
|
||||
var underlyingType = GetUnderlyingType(Type);
|
||||
|
||||
// We can only get valid values for enums
|
||||
if (underlyingType.IsEnum)
|
||||
|
||||
Reference in New Issue
Block a user