MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.Collections.Generic;6using System.Globalization;7using System.Management.Automation;8using System.Management.Automation.Internal;9 10namespace Microsoft.PowerShell.Commands.Internal.Format11{12 /// <summary>13 /// Internal class to manage the grouping algorithm for the14 /// format-xxx commands.15 /// </summary>16 internal sealed class GroupingInfoManager17 {18 /// <summary>19 /// Initialize with the grouping property data.20 /// </summary>21 /// <param name="groupingExpression">Name of the grouping property.</param>22 /// <param name="displayLabel">Display name of the property.</param>23 internal void Initialize(PSPropertyExpression groupingExpression, string displayLabel)24 {25 _groupingKeyExpression = groupingExpression;26 _label = displayLabel;27 }28 29 internal object CurrentGroupingKeyPropertyValue30 {31 get { return _currentGroupingKeyPropertyValue; }32 }33 34 internal string GroupingKeyDisplayName35 {36 get37 {38 if (_label != null)39 return _label;40 return _groupingKeyDisplayName;41 }42 }43 44 /// <summary>45 /// Compute the string value of the grouping property.46 /// </summary>47 /// <param name="so">Object to use to compute the property value.</param>48 /// <returns>True if there was an update.</returns>49 internal bool UpdateGroupingKeyValue(PSObject so)50 {51 if (_groupingKeyExpression == null)52 return false;53 54 List<PSPropertyExpressionResult> results = _groupingKeyExpression.GetValues(so);55 56 // if we have more that one match, we have to select the first one57 if (results.Count > 0 && results[0].Exception == null)58 {59 // no exception got thrown, so we can update60 object newValue = results[0].Result;61 object oldValue = _currentGroupingKeyPropertyValue;62 63 _currentGroupingKeyPropertyValue = newValue;64 65 // now do the comparison66 bool update = !(IsEqual(_currentGroupingKeyPropertyValue, oldValue) ||67 IsEqual(oldValue, _currentGroupingKeyPropertyValue));68 69 if (update && _label == null)70 {71 _groupingKeyDisplayName = results[0].ResolvedExpression.ToString();72 }73 74 return update;75 }76 77 // we had no matches or we could not get the value:78 // NOTICE: we need to do this to avoid starting a new group every time79 // there is a failure to read the grouping property.80 // For example, for AD, there are objects that throw when trying81 // to read the "distinguishedName" property (used by the brokered property "ParentPath)82 return false;83 }84 85 private static bool IsEqual(object first, object second)86 {87 if (LanguagePrimitives.TryCompare(first, second, true, CultureInfo.CurrentCulture, out int result))88 {89 return result == 0;90 }91 92 // Note that this will occur if the objects do not support93 // IComparable. We fall back to comparing as strings.94 95 // being here means the first object doesn't support ICompare96 // or an Exception was raised win Compare97 string firstString = PSObject.AsPSObject(first).ToString();98 string secondString = PSObject.AsPSObject(second).ToString();99 100 return string.Equals(firstString, secondString, StringComparison.CurrentCultureIgnoreCase);101 }102 103 /// <summary>104 /// Value of the display label passed in.105 /// </summary>106 private string _label = null;107 108 /// <summary>109 /// Value of the current active grouping key.110 /// </summary>111 private string _groupingKeyDisplayName = null;112 113 /// <summary>114 /// Name of the current grouping key.115 /// </summary>116 private PSPropertyExpression _groupingKeyExpression = null;117 118 /// <summary>119 /// The current value of the grouping key.120 /// </summary>121 private object _currentGroupingKeyPropertyValue = AutomationNull.Value;122 }123}124 