Skip to content

Commit

Permalink
[ADD] possibility to add custom C# type mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander van Delft committed Jan 22, 2025
1 parent 84aba3a commit 831b281
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 2 deletions.
55 changes: 55 additions & 0 deletions uml4net.Extensions.Tests/PropertyExtensionsTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,61 @@ public void Verify_that_QueryCSharpTypeName_returns_expected_Result()
Assert.That(name.QueryCSharpTypeName(), Is.EqualTo("string"));
}

[Test]
public void Verify_that_QueryCSharpTypeName_returns_expected_Result_when_Custom_mappings_are_added()
{
var structuredClassifiersPackage = this.xmiReaderResult.Root.NestedPackage.Single(x => x.Name == "Classification");

var property = structuredClassifiersPackage.PackagedElement.OfType<IClass>().Single(x => x.Name == "Property");

var lower = property.QueryAllProperties().Single(x => x.Name == "lower");
var dataType = property.QueryAllProperties().Single(x => x.Name == "datatype");

//Using only default mappings
Assert.Multiple(() =>
{
Assert.That(lower.QueryCSharpTypeName(), Is.EqualTo("int"));
Assert.That(dataType.QueryCSharpTypeName(), Is.EqualTo("DataType"));
});

//Using new Custom Type mapping
PropertyExtensions.AddOrOverwriteCSharpTypeMappings(("DataType", "CustomType"));

Assert.Multiple(() =>
{
Assert.That(lower.QueryCSharpTypeName(), Is.EqualTo("int"));
Assert.That(dataType.QueryCSharpTypeName(), Is.EqualTo("CustomType"));
});

//Using overwritten Custom Type mapping
PropertyExtensions.AddOrOverwriteCSharpTypeMappings(("DataType", "CustomType2"));

Assert.Multiple(() =>
{
Assert.That(lower.QueryCSharpTypeName(), Is.EqualTo("int"));
Assert.That(dataType.QueryCSharpTypeName(), Is.EqualTo("CustomType2"));
});


//Using overwritten Default Type mapping
PropertyExtensions.AddOrOverwriteCSharpTypeMappings(("Integer", "CustomInt"));

Assert.Multiple(() =>
{
Assert.That(lower.QueryCSharpTypeName(), Is.EqualTo("CustomInt"));
Assert.That(dataType.QueryCSharpTypeName(), Is.EqualTo("CustomType2"));
});

//After reset of custom mappings all should be using only default mappings again
PropertyExtensions.ResetCSharpTypeMappingsToDefault();

Assert.Multiple(() =>
{
Assert.That(lower.QueryCSharpTypeName(), Is.EqualTo("int"));
Assert.That(dataType.QueryCSharpTypeName(), Is.EqualTo("DataType"));
});
}

[Test]
public void Verify_that_QueryIsEnumerable_returns_expected_Result()
{
Expand Down
82 changes: 80 additions & 2 deletions uml4net.Extensions/PropertyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace uml4net.Extensions
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

using uml4net.Classification;
using uml4net.CommonStructure;
using uml4net.SimpleClassifiers;
Expand Down Expand Up @@ -147,7 +147,7 @@ public static bool QueryIsNumeric(this IProperty property)
/// <summary>
/// A mapping of the known SysML value types to C# types
/// </summary>
private static readonly Dictionary<string, string> CSharpTypeMapping = new ()
private static readonly Dictionary<string, string> DefaultCSharpTypeMapping = new ()
{
{"Boolean", "bool"},
{"Integer", "int"},
Expand All @@ -156,6 +156,84 @@ public static bool QueryIsNumeric(this IProperty property)
{"UnlimitedNatural", "string"},
};

/// <summary>
/// A mapping of the custom SysML value types to C# types
/// </summary>
private static readonly Dictionary<string, string> CustomCSharpTypeMapping = new();

/// <summary>
/// A mapping of the known and custom SysML value types to C# types
/// </summary>
/// <remarks>
/// By default, contains all items from DefaultCSharpTypeMapping
/// </remarks>
private static readonly Dictionary<string, string> CSharpTypeMapping =
DefaultCSharpTypeMapping.ToDictionary(x => x.Key, x => x.Value);

/// <summary>
/// Adds or overwrites the C# type mappings
/// </summary>
/// <param name="mappings">Collection of tuples with Key and Value data to add to custom mappings, or overwrite default mappings</param>
public static void AddOrOverwriteCSharpTypeMappings(params (string Key, string Value)[] mappings)
{
foreach (var kvp in mappings)
{
if (CustomCSharpTypeMapping.TryGetValue(kvp.Key, out var curVal))
{
if (curVal != kvp.Value)
{
CustomCSharpTypeMapping[kvp.Key] = kvp.Value;
}
}
else
{
CustomCSharpTypeMapping.Add(kvp.Key, kvp.Value);
}
}

RecalculateCSharpTypeMapping();
}

/// <summary>
/// Removes any added custom C# type mapping and resets to the default in <see cref="DefaultCSharpTypeMapping"/>
/// </summary>
public static void ResetCSharpTypeMappingsToDefault()
{
CustomCSharpTypeMapping.Clear();
RecalculateCSharpTypeMapping();
}

/// <summary>
/// Concatenates the default and custom type mappings from <see cref="CustomCSharpTypeMapping"/> and <see cref="DefaultCSharpTypeMapping"/> and stores the result in <see cref="CSharpTypeMapping"/>
/// </summary>
/// <remarks>
/// If the same key exist in <see cref="DefaultCSharpTypeMapping"/> and <see cref="CustomCSharpTypeMapping"/>, the value of the <see cref="CustomCSharpTypeMapping"/> mapping will be used.
/// </remarks>
private static void RecalculateCSharpTypeMapping()
{
CSharpTypeMapping.Clear();

foreach (var kvp in DefaultCSharpTypeMapping)
{
CSharpTypeMapping.Add(kvp.Key, kvp.Value);
}

foreach (var kvp in CustomCSharpTypeMapping)
{
if (CSharpTypeMapping.TryGetValue(kvp.Key, out var curVal))
{
if (curVal != kvp.Value)
{
CSharpTypeMapping[kvp.Key] = kvp.Value;
}
}
else
{
CSharpTypeMapping.Add(kvp.Key, kvp.Value);
}
}
}

/// <summary>
/// Queries the C# type-name of the <see cref="IProperty"/>
/// </summary>
Expand Down

0 comments on commit 831b281

Please sign in to comment.