Skip to content

Commit dcbd1c5

Browse files
authored
move IValueDescriptor to System.CommandLine.NamingConventionBinder (#2124)
1 parent d18f598 commit dcbd1c5

File tree

9 files changed

+27
-44
lines changed

9 files changed

+27
-44
lines changed

src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_NamingConventionBinder_api_is_not_changed.approved.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
public IValueDescriptor ValueDescriptor { get; }
1010
public IValueSource ValueSource { get; }
1111
public System.String ToString()
12+
public interface IValueDescriptor
13+
public System.Boolean HasDefaultValue { get; }
14+
public System.String ValueName { get; }
15+
public System.Type ValueType { get; }
16+
public System.Object GetDefaultValue()
1217
public interface IValueSource
1318
public System.Boolean TryGetValue(IValueDescriptor valueDescriptor, BindingContext bindingContext, ref System.Object& boundValue)
1419
System.CommandLine.NamingConventionBinder
@@ -104,12 +109,12 @@ System.CommandLine.NamingConventionBinder
104109
public System.Boolean EnforceExplicitBinding { get; set; }
105110
public ModelDescriptor ModelDescriptor { get; }
106111
public System.CommandLine.Binding.IValueDescriptor ValueDescriptor { get; }
107-
public System.Void BindMemberFromValue(System.Reflection.PropertyInfo property, System.CommandLine.Binding.IValueDescriptor valueDescriptor)
112+
public System.Void BindMemberFromValue(System.Reflection.PropertyInfo property, System.CommandLine.Symbol symbol)
108113
public System.Object CreateInstance(System.CommandLine.Binding.BindingContext bindingContext)
109114
public System.Void UpdateInstance<T>(T instance, System.CommandLine.Binding.BindingContext bindingContext)
110115
public class ModelBinder<TModel> : ModelBinder
111116
.ctor()
112-
public System.Void BindMemberFromValue<TValue>(Expression<Func<TModel,TValue>> property, System.CommandLine.Binding.IValueDescriptor valueDescriptor)
117+
public System.Void BindMemberFromValue<TValue>(Expression<Func<TModel,TValue>> property, System.CommandLine.Symbol symbol)
113118
public System.Void BindMemberFromValue<TValue>(Expression<Func<TModel,TValue>> property, Func<System.CommandLine.Binding.BindingContext,TValue> getValue)
114119
public class ModelBindingCommandHandler : BindingHandler
115120
public System.Void BindParameter(System.Reflection.ParameterInfo param, System.CommandLine.Argument argument)

src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
System.CommandLine
2-
public abstract class Argument : Symbol, System.CommandLine.Binding.IValueDescriptor
2+
public abstract class Argument : Symbol
33
public ArgumentArity Arity { get; set; }
44
public System.Collections.Generic.List<System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>>> CompletionSources { get; }
55
public System.Boolean HasDefaultValue { get; }
@@ -9,7 +9,7 @@ System.CommandLine
99
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.CommandLine.Completions.CompletionContext context)
1010
public System.Object GetDefaultValue()
1111
public System.String ToString()
12-
public class Argument<T> : Argument, System.CommandLine.Binding.IValueDescriptor
12+
public class Argument<T> : Argument
1313
.ctor(System.String name)
1414
public Func<System.CommandLine.Parsing.ArgumentResult,T> CustomParser { get; set; }
1515
public Func<System.CommandLine.Parsing.ArgumentResult,T> DefaultValueFactory { get; set; }
@@ -90,7 +90,7 @@ System.CommandLine
9090
public class EnvironmentVariablesDirective : Directive
9191
.ctor()
9292
public CliAction Action { get; set; }
93-
public abstract class Option : Symbol, System.CommandLine.Binding.IValueDescriptor
93+
public abstract class Option : Symbol
9494
public CliAction Action { get; set; }
9595
public System.Collections.Generic.ICollection<System.String> Aliases { get; }
9696
public System.Boolean AllowMultipleArgumentsPerToken { get; set; }
@@ -100,9 +100,8 @@ System.CommandLine
100100
public System.String HelpName { get; set; }
101101
public System.Boolean IsRequired { get; set; }
102102
public System.Collections.Generic.List<System.Action<System.CommandLine.Parsing.OptionResult>> Validators { get; }
103-
public System.Type ValueType { get; }
104103
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.CommandLine.Completions.CompletionContext context)
105-
public class Option<T> : Option, System.CommandLine.Binding.IValueDescriptor
104+
public class Option<T> : Option
106105
.ctor(System.String name, System.String[] aliases)
107106
public Func<System.CommandLine.Parsing.ArgumentResult,T> CustomParser { get; set; }
108107
public Func<System.CommandLine.Parsing.ArgumentResult,T> DefaultValueFactory { get; set; }
@@ -152,18 +151,12 @@ System.CommandLine
152151
public System.Collections.Generic.IEnumerable<Symbol> Parents { get; }
153152
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.CommandLine.Completions.CompletionContext context)
154153
public System.String ToString()
155-
public class VersionOption : Option<System.Boolean>, System.CommandLine.Binding.IValueDescriptor
154+
public class VersionOption : Option<System.Boolean>
156155
.ctor()
157156
.ctor(System.String name, System.String[] aliases)
158157
public CliAction Action { get; set; }
159158
public System.Boolean Equals(System.Object obj)
160159
public System.Int32 GetHashCode()
161-
System.CommandLine.Binding
162-
public interface IValueDescriptor
163-
public System.Boolean HasDefaultValue { get; }
164-
public System.String ValueName { get; }
165-
public System.Type ValueType { get; }
166-
public System.Object GetDefaultValue()
167160
System.CommandLine.Completions
168161
public abstract class CompletionContext
169162
public static CompletionContext Empty { get; }
@@ -222,7 +215,7 @@ System.CommandLine.Help
222215
public HelpBuilder HelpBuilder { get; }
223216
public System.IO.TextWriter Output { get; }
224217
public System.CommandLine.ParseResult ParseResult { get; }
225-
public class HelpOption : System.CommandLine.Option<System.Boolean>, System.CommandLine.Binding.IValueDescriptor
218+
public class HelpOption : System.CommandLine.Option<System.Boolean>
226219
.ctor()
227220
.ctor(System.String name, System.String[] aliases)
228221
public System.CommandLine.CliAction Action { get; set; }

