anonymous-dev
anonymous-dev

Reputation: 3529

How to get Either type arguments with source_gen

I have the following class

@Repository()
abstract class UserRepository {
  Future<Either<Failure, EntityA>> getUser(String id);
}

When using dart's analyzer trough source_gen with the Visitor Pattern I found the the MethodElement's returnType name is not Future<Either<Failure,EntityA>> but instead Future<Either<InvalidType, InvalidType>>. Why? Also the actual dart type parameters contain InvalidTypes. How can I get the actual types Failure and EntityA?

The only way I'm able to get it now is trough the source by using

  String? parseMethodEitherReturnType(String source, String methodName) {
    final mapRegex = RegExp(r'Future<Either<(.+),\s*(.+)>> ' + methodName);

    if (!mapRegex.hasMatch(source)) {
      return null;
    }

    final match1 = mapRegex.firstMatch(source)?.group(1);
    final match2 = mapRegex.firstMatch(source)?.group(2);

    if (match1 == null || match2 == null) {
      return null;
    }

    return 'Future<Either<$match1, $match2>>';
  }

But this is not ideal ofcourse.

Upvotes: 0

Views: 46

Answers (0)

Related Questions