< Summary

Information
Class: Elsa.Expressions.Helpers.ObjectFormatter
Assembly: Elsa.Expressions
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions/Helpers/ObjectFormatter.cs
Line coverage
64%
Covered lines: 16
Uncovered lines: 9
Coverable lines: 25
Total lines: 68
Line coverage: 64%
Branch coverage
59%
Covered branches: 13
Total branches: 22
Branch coverage: 59%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Format(...)66.66%301866.66%
IsMemoryLike(...)25%6450%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions/Helpers/ObjectFormatter.cs

#LineLine coverage
 1using System.Collections;
 2using System.ComponentModel;
 3using System.Text.Json;
 4
 5namespace Elsa.Expressions.Helpers;
 6
 7/// <summary>
 8/// Provides a set of static methods for formatting objects.
 9/// </summary>
 10public static class ObjectFormatter
 11{
 12    /// <summary>
 13    /// Formats the specified value as a string.
 14    /// </summary>
 15    /// <param name="value">The value.</param>
 16    /// <returns>A string representation of the value.</returns>
 17    public static string? Format(this object? value)
 18    {
 18719        if (value == null)
 9820            return null;
 21
 8922        if (value is string s)
 323            return s;
 24
 8625        var sourceType = value.GetType();
 8626        var underlyingSourceType = Nullable.GetUnderlyingType(sourceType) ?? sourceType;
 27
 8628        if (underlyingSourceType == typeof(string))
 029            return value as string;
 30
 31        // Byte arrays are base64-encoded for serialization
 8632        if (value is byte[] byteArray)
 033            return Convert.ToBase64String(byteArray);
 34
 35        // Memory-like types are preserved via TypeDescriptor
 8636        if (IsMemoryLike(sourceType))
 37        {
 038            var sourceTypeConverter = TypeDescriptor.GetConverter(underlyingSourceType);
 039            if (sourceTypeConverter.CanConvertTo(typeof(string)))
 040                return (string?)sourceTypeConverter.ConvertTo(value, typeof(string));
 41
 042            return value.ToString();
 43        }
 44
 45        // Serialize arrays and collections to JSON instead of "T[] Array" or "(Collection)".
 46        // Fixes GitHub issue #7019.
 8647        if (value is IEnumerable)
 48        {
 849            return JsonSerializer.Serialize(value);
 50        }
 51
 7852        var converter = TypeDescriptor.GetConverter(underlyingSourceType);
 53
 7854        if (converter.CanConvertTo(typeof(string)))
 7855            return (string?)converter.ConvertTo(value, typeof(string));
 56
 057        return value.ToString();
 58    }
 59
 60    private static bool IsMemoryLike(Type type)
 61    {
 8662        if (!type.IsGenericType)
 8663            return false;
 64
 065        var genericTypeDef = type.GetGenericTypeDefinition();
 066        return genericTypeDef == typeof(Memory<>) || genericTypeDef == typeof(ReadOnlyMemory<>);
 67    }
 68}