Hello,
this is an improvement in the operation of the default result.
Indeed I carried out a test to detail my remarks.
The default result works well for primitive types, however for complex object, it cannot work properly.
The reset function does not reset to the default state. It uses the referenced value. And therefore from one execution to another the result may not be reset correctly.
The improvement consists in passing in parameter of the default result a supplier function which will be executed at each reset which allows to correctly define the default result without alteration of it. The reference will change each time the reset function is called. "It's a real reset"
@Test
public void getResultFailedWithDefaultValueSetToAnObject() {
ComplexObject complexObject = new ComplexObject();
Result<ComplexObject> result = new Result<>(complexObject);
result.getValue()._choices.add("Bad choices");
complexObject._value = -1.0;
Assert.assertSame(result.getValue(), complexObject);
result.reset();
// Still same !
Assert.assertSame(result.getValue(), complexObject);
}
@Test
public void getResultWithSupplierAndDefaultValueSetToAnObject() {
Result<ComplexObject> result = new Result<>(ComplexObject::new);
result.getValue()._choices.add("Bad choices");
ComplexObject complexObject = result.getValue();
result.reset();
// Not same !
Assert.assertNotSame(result.getValue(), complexObject);
}
Hello, this is an improvement in the operation of the default result. Indeed I carried out a test to detail my remarks.
The default result works well for primitive types, however for complex object, it cannot work properly.
The reset function does not reset to the default state. It uses the referenced value. And therefore from one execution to another the result may not be reset correctly.
The improvement consists in passing in parameter of the default result a supplier function which will be executed at each reset which allows to correctly define the default result without alteration of it. The reference will change each time the reset function is called. "It's a real reset"
https://github.com/deliveredtechnologies/rulebook/pull/193