Files
speckle-sharp-connectors/Converters/CSi/Speckle.Converters.CSiShared/Utils/DictionaryExtensions.cs
T
Björn Steinhagen bb252a5115 bjorn/cnx-1112-get-user-facing-names-of-properties (#526)
* style: user facing names for frames

* style: user facing names for shells

* styling: user facing names for proxies

* style: dict utils as extension methods
2025-01-29 11:25:15 +01:00

43 lines
1.4 KiB
C#

namespace Speckle.Converters.CSiShared.Utils;
/// <summary>
/// Provides extension methods for dictionary operations common across the CSI converter.
/// </summary>
public static class DictionaryExtensions
{
/// <summary>
/// Ensures a nested dictionary exists at the specified key, creating it if necessary.
/// Used for organizing properties into hierarchical categories (e.g., "Geometry", "Assignments", "Design").
/// </summary>
/// <remarks>
/// This pattern is used throughout property extractors to maintain consistent property organization.
/// </remarks>
public static Dictionary<string, object?> EnsureNested(this Dictionary<string, object?> dictionary, string key)
{
if (!dictionary.TryGetValue(key, out var obj) || obj is not Dictionary<string, object?> nestedDictionary)
{
nestedDictionary = [];
dictionary[key] = nestedDictionary;
}
return nestedDictionary;
}
/// <summary>
/// Adds a value with its associated units to a parent dictionary using a standardized format.
/// Creates a nested dictionary with 'name', 'value', and 'units' keys.
/// </summary>
public static void AddWithUnits(
this Dictionary<string, object?> dictionary,
string key,
object value,
string? units = null
) =>
dictionary[key] = new Dictionary<string, object?>
{
["name"] = key,
["value"] = value,
["units"] = units
};
}