LCOV - code coverage report
Current view: top level - lib/matrix_api_lite/utils - try_get_map_extension.dart (source / functions) Coverage Total Hit
Test: merged.info Lines: 84.2 % 38 32
Test Date: 2025-01-14 13:39:53 Functions: - 0 0

            Line data    Source code
       1              : /* MIT License
       2              : * 
       3              : * Copyright (C) 2019, 2020, 2021 Famedly GmbH
       4              : * 
       5              : * Permission is hereby granted, free of charge, to any person obtaining a copy
       6              : * of this software and associated documentation files (the "Software"), to deal
       7              : * in the Software without restriction, including without limitation the rights
       8              : * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
       9              : * copies of the Software, and to permit persons to whom the Software is
      10              : * furnished to do so, subject to the following conditions:
      11              : * 
      12              : * The above copyright notice and this permission notice shall be included in all
      13              : * copies or substantial portions of the Software.
      14              : * 
      15              : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      16              : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      17              : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      18              : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      19              : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      20              : * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      21              : * SOFTWARE.
      22              : */
      23              : 
      24              : import 'dart:core';
      25              : 
      26              : import 'package:matrix/matrix_api_lite/utils/logs.dart';
      27              : 
      28              : abstract class TryGet {
      29              :   void call(String key, Type expected, Type actual);
      30              : 
      31              :   static const TryGet required = _RequiredLog();
      32              :   static const TryGet optional = _OptionalLog();
      33              : 
      34              :   /// This is helpful if you have a field that can mean multiple things on purpose.
      35              :   static const TryGet silent = _SilentLog();
      36              : }
      37              : 
      38              : class _RequiredLog implements TryGet {
      39           80 :   const _RequiredLog();
      40            2 :   @override
      41            4 :   void call(String key, Type expected, Type actual) => Logs().w(
      42            6 :         'Expected required "$expected" in event content for the Key "$key" but got "$actual" at ${StackTrace.current.firstLine}',
      43              :       );
      44              : }
      45              : 
      46              : class _OptionalLog implements TryGet {
      47           80 :   const _OptionalLog();
      48           42 :   @override
      49              :   void call(String key, Type expected, Type actual) {
      50           42 :     if (actual != Null) {
      51           14 :       Logs().w(
      52           21 :         'Expected optional "$expected" in event content for the Key "$key" but got "$actual" at ${StackTrace.current.firstLine}',
      53              :       );
      54              :     }
      55              :   }
      56              : }
      57              : 
      58              : class _SilentLog implements TryGet {
      59           80 :   const _SilentLog();
      60           27 :   @override
      61              :   void call(String key, Type expected, Type actual) {}
      62              : }
      63              : 
      64              : extension TryGetMapExtension on Map<String, Object?> {
      65           44 :   T? tryGet<T extends Object>(String key, [TryGet log = TryGet.optional]) {
      66           44 :     final Object? value = this[key];
      67           44 :     if (value is! T) {
      68           84 :       log(key, T, value.runtimeType);
      69              :       return null;
      70              :     }
      71              :     return value;
      72              :   }
      73              : 
      74           39 :   List<T>? tryGetList<T>(String key, [TryGet log = TryGet.optional]) {
      75           39 :     final Object? value = this[key];
      76           39 :     if (value is! List) {
      77           70 :       log(key, T, value.runtimeType);
      78              :       return null;
      79              :     }
      80              :     try {
      81              :       // copy entries to ensure type check failures here and not an access
      82           78 :       return value.cast<T>().toList();
      83              :     } catch (_) {
      84            0 :       Logs().v(
      85            0 :         'Unable to create "List<$T>" in event content for the key "$key" at ${StackTrace.current.firstLine}',
      86              :       );
      87              :       return null;
      88              :     }
      89              :   }
      90              : 
      91           40 :   Map<A, B>? tryGetMap<A, B>(String key, [TryGet log = TryGet.optional]) {
      92           40 :     final Object? value = this[key];
      93           40 :     if (value is! Map) {
      94          144 :       log(key, <A, B>{}.runtimeType, value.runtimeType);
      95              :       return null;
      96              :     }
      97              :     try {
      98              :       // copy map to ensure type check failures here and not an access
      99           80 :       return Map.from(value.cast<A, B>());
     100              :     } catch (_) {
     101            0 :       Logs().v(
     102            0 :         'Unable to create "Map<$A,$B>" in event content for the key "$key" at ${StackTrace.current.firstLine}',
     103              :       );
     104              :       return null;
     105              :     }
     106              :   }
     107              : 
     108           36 :   A? tryGetFromJson<A>(
     109              :     String key,
     110              :     A Function(Map<String, Object?>) fromJson, [
     111              :     TryGet log = TryGet.optional,
     112              :   ]) {
     113           36 :     final value = tryGetMap<String, Object?>(key, log);
     114              : 
     115           35 :     return value != null ? fromJson(value) : null;
     116              :   }
     117              : }
     118              : 
     119              : extension on StackTrace {
     120            8 :   String get firstLine {
     121           16 :     final lines = toString().split('\n');
     122           16 :     return lines.length >= 3
     123           16 :         ? lines[2].replaceFirst('#2      ', '')
     124            0 :         : lines.isNotEmpty
     125            0 :             ? lines.first
     126              :             : '(unknown position)';
     127              :   }
     128              : }
        

Generated by: LCOV version 2.0-1