• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kysect / PowerShellRunner / 9758853126

02 Jul 2024 09:56AM UTC coverage: 56.868%. Remained the same
9758853126

push

github

FrediKats
Add IPowerShellObjectMapper

165 of 401 branches covered (41.15%)

Branch coverage included in aggregate %.

12 of 22 new or added lines in 3 files covered. (54.55%)

16 existing lines in 3 files now uncovered.

1222 of 2038 relevant lines covered (59.96%)

3.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.19
/Sources/Kysect.PowerShellRunner/Mapping/PowerShellObjectMapper.cs
1
using Kysect.CommonLib.BaseTypes.Extensions;
2
using Kysect.CommonLib.Reflection;
3
using Kysect.CommonLib.Reflection.TypeCache;
4
using Kysect.PowerShellRunner.Abstractions.Cmdlets;
5
using Kysect.PowerShellRunner.Abstractions.Objects;
6
using Kysect.PowerShellRunner.Tools;
7
using System.Reflection;
8
using System.Text.Json;
9

10
namespace Kysect.PowerShellRunner.Mapping;
11

12
public class PowerShellObjectMapper : IPowerShellObjectMapper
13
{
14
    private readonly PowerShellObjectMappingConfiguration _configuration;
15
    private readonly JsonSerializerOptions _serializerOptions;
16

17
    public static PowerShellObjectMapper Instance { get; } = Create();
1✔
18
    public static PowerShellObjectMapper Create()
19
    {
20
        return new PowerShellObjectMapper(
4✔
21
            new PowerShellObjectMappingConfiguration(),
4✔
22
            new JsonSerializerOptions());
4✔
23
    }
24

25
    public PowerShellObjectMapper(PowerShellObjectMappingConfiguration configuration, JsonSerializerOptions serializerOptions)
26
    {
27
        _configuration = configuration;
4✔
28
        _serializerOptions = serializerOptions;
4✔
29
    }
4✔
30

31
    public T Map<T>(IPowerShellObject powerShellObject) where T : notnull
32
    {
33
        powerShellObject.ThrowIfNull();
5✔
34

35
        var reflectionInstanceInitializer = new ReflectionInstanceInitializer<T>();
5✔
36
        foreach (IPowerShellObjectMember powerShellMember in powerShellObject.GetProperties())
24✔
37
        {
38

39
            if (powerShellMember.Value is null)
7✔
40
                continue;
41

42
            PropertyInfo? targetProperty = TypeInstanceCache<T>.GetPublicProperties().SingleOrDefault(p => p.Name == powerShellMember.Name);
20✔
43
            if (targetProperty is null)
7✔
44
                continue;
45

46
            object convertValue = ConvertValue(powerShellMember.Value);
7✔
47
            if (targetProperty.PropertyType != convertValue.GetType())
7✔
48
            {
49
                convertValue = Map(convertValue, targetProperty);
1✔
50
            }
51

52
            reflectionInstanceInitializer.Set(powerShellMember.Name, convertValue);
7✔
53
        }
54

55
        return reflectionInstanceInitializer.GetValue();
5✔
56
    }
57

58
    private object ConvertValue(object sourceValue)
59
    {
60
        if (_configuration.TryMap(sourceValue, out object? converterValue))
7!
UNCOV
61
            return converterValue;
×
62

63
        return sourceValue;
7✔
64
    }
65

66
    // TODO: move to ReflectionJsonInstanceCreator
67
    private object Map(object sourceValue, PropertyInfo targetProperty)
68
    {
69
        try
70
        {
71
            string serializeObject = JsonSerializer.Serialize(sourceValue, _serializerOptions);
1✔
72
            object? result = JsonSerializer.Deserialize(serializeObject, targetProperty.PropertyType, _serializerOptions);
1✔
73
            return result.ThrowIfNull();
1✔
74
        }
UNCOV
75
        catch (Exception e)
×
76
        {
UNCOV
77
            throw new PowerShellIntegrationException($"Cannot map instance of type {sourceValue.GetType().Name} to type {targetProperty.PropertyType.Name}", e);
×
78
        }
79
    }
1✔
80
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc