Skip to content
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

Auto add builder factories for nested built_collection classes #1254

Open
JosephNK opened this issue Jun 3, 2023 · 2 comments
Open

Auto add builder factories for nested built_collection classes #1254

JosephNK opened this issue Jun 3, 2023 · 2 comments
Assignees

Comments

@JosephNK
Copy link

JosephNK commented Jun 3, 2023

When using BuiltMap nesting, is it not automatically created?

abstract class Calendar implements Built<Calendar, CalendarBuilder> { 
  ...
  @BuiltValueField(wireName: 'calendar')
    BuiltMap<String, BuiltMap<String, BuiltList<DayDTO>>>? get calendar;
  ...
}

automatically generated below

..addBuilderFactory(
          const FullType(BuiltMap, const [
            const FullType(String),
            const FullType(BuiltMap, const [
              const FullType(String),
              const FullType(
                  BuiltList, const [const FullType(DayDTO)])
            ])
          ]),
          () => new MapBuilder<String,
              BuiltMap<String, BuiltList<DayDTO>>>())

however, the below needs to be added manually.

..addBuilderFactory(
          const FullType(BuiltMap, const [
            const FullType(String),
            const FullType(
                BuiltList, const [const FullType(DayDTO)])
          ]),
          () => new MapBuilder<String, BuiltList<DayDTO>>())
..addBuilderFactory(
        const FullType(
            BuiltList, const [const FullType(DayDTO)]),
        () => new ListBuilder<DayDTO>())

It won't work unless you add it manually in the code above. So you have to add it manually, but it works.

Is there a way to automatically generate it?

@JosephNK
Copy link
Author

JosephNK commented Jun 3, 2023

I added the following code for auto-generation.

Is it correct to use it like this?

abstract class Calendar implements Built<Calendar, CalendarBuilder> { 
  ...
  @BuiltValueField(wireName: 'calendar')
    BuiltMap<String, BuiltMap<String, BuiltList<DayDTO>>>? get calendar;
  ...
  BuiltMap<String, BuiltList<DayDTO>>? get autogen01; <-- added
  BuiltList<DayDTO>? get autogen02;  <-- added
}

@davidmorgan
Copy link
Collaborator

Yes, it's necessary to add builder factories manually in some cases.

It's true that you could add fields as in your second post so the builder factories get generated for you, but then you will end up with a very weird class :) ... maybe you could put all such fields in a single class that you don't use, as a more convenient way to add builder factories.

Sorry that this part of built_value is so awkward, there is a long standing feature request to improve it but I haven't had a chance to tackle it!

@davidmorgan davidmorgan self-assigned this Jun 5, 2023
@davidmorgan davidmorgan changed the title When using BuiltMap nesting, is it not automatically created? Auto add builder factories for nested built_collection classes Dec 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants