LCOV - code coverage report
Current view: top level - utils - importable_type_to_string.dart (source / functions) Coverage Total Hit
Test: lcov.info Lines: 95.3 % 43 41
Test Date: 2024-07-24 10:28:19 Functions: - 0 0

            Line data    Source code
       1              : import 'package:flutter_navigation_generator/src/models/importable_type.dart';
       2              : import 'package:flutter_navigation_generator/src/utils/utils.dart';
       3              : 
       4              : class ImportableTypeStringConverter {
       5            2 :   static String convertFromString(ImportableType p, String s) {
       6            2 :     return switch (p.className) {
       7            4 :       'int' => 'int.parse($s)',
       8            3 :       'double' => 'double.parse($s)',
       9            4 :       'bool' => "$s == 'true'",
      10            2 :       'num' => 'num.parse($s)',
      11            2 :       'String' || 'dynamic' => s,
      12            1 :       'List' =>
      13            4 :         '(jsonDecode(utf8.decode(base64Decode($s))) as List<dynamic>).map((e) => ${convertTypeArguments(p.typeArguments.first)}).toList()',
      14            2 :       'Map' => convertMap(p, s),
      15              :       _ =>
      16            3 :         '${typeRefer(p).symbol}.fromJson(jsonDecode(utf8.decode(base64Decode($s))))',
      17              :     };
      18              :   }
      19              : 
      20            1 :   static String convertTypeArguments(ImportableType p) {
      21            1 :     final suffix = p.isNullable ? '?' : '';
      22            1 :     return switch (p.className) {
      23            1 :       'int' ||
      24            1 :       'double' ||
      25            1 :       'bool' ||
      26            1 :       'num' ||
      27            1 :       'String' =>
      28            2 :         'e as ${p.className}$suffix',
      29            1 :       'dynamic' => 'e',
      30            1 :       'Map' => convertMap(p, ('e')),
      31            1 :       'List' =>
      32            0 :         '(e as List<dynamic>).map((e) => ${convertTypeArguments(p.typeArguments.first)}).toList()',
      33              :       _ =>
      34            3 :         '${p.isNullable ? 'e == null ? null : ' : ''}${p.className}.fromJson(e as Map<String, dynamic>)',
      35              :     };
      36              :   }
      37              : 
      38            1 :   static String convertMap(ImportableType p, String s) {
      39            2 :     final typeKey = p.typeArguments.first;
      40            2 :     final typeValue = p.typeArguments[1];
      41              :     final mapFrom =
      42            5 :         'Map<${typeKey.className}${typeKey.isNullable ? '?' : ''}, ${typeValue.className}${typeValue.isNullable ? '?' : ''}>.from';
      43            1 :     final decode = 'jsonDecode(utf8.decode(base64Decode($s)))';
      44            1 :     final keyTransformer = convertMapTransformer(typeKey, 'k');
      45            1 :     final valueTransformer = convertMapTransformer(typeValue, 'v');
      46            4 :     if (keyTransformer.length == 1 && valueTransformer.length == 1) {
      47            1 :       return '$mapFrom($decode)';
      48              :     }
      49            1 :     return '$mapFrom($decode.map((k, v) => MapEntry($keyTransformer, $valueTransformer)))';
      50              :   }
      51              : 
      52            1 :   static String convertMapTransformer(ImportableType p, String s) {
      53            1 :     return switch (p.className) {
      54            6 :       'int' || 'double' || 'bool' || 'num' || 'String' || 'dynamic' => s,
      55            1 :       'Map' =>
      56            3 :         '${p.isNullable ? '$s == null ? null : ' : ''}${convertMap(p, s)}',
      57            1 :       'List' =>
      58            0 :         '${p.isNullable ? '$s == null ? null : ' : ''}($s as List<dynamic>).map((e) => ${convertTypeArguments(p.typeArguments.first)}).toList()',
      59              :       _ =>
      60            4 :         '${p.isNullable ? '$s == null ? null : ' : ''}${p.className}.fromJson($s as Map<String, dynamic>)',
      61              :     };
      62              :   }
      63              : }
        

Generated by: LCOV version 2.0-1