LCOV - code coverage report
Current view: top level - lib/domain - field.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 21 23 91.3 %
Date: 2022-01-14 16:05:24 Functions: 0 0 -

          Line data    Source code
       1             : // ignore_for_file: implementation_imports
       2             : 
       3             : import 'package:analyzer/dart/element/element.dart';
       4             : import 'package:analyzer/src/dart/element/element.dart';
       5             : import 'package:class_fields_annotation/class_fields_annotation.dart';
       6             : import 'package:source_gen/source_gen.dart';
       7             : 
       8             : /// {@template field}
       9             : /// The field of the class
      10             : /// {@endtemplate}
      11             : class Field {
      12             :   /// {@template field}
      13           1 :   const Field({
      14             :     required this.name,
      15             :     required String? key,
      16             :   }) : key = key ?? name;
      17             : 
      18             :   /// gets the field from the [FieldElement]
      19           1 :   factory Field.fromElement(FieldElement element) {
      20           1 :     String? checkForKey(Iterable<ElementAnnotation> annotations) {
      21           1 :       if (annotations.isEmpty) {
      22             :         return null;
      23             :       }
      24             : 
      25           1 :       String? getName(ElementAnnotation element, String field) {
      26           2 :         final reader = ConstantReader(element.computeConstantValue());
      27             : 
      28           2 :         final result = reader.peek(field)?.literalValue as String?;
      29             : 
      30             :         return result;
      31             :       }
      32             : 
      33           2 :       for (final annotation in annotations) {
      34           2 :         if (annotation.astName == 'JsonKey') {
      35           1 :           return getName(annotation, 'name');
      36           3 :         } else if (annotation.astName == '$Field') {
      37           1 :           return getName(annotation, 'key');
      38             :         }
      39             :       }
      40             :     }
      41             : 
      42           1 :     return Field(
      43           1 :       name: element.name,
      44           2 :       key: checkForKey(element.metadata),
      45             :     );
      46             :   }
      47             : 
      48             :   /// the name of the field
      49             :   final String name;
      50             : 
      51             :   /// the name of the field, removing all `_`
      52           1 :   String get cleanName {
      53           3 :     return name.replaceAll(RegExp('^_*'), '');
      54             :   }
      55             : 
      56             :   /// the key of the field
      57             :   ///
      58             :   /// set to [name] if not set
      59             :   final String key;
      60             : 
      61             :   /// gets all the fields from the [FieldElement]s
      62             :   static Iterable<Field> fromElements(Iterable<FieldElement> elements) sync* {
      63             :     if (elements.isEmpty) {
      64             :       return;
      65             :     }
      66             : 
      67             :     for (final element in elements) {
      68             :       // [element.isSynthetic] is true for fields that are
      69             :       // declared getters
      70             :       // Ignore all static fields
      71             :       if (element.isSynthetic || element.isStatic) {
      72             :         continue;
      73             :       }
      74             : 
      75             :       yield Field.fromElement(element);
      76             :     }
      77             :   }
      78             : }
      79             : 
      80             : extension on ElementAnnotation {
      81           1 :   String get astName {
      82           1 :     if (element == null) {
      83           0 :       throw ArgumentError.notNull('element');
      84             :     }
      85             : 
      86           1 :     if (this is! ElementAnnotationImpl) {
      87           0 :       return element!.displayName;
      88             :     }
      89             : 
      90           3 :     return (this as ElementAnnotationImpl).annotationAst.name.name;
      91             :   }
      92             : }

Generated by: LCOV version 1.15