Skip to content

Commit ad3164f

Browse files
committed
Fix property binding casts
1 parent 9b0f2dc commit ad3164f

7 files changed

Lines changed: 46 additions & 29 deletions

File tree

‎src/lib/Vertical/Cli/Binding/BindingContext.cs‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Vertical.Cli.Conversion;
55
using Vertical.Cli.Internal;
66
using Vertical.Cli.Invocation;
7-
using Vertical.Cli.IO;
87
using Vertical.Cli.Parsing;
98

109
namespace Vertical.Cli.Binding;
@@ -13,7 +12,7 @@ namespace Vertical.Cli.Binding;
1312
/// Represents a context used to provide values for model activation.
1413
/// </summary>
1514
/// <typeparam name="TModel">Model type</typeparam>
16-
public sealed class BindingContext<TModel> where TModel : class
15+
public sealed class BindingContext<TModel> : IBindingContext where TModel : class
1716
{
1817
internal BindingContext(
1918
IRootConfiguration rootConfiguration,
@@ -69,7 +68,7 @@ public TValue GetValue<TValue>(
6968
ValueConverter<TValue>? valueConverter)
7069
{
7170
var binding = GetPropertyBinding(propertyExpression);
72-
var bindingArgs = new PropertyBinder<TModel, TValue>(binding, this, valueConverter);
71+
var bindingArgs = new PropertyBinder<TValue>(binding, this, valueConverter);
7372
binding.TryBindValue(bindingArgs);
7473

7574
if (bindingArgs.TryGetExplicitValue(out var value))
@@ -97,7 +96,7 @@ public TCollection GetCollectionValue<TElement, TCollection>(
9796
where TCollection : IEnumerable<TElement>
9897
{
9998
var binding = GetPropertyBinding(propertyExpression);
100-
var bindingArgs = new PropertyBinder<TModel, TCollection>(binding, this, null);
99+
var bindingArgs = new PropertyBinder<TCollection>(binding, this, null);
101100

102101
binding.TryBindValue(bindingArgs);
103102

@@ -121,7 +120,7 @@ public TModel ActivateInstance()
121120
}
122121

123122
private bool TryGetSingleParseResultValue<TValue>(
124-
IPropertyBinding<TModel, TValue> binding,
123+
IPropertyBinding<TValue> binding,
125124
ValueConverter<TValue>? valueConverter,
126125
out TValue value)
127126
{
@@ -136,7 +135,7 @@ private bool TryGetSingleParseResultValue<TValue>(
136135
}
137136

138137
private bool TryGetCollectionParseResultValue<TCollection, TElement>(
139-
IPropertyBinding<TModel, TCollection> binding,
138+
IPropertyBinding<TCollection> binding,
140139
ValueConverter<TElement>? elementConverter,
141140
Func<IEnumerable<TElement>, TCollection> createCollection,
142141
[NotNullWhen(true)] out TCollection? collection)
@@ -185,13 +184,13 @@ private bool TryConvertValue<TValue>(
185184
}
186185
}
187186

188-
private IPropertyBinding<TModel, TValue> GetPropertyBinding<TValue>(Expression<Func<TModel, TValue>> propertyExpression)
187+
private IPropertyBinding<TValue> GetPropertyBinding<TValue>(Expression<Func<TModel, TValue>> propertyExpression)
189188
{
190189
var bindingName = propertyExpression.GetPropertyName();
191190

192191
return PropertyBindings.GetValueOrDefault(bindingName) switch
193192
{
194-
IPropertyBinding<TModel, TValue> typedBinding => typedBinding,
193+
IPropertyBinding<TValue> typedBinding => typedBinding,
195194
{ } binding => throw Exceptions.InvalidBindingCast(typeof(TModel), typeof(TValue), binding),
196195
_ => throw Exceptions.InvalidBindingName(typeof(TModel), typeof(TValue), bindingName)
197196
};

‎src/lib/Vertical/Cli/Binding/FunctionalValueBinding.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ namespace Vertical.Cli.Binding;
55
/// </summary>
66
/// <typeparam name="TValue">Value type</typeparam>
77
/// <typeparam name="TModel">Model type</typeparam>
8-
public sealed class FunctionalValueBinding<TModel, TValue> : IPropertyBinding<TModel, TValue>
8+
public sealed class FunctionalValueBinding<TModel, TValue> : IPropertyBinding<TValue>
99
where TModel : class
1010
{
1111
internal FunctionalValueBinding(Type modelType,
1212
string bindingName,
13-
Action<PropertyBinder<TModel, TValue>> configureBinding)
13+
Action<PropertyBinder<TValue>> configureBinding)
1414
{
1515
_configureBinding = configureBinding;
1616

1717
ModelType = modelType;
1818
BindingName = bindingName;
1919
}
2020

21-
private readonly Action<PropertyBinder<TModel, TValue>> _configureBinding;
21+
private readonly Action<PropertyBinder<TValue>> _configureBinding;
2222

2323
/// <inheritdoc />
24-
public bool TryBindValue(PropertyBinder<TModel, TValue> binder)
24+
public bool TryBindValue(PropertyBinder<TValue> binder)
2525
{
2626
_configureBinding(binder);
2727
return binder.IsValueSet;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Vertical.Cli.Parsing;
2+
3+
namespace Vertical.Cli.Binding;
4+
5+
/// <summary>
6+
/// Represents a binding context
7+
/// </summary>
8+
public interface IBindingContext
9+
{
10+
/// <summary>
11+
/// Gets the input stream.
12+
/// </summary>
13+
TextReader InputStream { get; }
14+
15+
/// <summary>
16+
/// Gets the parse result.
17+
/// </summary>
18+
ParseResult ParseResult { get; }
19+
}

‎src/lib/Vertical/Cli/Binding/IPropertyBinding.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ public interface IPropertyBinding
2929
/// <summary>
3030
/// Represents the interface of an object that binds a property value in an option model.
3131
/// </summary>
32-
public interface IPropertyBinding<TModel, TValue> : IPropertyBinding where TModel : class
32+
public interface IPropertyBinding<TValue> : IPropertyBinding
3333
{
3434
/// <summary>
3535
/// Tries to bind a value using the given arguments.
3636
/// </summary>
3737
/// <param name="binder">The object that contains binding data.</param>
3838
/// <returns><c>true</c> if a value was set.</returns>
39-
bool TryBindValue(PropertyBinder<TModel, TValue> binder);
39+
bool TryBindValue(PropertyBinder<TValue> binder);
4040
}

‎src/lib/Vertical/Cli/Binding/PropertyBinder.cs‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ namespace Vertical.Cli.Binding;
77
/// Represents data used to realize a model's property value.
88
/// </summary>
99
/// <typeparam name="TValue">Value type</typeparam>
10-
/// <typeparam name="TModel">Model type</typeparam>
11-
public sealed class PropertyBinder<TModel, TValue> where TModel : class
10+
public sealed class PropertyBinder<TValue>
1211
{
1312
internal PropertyBinder(
14-
IPropertyBinding<TModel, TValue> propertyBinding,
15-
BindingContext<TModel> bindingContext,
13+
IPropertyBinding<TValue> propertyBinding,
14+
IBindingContext bindingContext,
1615
ValueConverter<TValue>? valueConverter)
1716
{
1817
PropertyBinding = propertyBinding;
@@ -31,12 +30,12 @@ private enum ValueSource
3130
/// <summary>
3231
/// Gets the property binding.
3332
/// </summary>
34-
public IPropertyBinding<TModel, TValue> PropertyBinding { get; }
33+
public IPropertyBinding PropertyBinding { get; }
3534

3635
/// <summary>
3736
/// Gets the binding context.
3837
/// </summary>
39-
public BindingContext<TModel> BindingContext { get; }
38+
public IBindingContext BindingContext { get; }
4039

4140
/// <summary>
4241
/// Gets whether a result has been set.

‎src/lib/Vertical/Cli/Binding/SymbolBinding.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Vertical.Cli.Binding;
99
/// </summary>
1010
/// <typeparam name="TValue">Value type</typeparam>
1111
/// <typeparam name="TModel">Model type</typeparam>
12-
public sealed class SymbolBinding<TModel, TValue> : IPropertyBinding<TModel, TValue>, ISymbolBinding
12+
public sealed class SymbolBinding<TModel, TValue> : IPropertyBinding<TValue>, ISymbolBinding
1313
where TModel : class
1414
{
1515
internal SymbolBinding(
@@ -20,7 +20,7 @@ internal SymbolBinding(
2020
string[] aliases,
2121
Arity arity,
2222
SymbolHelpTag? helpTag,
23-
Action<PropertyBinder<TModel, TValue>>? setBindingOptions)
23+
Action<PropertyBinder<TValue>>? setBindingOptions)
2424
{
2525
BindingName = bindingName;
2626
Precedence = precedence;
@@ -62,7 +62,7 @@ internal SymbolBinding(
6262
/// <summary>
6363
/// Gets an action that binds the symbol value.
6464
/// </summary>
65-
public Action<PropertyBinder<TModel, TValue>>? BindingOptionsAction { get; }
65+
public Action<PropertyBinder<TValue>>? BindingOptionsAction { get; }
6666

6767
/// <summary>
6868
/// Gets the aliases the symbol is known by.
@@ -75,7 +75,7 @@ internal SymbolBinding(
7575
public bool HasBindingOptions => BindingOptionsAction != null;
7676

7777
/// <inheritdoc />
78-
public bool TryBindValue(PropertyBinder<TModel, TValue> binder)
78+
public bool TryBindValue(PropertyBinder<TValue> binder)
7979
{
8080
BindingOptionsAction?.Invoke(binder);
8181
return BindingOptionsAction != null;

‎src/lib/Vertical/Cli/Configuration/ModelConfiguration.cs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public ModelConfiguration<TModel> AddArgument<TValue>(
9191
BasicArity? arity = null,
9292
SymbolHelpTag? helpTag = null,
9393
Action<ValidationContext<TModel, TValue>>? configureValidation = null,
94-
Action<PropertyBinder<TModel, TValue>>? setBindingOptions = null)
94+
Action<PropertyBinder<TValue>>? setBindingOptions = null)
9595
{
9696
ArgumentNullException.ThrowIfNull(propertyExpression);
9797
if (name != null && TokenSyntax.Parse(name) is not { Kind: SyntaxKind.NonDecorated })
@@ -143,7 +143,7 @@ public ModelConfiguration<TModel> AddCollectionArgument<TCollection>(
143143
CollectionArity? arity = null,
144144
SymbolHelpTag? helpTag = null,
145145
Action<ValidationContext<TModel, TCollection>>? configureValidation = null,
146-
Action<PropertyBinder<TModel, TCollection>>? setBindingOptions = null)
146+
Action<PropertyBinder<TCollection>>? setBindingOptions = null)
147147
{
148148
ArgumentNullException.ThrowIfNull(propertyExpression);
149149
if (name != null && TokenSyntax.Parse(name) is not { Kind: SyntaxKind.NonDecorated })
@@ -190,7 +190,7 @@ public ModelConfiguration<TModel> AddOption<TValue>(
190190
BasicArity? arity = null,
191191
SymbolHelpTag? helpTag = null,
192192
Action<ValidationContext<TModel, TValue>>? configureValidation = null,
193-
Action<PropertyBinder<TModel, TValue>>? setBindingOptions = null)
193+
Action<PropertyBinder<TValue>>? setBindingOptions = null)
194194
{
195195
ArgumentNullException.ThrowIfNull(propertyExpression);
196196
ValidateAliases(aliases);
@@ -234,7 +234,7 @@ public ModelConfiguration<TModel> AddCollectionOption<TCollection>(
234234
CollectionArity? arity = null,
235235
SymbolHelpTag? helpTag = null,
236236
Action<ValidationContext<TModel, TCollection>>? configureValidation = null,
237-
Action<PropertyBinder<TModel, TCollection>>? setBindingOptions = null)
237+
Action<PropertyBinder<TCollection>>? setBindingOptions = null)
238238
{
239239
ArgumentNullException.ThrowIfNull(propertyExpression);
240240
ValidateAliases(aliases);
@@ -309,7 +309,7 @@ public ModelConfiguration<TModel> AddSwitch(
309309
/// <returns>A reference to this instance</returns>
310310
public ModelConfiguration<TModel> BindParseResultValue<TValue>(
311311
Expression<Func<TModel, TValue>> propertyExpression,
312-
Action<PropertyBinder<TModel, TValue>> mapResult)
312+
Action<PropertyBinder<TValue>> mapResult)
313313
{
314314
ArgumentNullException.ThrowIfNull(propertyExpression);
315315
ArgumentNullException.ThrowIfNull(mapResult);

0 commit comments

Comments
 (0)