src/System.CommandLine.DragonFruit.Tests/ConfigureFromMethodTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public void Options_are_not_built_for_infrastructure_types_exposed_by_method_par
209209
var options = handlerMethod.BuildOptions();
210210

211211
options.Should()
212-
.NotContain(o => o.ValueType == type);
212+
.NotContain(o => o.GetType().IsAssignableTo(typeof(Option<>).MakeGenericType(type)));
213213
}
214214

215215
[Fact]

src/System.CommandLine.NamingConventionBinder/ModelBinder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ internal ModelBinder(IValueDescriptor valueDescriptor)
5050
/// Sets a property using a value descriptor.
5151
/// </summary>
5252
/// <param name="property">The property to bind.</param>
53-
/// <param name="valueDescriptor">A descriptor of the value to be used to set the property.</param>
53+
/// <param name="symbol">A symbol to be used to set the property.</param>
5454
/// <exception cref="ArgumentException"></exception>
55-
public void BindMemberFromValue(PropertyInfo property, IValueDescriptor valueDescriptor)
55+
public void BindMemberFromValue(PropertyInfo property, Symbol symbol)
5656
{
5757
var propertyDescriptor = FindModelPropertyDescriptor(property.PropertyType, property.Name);
5858

@@ -62,7 +62,7 @@ public void BindMemberFromValue(PropertyInfo property, IValueDescriptor valueDes
6262
message: "Property is not described by any of the model property descriptors.");
6363
}
6464

65-
MemberBindingSources[propertyDescriptor] = new SpecificSymbolValueSource(valueDescriptor);
65+
MemberBindingSources[propertyDescriptor] = new SpecificSymbolValueSource(symbol);
6666
}
6767

6868
/// <summary>

