Skip to content

Allow nested classes in freezed classes #17

Description

@buraktabn

Let's say we have a Tweet class that uses Reply as a field. This works on non freezed classes

@json
class Tweet {
  DateTime? created_at;

  String? tweet;

  Reply? reply;
}

@json
class Reply {
  DateTime? created_at;

  String? reply;
}

When I convert this to freezed class I see that generated readTweet looking for fromJson methods in Reply which not exists.

@freezed
class Tweet with _$Tweet {
  @json
  const factory Tweet({
    DateTime? created_at,
    String? tweet,
    Reply? reply
  }) = _Tweet;
}

@freezed
class Reply with _$Reply {
  @json
  const factory Reply({
    DateTime? created_at,
    String? reply,
  }) = _Reply;
}
// ---- .g ----
extension ReadTweet on Crimson {
  Tweet readTweet() {
    DateTime? created_at;
    String? tweet;
    Reply? reply;

    loop:
    while (true) {
      switch (iterObjectHash()) {
        case -1:
          break loop;

        case -3141059728106243719: // created_at
          created_at = skipNull() ? null : DateTime.parse(readString());
          break;
        case -5949187214024354806: // tweet
          tweet = readStringOrNull();
          break;
        case -777431743796252409: // reply
          reply = Reply.fromJson(read());
          break;

        default:
          skip();
          break;
      }
    }
    ...
  }
  ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions