LCOV - code coverage report
Current view: top level - lib/matrix_api_lite/utils - try_get_map_extension.dart (source / functions) Hit Total Coverage
Test: merged.info Lines: 32 38 84.2 %
Date: 2024-07-12 20:20:16 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          82 :   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             : class _OptionalLog implements TryGet {
      46          82 :   const _OptionalLog();
      47          40 :   @override
      48             :   void call(String key, Type expected, Type actual) {
      49          40 :     if (actual != Null) {
      50          10 :       Logs().w(
      51          15 :           'Expected optional "$expected" in event content for the Key "$key" but got "$actual" at ${StackTrace.current.firstLine}');
      52             :     }
      53             :   }
      54             : }
      55             : 
      56             : class _SilentLog implements TryGet {
      57          82 :   const _SilentLog();
      58          26 :   @override
      59             :   void call(String key, Type expected, Type actual) {}
      60             : }
      61             : 
      62             : extension TryGetMapExtension on Map<String, Object?> {
      63          42 :   T? tryGet<T extends Object>(String key, [TryGet log = TryGet.optional]) {
      64          42 :     final Object? value = this[key];
      65          42 :     if (value is! T) {
      66          80 :       log(key, T, value.runtimeType);
      67             :       return null;
      68             :     }
      69             :     return value;
      70             :   }
      71             : 
      72          37 :   List<T>? tryGetList<T>(String key, [TryGet log = TryGet.optional]) {
      73          37 :     final Object? value = this[key];
      74          37 :     if (value is! List) {
      75          66 :       log(key, T, value.runtimeType);
      76             :       return null;
      77             :     }
      78             :     try {
      79             :       // copy entries to ensure type check failures here and not an access
      80          74 :       return value.cast<T>().toList();
      81             :     } catch (_) {
      82           0 :       Logs().v(
      83           0 :           'Unable to create "List<$T>" in event content for the key "$key" at ${StackTrace.current.firstLine}');
      84             :       return null;
      85             :     }
      86             :   }
      87             : 
      88          40 :   Map<A, B>? tryGetMap<A, B>(String key, [TryGet log = TryGet.optional]) {
      89          40 :     final Object? value = this[key];
      90          40 :     if (value is! Map) {
      91         144 :       log(key, <A, B>{}.runtimeType, value.runtimeType);
      92             :       return null;
      93             :     }
      94             :     try {
      95             :       // copy map to ensure type check failures here and not an access
      96          80 :       return Map.from(value.cast<A, B>());
      97             :     } catch (_) {
      98           0 :       Logs().v(
      99           0 :           'Unable to create "Map<$A,$B>" in event content for the key "$key" at ${StackTrace.current.firstLine}');
     100             :       return null;
     101             :     }
     102             :   }
     103             : 
     104          34 :   A? tryGetFromJson<A>(String key, A Function(Map<String, Object?>) fromJson,
     105             :       [TryGet log = TryGet.optional]) {
     106          34 :     final value = tryGetMap<String, Object?>(key, log);
     107             : 
     108          33 :     return value != null ? fromJson(value) : null;
     109             :   }
     110             : }
     111             : 
     112             : extension on StackTrace {
     113           6 :   String get firstLine {
     114          12 :     final lines = toString().split('\n');
     115          12 :     return lines.length >= 3
     116          12 :         ? lines[2].replaceFirst('#2      ', '')
     117           0 :         : lines.isNotEmpty
     118           0 :             ? lines.first
     119             :             : '(unknown position)';
     120             :   }
     121             : }

Generated by: LCOV version 1.14