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: Add DisplayText support on enums #2930

Merged
merged 3 commits into from
Oct 20, 2022
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
6 changes: 4 additions & 2 deletions BHoM_Engine/Compute/ParseEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ public static object ParseEnum(Type enumType, string value)
return Enum.GetValues(enumType).OfType<Enum>()
.FirstOrDefault(x => {
FieldInfo fi = enumType.GetField(x.ToString());
DescriptionAttribute[] attributes = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
DisplayTextAttribute[] displayTexts = fi.GetCustomAttributes(typeof(DisplayTextAttribute), false) as DisplayTextAttribute[];
DescriptionAttribute[] descriptions = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];

return attributes != null && attributes.Count() > 0 && attributes.First().Description == value;
return (displayTexts != null && displayTexts.Count() > 0 && displayTexts.First().Text == value)
|| (descriptions != null && descriptions.Count() > 0 && descriptions.First().Description == value);
});
}
}
Expand Down
56 changes: 55 additions & 1 deletion BHoM_Engine/Convert/ToText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
Expand All @@ -36,6 +37,10 @@ public static partial class Convert
/**** Interface Methods ****/
/***************************************************/

[Description("Provides a human-friendly text representation of the provided object.")]
[Input("member", "object to convert to text.")]
[Input("includePath", "If true, the path/namespace will be provided.")]
[Output("Text representation.")]
public static string IToText(this object member, bool includePath = false)
{
if (member == null)
Expand All @@ -49,6 +54,10 @@ public static string IToText(this object member, bool includePath = false)
/**** Public Methods ****/
/***************************************************/

[Description("Provides a human-friendly text representation of a member of a class.")]
[Input("member", "member to convert to text.")]
[Input("includePath", "If true, the path/namespace will be provided.")]
[Output("Text representation.")]
public static string ToText(this MemberInfo member, bool includePath = false)
{
if (member == null)
Expand All @@ -64,6 +73,18 @@ public static string ToText(this MemberInfo member, bool includePath = false)

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

[Description("Provides a human-friendly text representation of a method.")]
[Input("method", "method to convert to text.")]
[Input("includePath", "If true, the path/namespace will be provided.")]
[Input("paramStart", "Start symbol used for the beginning of the method parameters. Usually, '('.")]
[Input("paramSeparator", "Symbol used to separate the parameters. Usually, ','.")]
[Input("paramEnd", "Start symbol used for the end of the method parameters. Usually, ')'.")]
[Input("removeIForInterface", "If true, the 'I' in front of the interface method will not show.")]
[Input("includeParamNames", "If true, the name of the parameters will be written. Otherwise, only their types will show.")]
[Input("maxParams", "Maximum number of parameter to write. Any remaining will be represented with '...'.")]
[Input("maxChars", "Maximum number of characters to use. Any remaining will be represented with '...'.")]
[Input("includeParamPaths", "If true, the path/namespace will be provided.")]
[Output("Text representation.")]
public static string ToText(this MethodBase method, bool includePath = false, string paramStart = "(", string paramSeparator = ", ", string paramEnd = ")", bool removeIForInterface = true, bool includeParamNames = true, int maxParams = 5, int maxChars = 40, bool includeParamPaths = false)
{
if (method == null)
Expand Down Expand Up @@ -123,6 +144,13 @@ public static string ToText(this MethodBase method, bool includePath = false, st

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

[Description("Provides a human-friendly text representation of a type.")]
[Input("type", "type to convert to text.")]
[Input("includePath", "If true, the path/namespace will be provided.")]
[Input("genericStart", "Start symbol used for the beginning of the generic parameters, if any. Usually, '<'.")]
[Input("genericSeparator", "Symbol used to separate the generic parameters. Usually, ','.")]
[Input("genericEnd", "Start symbol used for the end of the generic parameters, if any. Usually, '>'.")]
[Output("Text representation.")]
public static string ToText(this Type type, bool includePath = false, bool replaceGeneric = false, string genericStart = "<", string genericSeparator = ", ", string genericEnd = ">")
{
if (type == null)
Expand Down Expand Up @@ -158,7 +186,33 @@ public static string ToText(this Type type, bool includePath = false, bool repla
}
}

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

[Description("Provides a human-friendly text representation of an enum.")]
[Input("item", "enum to convert to text.")]
[Input("includePath", "If true, the path/namespace will be provided.")]
[Output("Text representation.")]
public static string ToText(this Enum item, bool includePath = false)
{
if (item == null)
return "null";

if (includePath)
{
Type type = item.GetType();
return type.Path() + "." + type.Name + "." + item.ToString();
}
else
{
FieldInfo fi = item.GetType().GetField(item.ToString());
DisplayTextAttribute[] attributes = fi.GetCustomAttributes(typeof(DisplayTextAttribute), false) as DisplayTextAttribute[];

if (attributes != null && attributes.Count() > 0)
return attributes.First().Text;
else
return item.ToString();
}
}

/***************************************************/
/**** Fallback Methods ****/
Expand All @@ -168,7 +222,7 @@ private static string ToText(object item, bool includePath = false)
{
if (item == null)
return "null";
else if (item is string || item.GetType().IsEnum)
else if (item is string)
return item.ToString();
else
return item.GetType().ToString();
Expand Down