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

BHoM_Engine: creation of type in case of multiple matching types fixed #3344

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions BHoM_Engine/Create/Type/AllTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,7 @@ public static List<Type> AllTypes(string name, bool silent = false)
return new List<Type>();
}

List<Type> typeList = new List<Type>();
if (name.IsEngineNamespace())
typeList = Query.EngineTypeList();
else if (name.IsAdapterNamespace())
typeList = Query.AdapterTypeList();
else if (name.IsOmNamespace())
typeList = Query.BHoMTypeList();
else
typeList = Query.AllTypeList();

List<Type> typeList = Query.AllTypeList();
List<Type> types;
if (name.Contains(','))
types = typeList.Where(x => x.AssemblyQualifiedName.Contains(name)).ToList();
Expand Down
27 changes: 15 additions & 12 deletions BHoM_Engine/Create/Type/GenericType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ public static partial class Create
/**** Public Methods ****/
/***************************************************/

[PreviousVersion("7.2", "BH.Engine.Base.Create.GenericType(System.String, System.Boolean)")]
[Description("Creates a generic BHoM type that matches the given name.")]
[Input("name", "Name to be searched for among all BHoM generic types.")]
[Input("silent", "If true, the error about no type found will be suppressed, otherwise it will be raised.")]
[Input("takeFirstIfMultiple", "Defines what happens in case of finding multiple matching types. If true, first type found will be returned, otherwise null.")]
[Output("type", "BHoM generic type that matches the given name.")]
public static Type GenericType(string name, bool silent = false)
public static Type GenericType(string name, bool silent = false, bool takeFirstIfMultiple = false)
{
if (string.IsNullOrWhiteSpace(name))
{
Expand All @@ -48,9 +50,9 @@ public static Type GenericType(string name, bool silent = false)
}

if (name.Contains("<"))
return GenericTypeAngleBrackets(name, silent);
return GenericTypeAngleBrackets(name, silent, takeFirstIfMultiple);
else if (name.Contains('`') && name.Contains("[["))
return GenericTypeSquareBrackets(name, silent);
return GenericTypeSquareBrackets(name, silent, takeFirstIfMultiple);
else //No split chars found, return null and error as type is not generic
{
Compute.RecordError($"Provided string is not a generic type. Provided string was: {name}.");
Expand All @@ -60,21 +62,22 @@ public static Type GenericType(string name, bool silent = false)

/***************************************************/



[PreviousVersion("7.2", "BH.Engine.Base.Create.GenericType(System.String, System.Collections.Generic.List<System.String>, System.Boolean)")]
[Description("Creates a generic BHoM type that matches the given name.")]
[Input("name", "Name to be searched for among all BHoM generic types.")]
[Input("arguments", "Generic parameters of the type to create.")]
[Input("silent", "If true, the error about no type found will be suppressed, otherwise it will be raised.")]
[Input("takeFirstIfMultiple", "Defines what happens in case of finding multiple matching types. If true, first type found will be returned, otherwise null.")]
[Output("type", "BHoM generic type that matches the given name.")]
public static Type GenericType(string name, List<string> arguments, bool silent = false)
public static Type GenericType(string name, List<string> arguments, bool silent = false, bool takeFirstIfMultiple = false)
{
Type typeDefinition = Type(name, silent);
Type typeDefinition = Type(name, silent, takeFirstIfMultiple);
if (typeDefinition == null)
return null;

try
{
return typeDefinition.MakeGenericType(arguments.Select(x => Type(x, silent)).ToArray()); //Call to Type(x) will recursively call this method for inner generic types
return typeDefinition.MakeGenericType(arguments.Select(x => Type(x, silent, takeFirstIfMultiple)).ToArray()); //Call to Type(x) will recursively call this method for inner generic types
}
catch
{
Expand All @@ -90,7 +93,7 @@ public static Type GenericType(string name, List<string> arguments, bool silent
[Input("name", "Name to be searched for among all BHoM generic types.")]
[Input("silent", "If true, the error about no type found will be suppressed, otherwise it will be raised.")]
[Output("type", "BHoM generic type that matches the given name.")]
private static Type GenericTypeAngleBrackets(string name, bool silent = false)
private static Type GenericTypeAngleBrackets(string name, bool silent = false, bool takeFirstIfMultiple = false)
{
if (string.IsNullOrWhiteSpace(name))
{
Expand Down Expand Up @@ -141,7 +144,7 @@ private static Type GenericTypeAngleBrackets(string name, bool silent = false)
}
//Main type definition as string up until the first split char (first '<').
//Number of generic arguments will be 1 less than the number of argsSplit count
return GenericType(name.Substring(0, argsSplit[0]) + "`" + (argsSplit.Count - 1), arguments, silent);
return GenericType(name.Substring(0, argsSplit[0]) + "`" + (argsSplit.Count - 1), arguments, silent, takeFirstIfMultiple);
}

/***************************************************/
Expand All @@ -150,7 +153,7 @@ private static Type GenericTypeAngleBrackets(string name, bool silent = false)
[Input("name", "Name to be searched for among all BHoM generic types.")]
[Input("silent", "If true, the error about no type found will be suppressed, otherwise it will be raised.")]
[Output("type", "BHoM generic type that matches the given name.")]
private static Type GenericTypeSquareBrackets(string name, bool silent = false)
private static Type GenericTypeSquareBrackets(string name, bool silent = false, bool takeFirstIfMultiple = false)
{
if (string.IsNullOrWhiteSpace(name))
{
Expand Down Expand Up @@ -210,7 +213,7 @@ private static Type GenericTypeSquareBrackets(string name, bool silent = false)
string mainName = name.Substring(0, nameEnd);
//Main type definition as string up until the first split char (first '<').
//Number of generic arguments will be 1 less than the number of argsSplit count
return GenericType(mainName, arguments, silent);
return GenericType(mainName, arguments, silent, takeFirstIfMultiple);
}

/***************************************************/
Expand Down
12 changes: 8 additions & 4 deletions BHoM_Engine/Create/Type/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ public static partial class Create
/**** Public Methods ****/
/***************************************************/

[PreviousVersion("7.2", "BH.Engine.Base.Create.Type(System.String, System.Boolean)")]
[Description("Creates a BHoM type that matches the given name.")]
[Input("name", "Name to be searched for among all BHoM types.")]
[Input("silent", "If true, the error about no type found will be suppressed, otherwise it will be raised.")]
[Input("takeFirstIfMultiple", "Defines what happens in case of finding multiple matching types. If true, first type found will be returned, otherwise null.")]
[Output("type", "BHoM type that matches the given name.")]
public static Type Type(string name, bool silent = false)
public static Type Type(string name, bool silent = false, bool takeFirstIfMultiple = false)
{
if (name == null)
{
Expand All @@ -49,9 +51,9 @@ public static Type Type(string name, bool silent = false)
}

if (name.Contains('<'))
return GenericTypeAngleBrackets(name, silent);
return GenericTypeAngleBrackets(name, silent, takeFirstIfMultiple);
else if (name.Contains('`') && name.Contains("[["))
return GenericTypeSquareBrackets(name, silent);
return GenericTypeSquareBrackets(name, silent, takeFirstIfMultiple);

string unQualifiedName = name.Contains(",") ? name.Split(',').First() : name;
List<Type> types = null;
Expand All @@ -68,14 +70,16 @@ public static Type Type(string name, bool silent = false)
if (type != null)
type = type.MakeByRefType();
}

if (type == null && !silent)
Compute.RecordError($"A type corresponding to {name} cannot be found.");

return type;
}
else if (types.Count == 1)
return types[0];
else if (types.Count > 1 && takeFirstIfMultiple)
return types.OrderBy(x => x.Assembly.FullName).First();
else if (!silent)
{
string message = "Ambiguous match: Multiple types correspond the the name provided: \n";
Expand Down
4 changes: 2 additions & 2 deletions Serialiser_Engine/Compute/Deserialise/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private static Type GetTypeFromName(string fullName)
if (fullName == "T")
return null;
if (fullName.IsOmNamespace())
type = Base.Create.Type(fullName, true);
type = Base.Create.Type(fullName, true, true);
else if (fullName.IsEngineNamespace())
type = Base.Create.EngineType(fullName, true);
else
Expand All @@ -157,7 +157,7 @@ private static Type GetTypeFromName(string fullName)
{
List<Type> types = Base.Create.AllTypes(fullName, true);
if (types.Count > 0)
type = types.First();
type = types.OrderBy(x => x.Assembly.FullName).First();
}

return type;
Expand Down
Loading