- 
                Notifications
    You must be signed in to change notification settings 
- Fork 111
Add Reviver #679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            Nexushunter
  wants to merge
  4
  commits into
  dart-lang:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
Nexushunter:reviver
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
      
        
          +328
        
        
          −2
        
        
          
        
      
    
  
  
     Open
                    Add Reviver #679
Changes from all commits
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,3 +1,42 @@ | ||
| /// A sample library for the Annotations used during testing. | ||
| library _test_annotations; | ||
|  | ||
| class TestAnnotation { | ||
| const TestAnnotation(); | ||
| } | ||
|  | ||
| class TestAnnotationWithComplexObject { | ||
| final ComplexObject object; | ||
| const TestAnnotationWithComplexObject(this.object); | ||
| } | ||
|  | ||
| class TestAnnotationWithSimpleObject { | ||
| final SimpleObject obj; | ||
| const TestAnnotationWithSimpleObject(this.obj); | ||
| } | ||
|  | ||
| class SimpleObject { | ||
| final int i; | ||
| const SimpleObject(this.i); | ||
| } | ||
|  | ||
| class ComplexObject { | ||
| final SimpleObject sObj; | ||
| final CustomEnum? cEnum; | ||
| final Map<String, ComplexObject>? cMap; | ||
| final List<ComplexObject>? cList; | ||
| final Set<ComplexObject>? cSet; | ||
| const ComplexObject( | ||
| this.sObj, { | ||
| this.cEnum, | ||
| this.cMap, | ||
| this.cList, | ||
| this.cSet, | ||
| }); | ||
| } | ||
|  | ||
| enum CustomEnum { | ||
| v1, | ||
| v2, | ||
| v3; | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| ## 1.4.1-wip | ||
| ## 1.5.0-wip | ||
| - Add a `Reviver` class that hydrates a `ConstantReader` or `DartObject?` | ||
|  | ||
| ## 1.4.0 | ||
|  | ||
|  | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| import 'dart:mirrors'; | ||
|  | ||
| import 'package:analyzer/dart/constant/value.dart'; | ||
|  | ||
| import '../type_checker.dart'; | ||
| import 'reader.dart'; | ||
|  | ||
| /// Revives a [ConstantReader] to an instance in memory using Dart mirrors. | ||
| /// | ||
| /// Converts a serialized [DartObject] and transforms it into a fully qualified | ||
| /// instance of the object to be consumed. | ||
| /// | ||
| /// An intended usage of this is to provide those creating Generators a simpler | ||
| /// way to initialize their annotations as in memory instances. This allows for | ||
| /// cleaner and smaller implementations that don't have an underlying knowledge | ||
| /// of the [ConstantReader]. This simplfies cases like the following: | ||
| /// | ||
| /// ```dart | ||
| /// // Defined within a builder library and exposed for consumers to extend. | ||
| /// /// This [Delegator] delegates some complex processing. | ||
| /// abstract class Delegator<T> { | ||
| /// const Delegator(); | ||
| /// | ||
| /// T call([dynamic]); | ||
| /// } | ||
| /// | ||
| /// // Consumer library | ||
| /// /// My CustomDelegate callable to be used in a builder | ||
| /// class CustomDelegate implements Delegator<ReturnType> { | ||
| /// const CustomDelegate(); | ||
| /// | ||
| /// @override | ||
| /// ReturnType call(Map<String,String> args) async { | ||
| /// // My implementation details. | ||
| /// } | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// Where a library exposes an interface that the user is to implement by | ||
| /// the library doesn't need to know all of the implementation details. | ||
| class Reviver { | ||
| final ConstantReader reader; | ||
| const Reviver(this.reader); | ||
| Reviver.fromDartObject(DartObject? object) : this(ConstantReader(object)); | ||
|  | ||
| /// Recurively build the instance and return it. | ||
| /// | ||
| /// This may return null when the declaration doesn't exist within the | ||
| /// system or the [reader] is null. | ||
| /// | ||
| /// In the event the reader is a primative type it returns that value. | ||
| /// Collections are iterated and revived. | ||
| /// Otherwise a fully qualified instance is returned. | ||
| dynamic toInstance() { | ||
| if (reader.isPrimative) { | ||
| return primativeValue; | ||
| } else if (reader.isCollection) { | ||
| if (reader.isList) { | ||
| // ignore: omit_local_variable_types | ||
| Type t = dynamic; | ||
| if (reader.listValue.isNotEmpty) { | ||
| // ignore: avoid_dynamic_calls | ||
| t = Reviver.fromDartObject(reader.listValue.first) | ||
| .toInstance() | ||
| .runtimeType; | ||
| } | ||
| return toTypedList(t); | ||
| } else if (reader.isSet) { | ||
| // ignore: omit_local_variable_types | ||
| Type t = dynamic; | ||
| if (reader.setValue.isNotEmpty) { | ||
| // ignore: avoid_dynamic_calls | ||
| t = Reviver.fromDartObject(reader.setValue.first) | ||
| .toInstance() | ||
| .runtimeType; | ||
| } | ||
| return toTypedSet(t); | ||
| } else { | ||
| // ignore: omit_local_variable_types | ||
| Type kt = dynamic; | ||
| // ignore: omit_local_variable_types | ||
| Type vt = dynamic; | ||
| if (reader.mapValue.isNotEmpty) { | ||
| // ignore: avoid_dynamic_calls | ||
| kt = Reviver.fromDartObject(reader.mapValue.keys.first) | ||
| .toInstance() | ||
| .runtimeType; | ||
| // ignore: avoid_dynamic_calls | ||
| vt = Reviver.fromDartObject(reader.mapValue.values.first) | ||
| .toInstance() | ||
| .runtimeType; | ||
| } | ||
| return toTypedMap(kt, vt); | ||
| } | ||
| } else if (reader.isLiteral) { | ||
| return reader.literalValue; | ||
| } else if (reader.isType) { | ||
| return reader.typeValue; | ||
| } else if (reader.isSymbol) { | ||
| return reader.symbolValue; | ||
| } else { | ||
| final decl = classMirror; | ||
| if (decl.isEnum) { | ||
| final values = decl.getField(const Symbol('values')).reflectee as List; | ||
| return values[reader.objectValue.getField('index')!.toIntValue()!]; | ||
| } | ||
|  | ||
| final pv = positionalValues; | ||
| final nv = namedValues; | ||
|  | ||
| return decl | ||
| .newInstance(Symbol(reader.revive().accessor), pv, nv) | ||
| .reflectee; | ||
| } | ||
| } | ||
|  | ||
| dynamic get primativeValue { | ||
| if (reader.isNull) { | ||
| return null; | ||
| } else if (reader.isBool) { | ||
| return reader.boolValue; | ||
| } else if (reader.isDouble) { | ||
| return reader.doubleValue; | ||
| } else if (reader.isInt) { | ||
| return reader.intValue; | ||
| } else if (reader.isString) { | ||
| return reader.stringValue; | ||
| } | ||
| } | ||
|  | ||
| List<dynamic> get positionalValues => reader | ||
| .revive() | ||
| .positionalArguments | ||
| .map( | ||
| (value) => Reviver.fromDartObject(value).toInstance(), | ||
| ) | ||
| .toList(); | ||
|  | ||
| Map<Symbol, dynamic> get namedValues => reader.revive().namedArguments.map( | ||
| (key, value) { | ||
| final k = Symbol(key); | ||
| final v = Reviver.fromDartObject(value).toInstance(); | ||
| return MapEntry(k, v); | ||
| }, | ||
| ); | ||
|  | ||
| ClassMirror get classMirror { | ||
| final revivable = reader.revive(); | ||
|  | ||
| // Flatten the list of libraries | ||
| final entries = Map.fromEntries(currentMirrorSystem().libraries.entries) | ||
| .map((key, value) => MapEntry(key.pathSegments.first, value)); | ||
|  | ||
| // Grab the library from the system | ||
| final libraryMirror = entries[revivable.source.pathSegments.first]; | ||
| if (libraryMirror == null || libraryMirror.simpleName == Symbol.empty) { | ||
| throw Exception('Library missing'); | ||
| } | ||
|  | ||
| // Determine the declaration being requested. Split on . when an enum is passed in. | ||
| var declKey = Symbol(revivable.source.fragment); | ||
| if (reader.isEnum) { | ||
| // The accessor when the entry is an enum is the ClassName.value | ||
| declKey = Symbol(revivable.accessor.split('.')[0]); | ||
| } | ||
|  | ||
| final decl = libraryMirror.declarations[declKey] as ClassMirror?; | ||
| if (decl == null) { | ||
| throw Exception('Declaration missing'); | ||
| } | ||
| return decl; | ||
| } | ||
|  | ||
| List<T>? toTypedList<T>(T t) => reader.listValue | ||
| .map((e) => Reviver.fromDartObject(e).toInstance() as T) | ||
| .toList() as List<T>?; | ||
|  | ||
| Map<KT, VT>? toTypedMap<KT, VT>(KT kt, VT vt) => reader.mapValue.map( | ||
| (key, value) => MapEntry( | ||
| Reviver.fromDartObject(key).toInstance() as KT, | ||
| Reviver.fromDartObject(value).toInstance() as VT, | ||
| ), | ||
| ) as Map<KT, VT>?; | ||
|  | ||
| Set<T>? toTypedSet<T>(T t) => reader.setValue | ||
| .map((e) => Reviver.fromDartObject(e).toInstance() as T) | ||
| .toSet() as Set<T>?; | ||
| } | ||
|  | ||
| extension IsChecks on ConstantReader { | ||
| bool get isCollection => isList || isMap || isSet; | ||
| bool get isEnum => instanceOf(const TypeChecker.fromRuntime(Enum)); | ||
| bool get isPrimative => isBool || isDouble || isInt || isString || isNull; | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import 'package:_test_annotations/test_annotations.dart'; | ||
| import 'package:build_test/build_test.dart'; | ||
| import 'package:source_gen/src/constants/reader.dart'; | ||
| import 'package:source_gen/src/constants/reviver.dart'; | ||
| import 'package:test/test.dart'; | ||
|  | ||
| void main() { | ||
| group('Reviver', () { | ||
| group('revives classes', () { | ||
| group('returns qualified class', () { | ||
| const declSrc = r''' | ||
| library test_lib; | ||
|  | ||
| import 'package:_test_annotations/test_annotations.dart'; | ||
|  | ||
| @TestAnnotation() | ||
| class TestClassSimple {} | ||
|  | ||
| @TestAnnotationWithComplexObject(ComplexObject(SimpleObject(1))) | ||
| class TestClassComplexPositional {} | ||
|  | ||
| @TestAnnotationWithComplexObject(ComplexObject(SimpleObject(1), cEnum: CustomEnum.v2, cMap: <String,ComplexObject>{'1':ComplexObject(SimpleObject(1)),'2':ComplexObject(SimpleObject(2)),'fred':ComplexObject(SimpleObject(3))}, cList: <ComplexObject>[ComplexObject(SimpleObject(1))], cSet: <ComplexObject>{ComplexObject(SimpleObject(1)),ComplexObject(SimpleObject(2))})) | ||
| class TestClassComplexPositionalAndNamed {} | ||
| '''; | ||
| test('with simple objects', () async { | ||
| final reader = (await resolveSource( | ||
| declSrc, | ||
| (resolver) async => (await resolver.findLibraryByName('test_lib'))!, | ||
| )) | ||
| .getClass('TestClassSimple')! | ||
| .metadata | ||
| .map((e) => ConstantReader(e.computeConstantValue()!)) | ||
| .toList() | ||
| .first; | ||
|  | ||
| final reviver = Reviver(reader); | ||
| final instance = reviver.toInstance(); | ||
| expect(instance, isNotNull); | ||
| expect(instance, isA<TestAnnotation>()); | ||
| }); | ||
|  | ||
| for (final s in ['Positional', 'PositionalAndNamed']) { | ||
| test('with complex objects: $s', () async { | ||
| final reader = (await resolveSource( | ||
| declSrc, | ||
| (resolver) async => | ||
| (await resolver.findLibraryByName('test_lib'))!, | ||
| )) | ||
| .getClass('TestClassComplex$s')! | ||
| .metadata | ||
| .map((e) => ConstantReader(e.computeConstantValue()!)) | ||
| .toList() | ||
| .first; | ||
|  | ||
| final reviver = Reviver(reader); | ||
| final instance = reviver.toInstance(); | ||
| expect(instance, isNotNull); | ||
| expect(instance, isA<TestAnnotationWithComplexObject>()); | ||
| instance as TestAnnotationWithComplexObject; | ||
|  | ||
| expect(instance.object, isNotNull); | ||
| expect(instance.object.sObj.i, 1); | ||
|  | ||
| if (s == 'PositionalAndNamed') { | ||
| expect(instance.object.cEnum, isNotNull); | ||
| expect(instance.object.cEnum, CustomEnum.v2); | ||
|  | ||
| expect(instance.object.cList, isNotNull); | ||
| expect(instance.object.cList!, const <ComplexObject>[ | ||
| ComplexObject(SimpleObject(1)), | ||
| ]); | ||
|  | ||
| expect(instance.object.cMap, isNotNull); | ||
| expect(instance.object.cMap!, const <String, ComplexObject>{ | ||
| '1': ComplexObject(SimpleObject(1)), | ||
| '2': ComplexObject(SimpleObject(2)), | ||
| 'fred': ComplexObject(SimpleObject(3)), | ||
| }); | ||
|  | ||
| expect(instance.object.cSet, isNotNull); | ||
| expect(instance.object.cSet!, const <ComplexObject>{ | ||
| ComplexObject(SimpleObject(1)), | ||
| ComplexObject(SimpleObject(2)), | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
| } | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trying to leverage these results in the following stack trace:
This feels wrong. The reason I'm doing this is because there is a limitation of not being able to do: