Closed making closed 4 months ago
Hi @making,
thanks for opening the issue!
Currently, with the builder pattern in Jilt, the only option is to return an instance of the target class (e.g.,
Car
) directly.
Not sure what part of the documentation gave you that impression, but that's not correct - you can return any class from your Builder that you want!
So, using your example, if you do this:
@Builder(className = "CarBuilder", factoryMethod = "car")
public static Validated<Car> validate(String manufacturer, String licensePlate, Integer seatCount) {
return Car.validator.validate(manufacturer, licensePlate, seatCount);
}
It will generate a Builder that can be used exactly like you wanted:
Validated<Car> validatedCar = CarBuilder.car()
.manufacturer("Morris")
.licensePlate("DD-AB-123")
.seatCount(2)
.build();
Let me know if this makes sense!
Thanks, Adam
Thanks! It seems I misunderstood something by looking at the source code that was generated when I tried something. I was able to get a great integration!
Of course, glad you got it working!
We have a use case where I need to validate arguments using a validator before creating an instance of a class with a builder. Using YAVI, I can achieve this by validating the arguments and representing the validated state with a
Validated
class (similar toOptional
).Here's an example with YAVI:
Currently, with the builder pattern in Jilt, the only option is to return an instance of the target class (e.g.,
Car
) directly. However, I would like the option to return an instance of a wrapper class likeValidated<Car>
instead of the target class.The usage would look like this (?):