This commit is contained in:
Tyrrrz
2024-06-15 21:19:44 +03:00
parent 24fd87b1e1
commit 2323a57c39
4 changed files with 22 additions and 26 deletions

View File

@@ -9,20 +9,9 @@ namespace CliFx.Schema;
public interface IInputSchema
{
/// <summary>
/// Information about the property that this input is bound to.
/// Describes the binding of this input to a CLR property.
/// </summary>
PropertyDescriptor Property { get; }
/// <summary>
/// Whether this input is a scalar (single value) or a sequence (multiple values).
/// </summary>
bool IsScalar { get; }
/// <summary>
/// Valid values for this input, if applicable.
/// If the input does not have a predefined set of valid values, this property is <c>null</c>.
/// </summary>
IReadOnlyList<object?>? ValidValues { get; }
PropertyBinding Property { get; }
/// <summary>
/// Optional binding converter for this input.

View File

@@ -9,7 +9,7 @@ namespace CliFx.Schema;
/// Describes a command's option.
/// </summary>
public class OptionSchema(
PropertyDescriptor property,
PropertyBinding property,
bool isScalar,
IReadOnlyList<object?>? validValues,
string? name,
@@ -22,7 +22,7 @@ public class OptionSchema(
) : IInputSchema
{
/// <inheritdoc />
public PropertyDescriptor Property { get; } = property;
public PropertyBinding Property { get; } = property;
/// <inheritdoc />
public bool IsScalar { get; } = isScalar;

View File

@@ -7,7 +7,7 @@ namespace CliFx.Schema;
/// Describes a command's parameter.
/// </summary>
public class ParameterSchema(
PropertyDescriptor property,
PropertyBinding property,
bool isScalar,
IReadOnlyList<object?>? validValues,
int order,
@@ -19,7 +19,7 @@ public class ParameterSchema(
) : IInputSchema
{
/// <inheritdoc />
public PropertyDescriptor Property { get; } = property;
public PropertyBinding Property { get; } = property;
/// <inheritdoc />
public bool IsScalar { get; } = isScalar;

View File

@@ -1,24 +1,23 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
using System.Linq;
namespace CliFx.Schema;
/// <summary>
/// Describes a CLR property.
/// </summary>
public class PropertyDescriptor(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
Type type,
public class PropertyBinding(
Type propertyType,
Func<object, object?> getValue,
Action<object, object?> setValue
)
)
{
/// <summary>
/// Property's CLR type.
/// Underlying property type.
/// </summary>
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
public Type Type { get; } = type;
public Type PropertyType { get; } = propertyType;
/// <summary>
/// Gets the current value of the property on the specified instance.
/// </summary>
@@ -29,3 +28,11 @@ public class PropertyDescriptor(
/// </summary>
public void SetValue(object instance, object? value) => setValue(instance, value);
}
internal static class PropertyBindingExtensions
{
public static IReadOnlyList<object?>? TryGetValidValues(this PropertyBinding binding) =>
binding.PropertyType.IsEnum
? binding.PropertyType.GetEnumValuesAsUnderlyingType().Cast<object?>().ToArray()
: null;
}