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

net-daemon / netdaemon / 6742201237

03 Nov 2023 06:54AM UTC coverage: 79.655% (+0.1%) from 79.548%
6742201237

push

github

helto4real
[Codegen] Allow any nullable double fields to be strings or doubles and improved error reporting (#984)

* Allow any nullable double fields to be strings or doubles and improved error reporting

* small fix

813 of 1153 branches covered (0.0%)

Branch coverage included in aggregate %.

49 of 57 new or added lines in 5 files covered. (85.96%)

8 existing lines in 3 files now uncovered.

2926 of 3541 relevant lines covered (82.63%)

50.04 hits per line

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

88.24
/src/HassModel/NetDaemon.HassModel.CodeGenerator/MetaData/ServicesMetaData/ServiceMetaDataParser.cs
1
namespace NetDaemon.HassModel.CodeGenerator.Model;
2

3
public record DeserializationError(Exception Exception, string? Context, JsonElement Element);
5✔
4

5
internal static class ServiceMetaDataParser
6
{
7

8
    public static readonly JsonSerializerOptions SerializerOptions = new()
1✔
9
    {
1✔
10
        PropertyNamingPolicy = SnakeCaseNamingPolicy.Instance,
1✔
11
        Converters = { new StringAsDoubleConverter() }
1✔
12
    };
1✔
13

UNCOV
14
    public static IReadOnlyCollection<HassServiceDomain> Parse(JsonElement element) => Parse(element, out _);
×
15

16
    /// <summary>
17
    ///     Parses all json elements to instance result from GetServices call
18
    /// </summary>
19
    /// <param name="element">JsonElement containing the result data</param>
20
    /// <param name="errors">Outputs Any Exceptions during deserialization</param>
21
    public static IReadOnlyCollection<HassServiceDomain> Parse(JsonElement element, out List<DeserializationError> errors)
22
    {
23
        errors = new List<DeserializationError>();
7✔
24
        if (element.ValueKind != JsonValueKind.Object)
7!
UNCOV
25
            throw new InvalidOperationException("Not expected result from the GetServices result");
×
26

27
        var hassServiceDomains = new List<HassServiceDomain>();
7✔
28
        foreach (var domainProperty in element.EnumerateObject())
28✔
29
        {
30
            try
31
            {
32
                var hassServiceDomain = new HassServiceDomain
7✔
33
                {
7✔
34
                    Domain = domainProperty.Name,
7✔
35
                    Services = GetServices(domainProperty.Value, errors, domainProperty.Name)
7✔
36
                };
7✔
37
                hassServiceDomains.Add(hassServiceDomain);
7✔
38
            }
7✔
UNCOV
39
            catch (JsonException e)
×
40
            {
NEW
41
                errors.Add(new (e, domainProperty.Name, domainProperty.Value));
×
UNCOV
42
            }
×
43
        }
44
        return hassServiceDomains;
7✔
45
    }
46

47
    private static IReadOnlyCollection<HassService> GetServices(JsonElement domainElement, List<DeserializationError> errors, string context)
48
    {
49
        return domainElement.EnumerateObject()
7✔
50
            .Select(serviceDomainProperty =>
7✔
51
                GetService(serviceDomainProperty.Name, serviceDomainProperty.Value, errors, context))
10✔
52
            .OfType<HassService>().ToList();
7✔
53
  }
54

55
    private static HassService? GetService(string serviceName, JsonElement serviceElement, List<DeserializationError> errors, string context)
56
    {
57
        try
58
        {
59
            var result = serviceElement.Deserialize<HassService>(SerializerOptions)! with
10✔
60
            {
10✔
61
                Service = serviceName,
10✔
62
            };
10✔
63

64
            if (serviceElement.TryGetProperty("fields", out var fieldProperty))
9✔
65
            {
66
                result = result with
9✔
67
                {
9✔
68
                    Fields = fieldProperty.EnumerateObject().Select(p => GetField(p.Name, p.Value)).ToList()
9✔
69
                };
9✔
70
            }
71

72
            return result;
9✔
73
        }
74
        catch (Exception ex)
1✔
75
        {
76
            errors.Add(new (ex, $"{context}.{serviceName}", serviceElement));
1✔
77
            return null;
1✔
78
        }
79
    }
10✔
80

81
    private static HassServiceField GetField(string fieldName, JsonElement element)
82
    {
83
        return element.Deserialize<HassServiceField>(SerializerOptions)! with
9✔
84
        {
9✔
85
            Field = fieldName,
9✔
86
        };
9✔
87
    }
88
}
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

© 2025 Coveralls, Inc