src/System.CommandLine.NamingConventionBinder/ModelBinder{T}.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ public ModelBinder() : base(typeof(TModel))
1818
/// Configures a custom binding behavior for the specified property.
1919
/// </summary>
2020
/// <param name="property">An expression specifying the property to bind.</param>
21-
/// <param name="valueDescriptor">A value descriptor for the value from which the property will be bound.</param>
21+
/// <param name="symbol">Symbol for the value from which the property will be bound.</param>
2222
/// <typeparam name="TValue">The type of the value to be bound.</typeparam>
2323
public void BindMemberFromValue<TValue>(
2424
Expression<Func<TModel, TValue>> property,
25-
IValueDescriptor valueDescriptor)
25+
Symbol symbol)
2626
{
2727
var (propertyType, propertyName) = property.MemberTypeAndName();
2828
var propertyDescriptor = FindModelPropertyDescriptor(propertyType, propertyName);
2929

3030
if (propertyDescriptor is not null)
3131
{
32-
MemberBindingSources[propertyDescriptor] = new SpecificSymbolValueSource(valueDescriptor);
32+
MemberBindingSources[propertyDescriptor] = new SpecificSymbolValueSource(symbol);
3333
}
3434
}
3535

src/System.CommandLine.NamingConventionBinder/SpecificSymbolValueSource.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@ namespace System.CommandLine.NamingConventionBinder;
77

88
internal class SpecificSymbolValueSource : IValueSource
99
{
10-
public SpecificSymbolValueSource(IValueDescriptor valueDescriptor)
10+
private readonly Symbol _symbol;
11+
12+
public SpecificSymbolValueSource(Symbol symbol)
1113
{
12-
ValueDescriptor = valueDescriptor ?? throw new ArgumentNullException(nameof(valueDescriptor));
14+
_symbol = symbol ?? throw new ArgumentNullException(nameof(symbol));
1315
}
1416

15-
public IValueDescriptor ValueDescriptor { get; }
16-
1717
public bool TryGetValue(IValueDescriptor valueDescriptor,
1818
BindingContext? bindingContext,
1919
out object? boundValue)
2020
{
21-
var specificDescriptor = ValueDescriptor;
22-
switch (specificDescriptor)
21+
switch (_symbol)
2322
{
2423
case Option option:
2524
var optionResult = bindingContext?.ParseResult.FindResultFor(option);

src/System.CommandLine/Argument.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace System.CommandLine
1212
/// <summary>
1313
/// A symbol defining a value that can be passed on the command line to a <see cref="Command">command</see> or <see cref="Option">option</see>.
1414
/// </summary>
15-
public abstract class Argument : Symbol, IValueDescriptor
15+
public abstract class Argument : Symbol
1616
{
1717
private ArgumentArity _arity;
1818
private TryConvertArgument? _convertArguments;
@@ -101,9 +101,6 @@ public override IEnumerable<CompletionItem> GetCompletions(CompletionContext con
101101
/// <inheritdoc />
102102
public override string ToString() => $"{nameof(Argument)}: {Name}";
103103

104-
/// <inheritdoc />
105-
string IValueDescriptor.ValueName => Name;
106-
107104
internal bool IsBoolean() => ValueType == typeof(bool) || ValueType == typeof(bool?);
108105
}
109106
}

src/System.CommandLine/Option.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace System.CommandLine
1212
/// <summary>
1313
/// A symbol defining a named parameter and a value for that parameter.
1414
/// </summary>
15-
public abstract class Option : Symbol, IValueDescriptor
15+
public abstract class Option : Symbol
1616
{
1717
internal AliasSet? _aliases;
1818
private List<Action<OptionResult>>? _validators;
@@ -110,17 +110,6 @@ internal virtual bool IsGreedy
110110
/// </summary>
111111
public virtual CliAction? Action { get; set; }
112112

113-
string IValueDescriptor.ValueName => Name;
114-
115-
/// <summary>
116-
/// The <see cref="System.Type"/> that the option's arguments are expected to be parsed as.
117-
/// </summary>
118-
public Type ValueType => Argument.ValueType;
119-
120-
bool IValueDescriptor.HasDefaultValue => Argument.HasDefaultValue;
121-
122-
object? IValueDescriptor.GetDefaultValue() => Argument.GetDefaultValue();
123-
124113
/// <inheritdoc />
125114
public override IEnumerable<CompletionItem> GetCompletions(CompletionContext context)
126115
{

0 commit comments

Comments
 (0)