-
Notifications
You must be signed in to change notification settings - Fork 400
Value handling cleanup #2486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KathleenDollard
wants to merge
10
commits into
dotnet:main-powderhouse
Choose a base branch
from
KathleenDollard:m-value-handling-cleanup
base: main-powderhouse
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Value handling cleanup #2486
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
74b9cf9
Implemented CalculatedValue
KathleenDollard ac61918
Updated ValueProvider for correct TryGet handling
KathleenDollard 5ee18e3
Added reentrancy test
KathleenDollard a13bf78
Added `dotnet nuget why` test and fixed
KathleenDollard 7265345
Added support for multiple symbols and Point example
KathleenDollard 8b36b4b
General cleanup
KathleenDollard 81d2ca8
Renaming: All value sources but constants include calculations
KathleenDollard 17be3a2
Better message for CollectionValueSource parameters
KathleenDollard 9edf1cc
Remove CalculateVaue and OtherValueSymbol support
KathleenDollard 7ac56d1
Updates
KathleenDollard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,16 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.CommandLine.Subsystems.Annotations; | ||
using System.CommandLine.ValueSources; | ||
using static System.Net.Mime.MediaTypeNames; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MediaTypeNames doesn't seem relevant here. |
||
|
||
namespace System.CommandLine; | ||
|
||
internal class ValueProvider | ||
{ | ||
private struct CacheItem | ||
{ | ||
internal object? Value; | ||
internal bool WasFound; | ||
internal bool IsCalculating; | ||
} | ||
private record struct CacheItem(object? Value, bool WasFound, bool IsCalculating) | ||
{ } | ||
|
||
private Dictionary<CliSymbol, CacheItem> cachedValues = []; | ||
private PipelineResult pipelineResult; | ||
|
@@ -22,17 +20,6 @@ public ValueProvider(PipelineResult pipelineResult) | |
this.pipelineResult = pipelineResult; | ||
} | ||
|
||
private void SetCachedValue(CliSymbol symbol, object? value, bool found) | ||
{ | ||
// TODO: MHutch: Feel free to optimize this and SetIsCalculating. We need the struct or a tuple here for "WasFound" which turns out we need. | ||
cachedValues[symbol] = new CacheItem() | ||
{ | ||
Value = value, | ||
WasFound = found, | ||
IsCalculating = false | ||
}; | ||
} | ||
|
||
private void SetIsCalculating(CliSymbol symbol) | ||
KathleenDollard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
cachedValues[symbol] = new CacheItem() | ||
|
@@ -79,7 +66,7 @@ private bool TryGetValueInternal<T>(CliValueSymbol valueSymbol, out T? value) | |
value = valueResult.GetValue<T>(); | ||
return CacheAndReturn(valueSymbol, value, true); | ||
} | ||
if (valueSymbol.TryGetDefault(out ValueSource? defaultValueSource)) | ||
if (valueSymbol.TryGetAnnotation(ValueAnnotations.DefaultValue, out var defaultValueSource)) | ||
{ | ||
if (defaultValueSource is not ValueSource<T> typedDefaultValueSource) | ||
{ | ||
|
@@ -90,13 +77,12 @@ private bool TryGetValueInternal<T>(CliValueSymbol valueSymbol, out T? value) | |
return CacheAndReturn(valueSymbol, value, true); | ||
} | ||
} | ||
// TODO: Determine if we should cache default. If so, additional design is needed to avoid first hit returning false, and remainder returning true | ||
value = default; | ||
return CacheAndReturn(valueSymbol, value, false); | ||
|
||
bool CacheAndReturn(CliValueSymbol valueSymbol, T? valueToCache, bool valueFound) | ||
{ | ||
SetCachedValue(valueSymbol, valueToCache, valueFound); | ||
cachedValues[valueSymbol] = new CacheItem(valueToCache, valueFound, false); | ||
return valueFound; | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,11 @@ | |
|
||
namespace System.CommandLine.ValueSources; | ||
|
||
// TODO: Consider creating a set of classes with different arity: T1 and T1, T2 and T1, T2, T3, etc. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be easier to parse if written with angle brackets // TODO: Consider creating a set of classes with different arity: <T1> and <T1, T2> and <T1, T2, T3>, etc. |
||
|
||
/// <summary> | ||
/// <see cref="ValueSource"/> that returns the value of the specified other symbol. | ||
/// If the calculation delegate is supplied, the returned value of the calculation is returned. | ||
/// <see cref="ValueSource"/> that returns the value value calculated from a set of other symbols. | ||
/// The calculation must be supplied. the returned value of the calculation is returned. | ||
/// </summary> | ||
/// <typeparam name="T">The type to be returned, which is almost always the type of the symbol the ValueSource will be used for.</typeparam> | ||
/// <param name="otherSymbols">The <see cref="CliOption">, <see cref="CliArgument"/>, or <see cref="CalculatedValue"/> to include as sources.</param> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's just harder to understand.