Skip to content
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

[Feature] Define short alias names for all keys supported in calculated-property definitions #8430

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,15 @@

namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Definitions for hash table keys.
/// </summary>
internal static class SortObjectParameterDefinitionKeys
{
internal const string AscendingEntryKey = "ascending";
internal const string DescendingEntryKey = "descending";
}

/// <summary>
/// </summary>
internal class SortObjectExpressionParameterDefinition : CommandParameterDefinition
{
protected override void SetEntries()
{
this.hashEntries.Add(new ExpressionEntryDefinition(false));
this.hashEntries.Add(new BooleanEntryDefinition(SortObjectParameterDefinitionKeys.AscendingEntryKey));
this.hashEntries.Add(new BooleanEntryDefinition(SortObjectParameterDefinitionKeys.DescendingEntryKey));
this.hashEntries.Add(new BooleanEntryDefinition(CalculatedPropertyDefinitionKeys.AscendingEntryKey, new string[] { CalculatedPropertyDefinitionKeys.AscendingEntryKeyShort }));
this.hashEntries.Add(new BooleanEntryDefinition(CalculatedPropertyDefinitionKeys.DescendingEntryKey, new string[] { CalculatedPropertyDefinitionKeys.DescendingEntryKeyShort }));
}
}

Expand Down Expand Up @@ -240,7 +231,7 @@ internal List<MshParameter> MshParameterList

