< Summary

Class:Itinero.IO.Osm.Tiles.Parsers.Semantics.TagMapperConfigParser
Assembly:Itinero.IO.Osm.Tiles
File(s):/home/runner/work/routing2/routing2/src/Itinero.IO.Osm.Tiles/Parsers/Semantics/TagMapperConfigParser.cs
Covered lines:0
Uncovered lines:56
Coverable lines:56
Total lines:85
Line coverage:0% (0 of 56)
Covered branches:0
Total branches:18
Branch coverage:0% (0 of 18)
Tag:224_14471318300

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
Parse(...)0%180%

File(s)

/home/runner/work/routing2/routing2/src/Itinero.IO.Osm.Tiles/Parsers/Semantics/TagMapperConfigParser.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using Itinero.Logging;
 5using Newtonsoft.Json.Linq;
 6
 7namespace Itinero.IO.Osm.Tiles.Parsers.Semantics;
 8
 9public static class TagMapperConfigParser
 10{
 11    public static Dictionary<string, TagMapperConfig> Parse(Stream stream)
 012    {
 013        var mappings = new Dictionary<string, TagMapperConfig>();
 14
 015        using (var textReader = new StreamReader(stream))
 016        {
 017            var parsed = JArray.Parse(textReader.ReadToEnd());
 18
 019            foreach (var item in parsed)
 020            {
 21                try
 022                {
 023                    var osmKeyValue = item["osm_key"];
 024                    if (osmKeyValue == null)
 025                    {
 026                        throw new Exception("osm_key not found.");
 27                    }
 28
 029                    if (osmKeyValue.Type != JTokenType.String)
 030                    {
 031                        throw new Exception("osm_key not a string.");
 32                    }
 33
 034                    var osmKey = osmKeyValue.Value<string>();
 035                    var predicateValue = item["predicate"];
 036                    if (predicateValue == null)
 037                    {
 038                        throw new Exception("predicate not found.");
 39                    }
 40
 041                    if (predicateValue.Type != JTokenType.String)
 042                    {
 043                        throw new Exception("predicate not a string.");
 44                    }
 45
 046                    var predicate = predicateValue.Value<string>();
 47
 048                    var map = item["mapping"];
 049                    Dictionary<string, string>? mapping = null;
 050                    if (map != null)
 051                    {
 052                        mapping = new Dictionary<string, string>();
 053                        foreach (var child in map.Children())
 054                        {
 055                            if (!(child is JProperty property))
 056                            {
 057                                continue;
 58                            }
 59
 060                            if (property.Value is JValue val)
 061                            {
 062                                mapping[val.Value.ToInvariantString()] = property.Name;
 063                            }
 064                        }
 065                    }
 66
 067                    mappings[predicate] = new TagMapperConfig
 068                    {
 069                        ReverseMapping = mapping,
 070                        OsmKey = osmKey,
 071                        Predicate = predicate
 072                    };
 073                }
 074                catch (Exception ex)
 075                {
 076                    Logger.Log($"{nameof(TagMapperConfigParser)}.{nameof(Parse)}", TraceEventType.Error,
 077                        "Could not fully parse mapping configuration {0}", ex);
 078                    throw;
 79                }
 080            }
 081        }
 82
 083        return mappings;
 084    }
 85}

Methods/Properties

Parse(...)