Skip to content

Commit f0ceea6

Browse files
committed
remove null case from explicit binding implementation
1 parent d195a24 commit f0ceea6

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace System.CommandLine.Binding
5+
{
6+
internal class MissingValueSource : IValueSource
7+
{
8+
public bool TryGetValue(IValueDescriptor valueDescriptor, BindingContext? bindingContext, out object? boundValue)
9+
{
10+
boundValue = null;
11+
return false;
12+
}
13+
}
14+
}

src/System.CommandLine/Binding/ModelBinder.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,7 @@ private IReadOnlyList<BoundValue> GetValues(
202202
var valueSource = GetValueSource(bindingSources, bindingContext, valueDescriptor);
203203

204204
BoundValue? boundValue;
205-
if (valueSource is null)
206-
{
207-
// If there is no source to bind from, no value can be bound.
208-
boundValue = null;
209-
}
210-
else if (!bindingContext.TryBindToScalarValue(
205+
if (!bindingContext.TryBindToScalarValue(
211206
valueDescriptor,
212207
valueSource,
213208
out boundValue) && valueDescriptor.HasDefaultValue)
@@ -240,7 +235,7 @@ private IReadOnlyList<BoundValue> GetValues(
240235
return values;
241236
}
242237

243-
private IValueSource? GetValueSource(
238+
private IValueSource GetValueSource(
244239
IDictionary<IValueDescriptor, IValueSource>? bindingSources,
245240
BindingContext bindingContext,
246241
IValueDescriptor valueDescriptor)
@@ -263,7 +258,7 @@ private IReadOnlyList<BoundValue> GetValues(
263258
return new ParseResultMatchingValueSource();
264259
}
265260

266-
return null;
261+
return new MissingValueSource();
267262
}
268263

269264
public override string ToString() =>

0 commit comments

Comments
 (0)