foreach (MshParameter unexpandedParameter in _unexpandedParameterList)
{
PSPropertyExpression mshExpression = (PSPropertyExpression)unexpandedParameter.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey);
PSPropertyExpression mshExpression = (PSPropertyExpression)unexpandedParameter.GetEntry(CalculatedPropertyDefinitionKeys.ExpressionEntryKey);
if (!mshExpression.HasWildCardCharacters) // this special cases 1) script blocks and 2) wildcard-less strings
{
_mshParameterList.Add(unexpandedParameter);
Expand Down Expand Up @@ -269,7 +260,7 @@ private static List<MshParameter> ExpandExpressions(List<PSObject> inputObjects,
{
foreach (MshParameter unexpandedParameter in unexpandedParameterList)
{
PSPropertyExpression ex = (PSPropertyExpression)unexpandedParameter.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey);
PSPropertyExpression ex = (PSPropertyExpression)unexpandedParameter.GetEntry(CalculatedPropertyDefinitionKeys.ExpressionEntryKey);
if (!ex.HasWildCardCharacters) // this special cases 1) script blocks and 2) wildcard-less strings
{
expandedParameterList.Add(unexpandedParameter);
Expand Down Expand Up @@ -297,7 +288,7 @@ private static List<MshParameter> ExpandExpressions(List<PSObject> inputObjects,
{
MshParameter expandedParameter = new MshParameter();
expandedParameter.hash = (Hashtable)unexpandedParameter.hash.Clone();
expandedParameter.hash[FormatParameterDefinitionKeys.ExpressionEntryKey] = expandedExpression;
expandedParameter.hash[CalculatedPropertyDefinitionKeys.ExpressionEntryKey] = expandedExpression;

expandedParameterList.Add(expandedParameter);
}
Expand All @@ -316,7 +307,7 @@ private static void ExpandExpressions(PSObject inputObject, List<MshParameter> U
{
foreach (MshParameter unexpandedParameter in UnexpandedParametersWithWildCardPattern)
{
PSPropertyExpression ex = (PSPropertyExpression)unexpandedParameter.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey);
PSPropertyExpression ex = (PSPropertyExpression)unexpandedParameter.GetEntry(CalculatedPropertyDefinitionKeys.ExpressionEntryKey);

SortedDictionary<string, PSPropertyExpression> expandedPropertyNames = new SortedDictionary<string, PSPropertyExpression>(StringComparer.OrdinalIgnoreCase);
if (inputObject == null)
Expand All @@ -333,7 +324,7 @@ private static void ExpandExpressions(PSObject inputObject, List<MshParameter> U
{
MshParameter expandedParameter = new MshParameter();
expandedParameter.hash = (Hashtable)unexpandedParameter.hash.Clone();
expandedParameter.hash[FormatParameterDefinitionKeys.ExpressionEntryKey] = expandedExpression;
expandedParameter.hash[CalculatedPropertyDefinitionKeys.ExpressionEntryKey] = expandedExpression;

expandedParameterList.Add(expandedParameter);
}
Expand Down Expand Up @@ -414,9 +405,9 @@ private static bool isOrderEntryKeyDefined(object orderEntryKey)
for (int k = 0; k < ascendingOverrides.Length; k++)
{
object ascendingVal = mshParameterList[k].GetEntry(
SortObjectParameterDefinitionKeys.AscendingEntryKey);
CalculatedPropertyDefinitionKeys.AscendingEntryKey);
object descendingVal = mshParameterList[k].GetEntry(
SortObjectParameterDefinitionKeys.DescendingEntryKey);
CalculatedPropertyDefinitionKeys.DescendingEntryKey);
bool isAscendingDefined = isOrderEntryKeyDefined(ascendingVal);
bool isDescendingDefined = isOrderEntryKeyDefined(descendingVal);
if (!isAscendingDefined && !isDescendingDefined)
Expand Down Expand Up @@ -564,7 +555,7 @@ internal static class OrderByPropertyEntryEvaluationHelper
ref bool comparable)
{
// NOTE: we assume globbing was not allowed in input
PSPropertyExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression;
PSPropertyExpression ex = p.GetEntry(CalculatedPropertyDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression;

// get the values, but do not expand aliases
List<PSPropertyExpressionResult> expressionResults = ex.GetValues(inputObject, false, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ private void Emit(OrderByPropertyEntry entry, string sideIndicator)
Diagnostics.Assert(mshParameter != null, "null mshParameter");
Hashtable hash = mshParameter.hash;
Diagnostics.Assert(hash != null, "null hash");
object prop = hash[FormatParameterDefinitionKeys.ExpressionEntryKey];
object prop = hash[CalculatedPropertyDefinitionKeys.ExpressionEntryKey];
Diagnostics.Assert(prop != null, "null prop");
string propName = prop.ToString();
PSNoteProperty propertyNote = new PSNoteProperty(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,16 +282,6 @@ public SwitchParameter Transitional
}
private bool _transitional = false;

/// <summary>
/// Definitions for hash table keys.
/// </summary>
internal static class ConvertHTMLParameterDefinitionKeys
{
internal const string LabelEntryKey = "label";
internal const string AlignmentEntryKey = "alignment";
internal const string WidthEntryKey = "width";
}

/// <summary>
/// This allows for @{e='foo';label='bar';alignment='center';width='20'}.
/// </summary>
Expand All @@ -300,9 +290,9 @@ internal class ConvertHTMLExpressionParameterDefinition : CommandParameterDefini
protected override void SetEntries()
{
this.hashEntries.Add(new ExpressionEntryDefinition());
this.hashEntries.Add(new HashtableEntryDefinition(ConvertHTMLParameterDefinitionKeys.LabelEntryKey, new Type[] { typeof(string) }));
this.hashEntries.Add(new HashtableEntryDefinition(ConvertHTMLParameterDefinitionKeys.AlignmentEntryKey, new Type[] { typeof(string) }));
this.hashEntries.Add(new HashtableEntryDefinition(ConvertHTMLParameterDefinitionKeys.WidthEntryKey, new Type[] { typeof(string) }));
this.hashEntries.Add(new LabelEntryDefinition());
this.hashEntries.Add(new AlignmentEntryDefinition(forHtml: true));
this.hashEntries.Add(new WidthEntryDefinition(forHtml: true));
}
}

Expand Down Expand Up @@ -333,18 +323,31 @@ private void InitializeResolvedNameMshParameters()

foreach (MshParameter p in _propertyMshParameterList)
{
string label = p.GetEntry(ConvertHTMLParameterDefinitionKeys.LabelEntryKey) as string;
string alignment = p.GetEntry(ConvertHTMLParameterDefinitionKeys.AlignmentEntryKey) as string;
string width = p.GetEntry(ConvertHTMLParameterDefinitionKeys.WidthEntryKey) as string;
PSPropertyExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression;
string label = p.GetEntry(CalculatedPropertyDefinitionKeys.LabelEntryKey) as string;
string alignment = p.GetEntry(CalculatedPropertyDefinitionKeys.AlignmentEntryKey) as string;

// Accept the width both as a string and as an int.
string width;
int? widthNum = p.GetEntry(CalculatedPropertyDefinitionKeys.WidthEntryKey) as int?;
width = widthNum != null ? widthNum.Value.ToString() : p.GetEntry(CalculatedPropertyDefinitionKeys.WidthEntryKey) as string;
PSPropertyExpression ex = p.GetEntry(CalculatedPropertyDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression;
List<PSPropertyExpression> resolvedNames = ex.ResolveNames(_inputObject);
foreach (PSPropertyExpression resolvedName in resolvedNames)
{
Hashtable ht = CreateAuxPropertyHT(label, alignment, width);
ht.Add(FormatParameterDefinitionKeys.ExpressionEntryKey, resolvedName.ToString());
if (resolvedName.Script != null)
{
// The argument is a calculated property whose value is calculated by a script block.
ht.Add(CalculatedPropertyDefinitionKeys.ExpressionEntryKey, resolvedName.Script);
}
else
{
ht.Add(CalculatedPropertyDefinitionKeys.ExpressionEntryKey, resolvedName.ToString());
}
resolvedNameProperty.Add(ht);
}
}

_resolvedNameMshParameters = ProcessParameter(resolvedNameProperty.ToArray());
}

Expand All @@ -356,15 +359,15 @@ private void InitializeResolvedNameMshParameters()
Hashtable ht = new Hashtable();
if (label != null)
{
ht.Add(ConvertHTMLParameterDefinitionKeys.LabelEntryKey, label);
ht.Add(CalculatedPropertyDefinitionKeys.LabelEntryKey, label);
}
if (alignment != null)
{
ht.Add(ConvertHTMLParameterDefinitionKeys.AlignmentEntryKey, alignment);
ht.Add(CalculatedPropertyDefinitionKeys.AlignmentEntryKey, alignment);
}
if (width != null)
{
ht.Add(ConvertHTMLParameterDefinitionKeys.WidthEntryKey, width);
ht.Add(CalculatedPropertyDefinitionKeys.WidthEntryKey, width);
}
return ht;
}
Expand Down Expand Up @@ -502,14 +505,14 @@ private void WriteColumns(List<MshParameter> mshParams)
foreach (MshParameter p in mshParams)
{
COLTag.Append("<col");
string width = p.GetEntry(ConvertHTMLParameterDefinitionKeys.WidthEntryKey) as string;
string width = p.GetEntry(CalculatedPropertyDefinitionKeys.WidthEntryKey) as string;
if (width != null)
{
COLTag.Append(" width = \"");
COLTag.Append(width);
COLTag.Append("\"");
}
string alignment = p.GetEntry(ConvertHTMLParameterDefinitionKeys.AlignmentEntryKey) as string;
string alignment = p.GetEntry(CalculatedPropertyDefinitionKeys.AlignmentEntryKey) as string;
if (alignment != null)
{
COLTag.Append(" align = \"");
Expand Down Expand Up @@ -555,14 +558,14 @@ private void WriteListEntry()
private void WritePropertyName(StringBuilder Listtag, MshParameter p)
{
//for writing the property name
string label = p.GetEntry(ConvertHTMLParameterDefinitionKeys.LabelEntryKey) as string;
string label = p.GetEntry(CalculatedPropertyDefinitionKeys.LabelEntryKey) as string;
if (label != null)
{
Listtag.Append(label);
}
else
{
PSPropertyExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression;
PSPropertyExpression ex = p.GetEntry(CalculatedPropertyDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression;
Listtag.Append(ex.ToString());
}
}
Expand All @@ -572,7 +575,7 @@ private void WritePropertyName(StringBuilder Listtag, MshParameter p)
/// </summary>
private void WritePropertyValue(StringBuilder Listtag, MshParameter p)
{
PSPropertyExpression exValue = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression;
PSPropertyExpression exValue = p.GetEntry(CalculatedPropertyDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression;

// get the value of the property
List<PSPropertyExpressionResult> resultList = exValue.GetValues(_inputObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,9 @@ private void ProcessObject(PSObject inputObject)

private void ProcessParameter(MshParameter p, PSObject inputObject, List<PSNoteProperty> result)
{
string name = p.GetEntry(NameEntryDefinition.NameEntryKey) as string;
string name = p.GetEntry(CalculatedPropertyDefinitionKeys.NameEntryKey) as string;

PSPropertyExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression;
PSPropertyExpression ex = p.GetEntry(CalculatedPropertyDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression;
List<PSPropertyExpressionResult> expressionResults = new List<PSPropertyExpressionResult>();
foreach (PSPropertyExpression resolvedName in ex.ResolveNames(inputObject))
{
Expand Down Expand Up @@ -479,7 +479,7 @@ private void ProcessParameter(MshParameter p, PSObject inputObject, List<PSNoteP
private void ProcessExpandParameter(MshParameter p, PSObject inputObject,
List<PSNoteProperty> matchedProperties)
{
PSPropertyExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression;
PSPropertyExpression ex = p.GetEntry(CalculatedPropertyDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression;
List<PSPropertyExpressionResult> expressionResults = ex.GetValues(inputObject);

if (expressionResults.Count == 0)
Expand Down