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

Allow inherrited BuiltValueHooks #1310

Open
Leptopoda opened this issue Apr 11, 2024 · 1 comment
Open

Allow inherrited BuiltValueHooks #1310

Leptopoda opened this issue Apr 11, 2024 · 1 comment
Assignees

Comments

@Leptopoda
Copy link
Contributor

When making use of interfaces one might wish to add hooks to it that should be invoked by the subclasses.
An example is when setting default (initializeHook) and validating the builder (finalizeHook).
Currently one has to manually add the builders to the subclasses.
Is this a feature you are interested in?

@BuiltValue(instantiable: false)
abstract interface class TestInterface {
  String get value;
  @BuiltValueHook(initializeBuilder: true)
  static void _defaults(TestInterfaceBuilder b) {
    b.value = '123';
  }

  @BuiltValueHook(finalizeBuilder: true)
  static void _validate(TestInterfaceBuilder b) {
    if (b.value!.isEmpty) {
      throw Error();
    }
  }
}

abstract class Test implements TestInterface, Built<Test, TestBuilder> {
  factory Test([void Function(TestBuilder)? b]) = _$SuperObject;
  const Test._();

  static Serializer<Test> get serializer => _$superObjectSerializer;

  @BuiltValueHook(initializeBuilder: true)
  static void _defaults(TestBuilder b) {
    TestInterface._defaults(b);
  }

  @BuiltValueHook(finalizeBuilder: true)
  static void _validate(TestBuilder b) {
    TestInterface._validate(b);
  }
}
@davidmorgan
Copy link
Collaborator

Thanks for filing! Sorry for the slow response, I've been on vacation.

That does seem like it makes sense, unfortunately I suspect it's only landable as a breaking change or via extra configuring (inheritBuilders: true), neither of which is ideal.

I'll put it in the pile of possible changes for a closer look at some point, but I don't have a lot of bandwidth for built_value improvements so I suspect it will be a while before I get to it.

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