Skip to content

🐛 fix missing boolean field accessor for generated objects #219

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

Merged
merged 3 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/java/com/mindee/parsing/generated/GeneratedFeature.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mindee.MindeeException;
import com.mindee.parsing.standard.AmountField;
import com.mindee.parsing.standard.BooleanField;
import com.mindee.parsing.standard.ClassificationField;
import com.mindee.parsing.standard.DateField;
import com.mindee.parsing.standard.StringField;
Expand Down Expand Up @@ -34,6 +35,7 @@ public class GeneratedFeature extends ArrayList<GeneratedObject> {

/**
* Whether the original feature is a list.
* @param isList Whether the feature is a list.
*/
public GeneratedFeature(boolean isList) {
this.isList = isList;
Expand All @@ -42,6 +44,7 @@ public GeneratedFeature(boolean isList) {
/**
* Represent the feature as a standard {@link StringField}.
* Only works for non-list features.
* @return An instance of a {@link StringField}.
*/
public StringField asStringField() {
if (this.isList) {
Expand All @@ -50,9 +53,20 @@ public StringField asStringField() {
return this.get(0).asStringField();
}

/**
* @return An instance of a {@link BooleanField}.
*/
public BooleanField asBooleanField() {
if (this.isList) {
throw new MindeeException("Cannot convert a list feature into a BooleanField.");
}
return this.get(0).asBooleanField();
}

/**
* Represent the feature as a standard {@link AmountField}.
* Only works for non-list features.
* @return An instance of a {@link AmountField}.
*/
public AmountField asAmountField() {
if (this.isList) {
Expand All @@ -64,6 +78,7 @@ public AmountField asAmountField() {
/**
* Represent the feature as a standard {@link DateField}.
* Only works for non-list features.
* @return An instance of a {@link DateField}.
*/
public DateField asDateField() {
if (this.isList) {
Expand All @@ -75,6 +90,7 @@ public DateField asDateField() {
/**
* Represent the feature as a standard {@link ClassificationField}.
* Only works for non-list features.
* @return An instance of a {@link ClassificationField}.
*/
public ClassificationField asClassificationField() {
if (this.isList) {
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/mindee/parsing/generated/GeneratedObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mindee.geometry.Polygon;
import com.mindee.geometry.PolygonUtils;
import com.mindee.parsing.standard.AmountField;
import com.mindee.parsing.standard.BooleanField;
import com.mindee.parsing.standard.ClassificationField;
import com.mindee.parsing.standard.DateField;
import com.mindee.parsing.standard.StringField;
Expand All @@ -17,6 +18,7 @@ public class GeneratedObject extends HashMap<String, Object> {

/**
* Represent the object as a standard {@link StringField}.
* @return A {@link StringField} containing a string value.
*/
public StringField asStringField() {
return new StringField(
Expand All @@ -30,6 +32,7 @@ public StringField asStringField() {

/**
* Represent the object as a standard {@link AmountField}.
* @return An {@link AmountField} containing a double value.
*/
public AmountField asAmountField() {
Double value;
Expand All @@ -51,8 +54,21 @@ else if (rawValue instanceof Double) {
);
}

/**
* Represent the object as a standard {@link BooleanField}.
* @return A {@link BooleanField} containing a boolean value.
*/
public BooleanField asBooleanField() {
Object rawValue = this.get("value");
if (rawValue instanceof Boolean) {
return new BooleanField((Boolean) rawValue, this.getConfidence(), this.getPolygon(), this.getPageId());
}
throw new ClassCastException("Cannot cast " + rawValue + " to Boolean");
}

/**
* Represent the object as a standard {@link DateField}.
* @return A {@link DateField} containing a date value.
*/
public DateField asDateField() {
return new DateField(
Expand All @@ -66,6 +82,7 @@ public DateField asDateField() {

/**
* Represent the object as a standard {@link ClassificationField}.
* @return A {@link ClassificationField} containing a string value.
*/
public ClassificationField asClassificationField() {
return new ClassificationField(
Expand All @@ -75,13 +92,16 @@ public ClassificationField asClassificationField() {

/**
* Get the polygon, if present.
* @return A {@link Polygon}.
*/
public Polygon getPolygon() {
return this.getAsPolygon("polygon");
}

/**
* Get the specified key as a {@link Polygon} object.
* @param key Key to retrieve the polygon from.
* @return A {@link Polygon}.
*/
public Polygon getAsPolygon(String key) {
if (this.containsKey(key)) {
Expand All @@ -92,20 +112,23 @@ public Polygon getAsPolygon(String key) {

/**
* Get the page ID, if present.
* @return A page ID, as an integer.
*/
public Integer getPageId() {
return this.get("page_id") != null ? (Integer) this.get("page_id") : null;
}

/**
* Get the confidence score, if present.
* @return The confidence score, as a double.
*/
public Double getConfidence() {
return this.get("confidence") != null ? (Double) this.get("confidence") : null;
}

/**
* Get the information on whether the date field was extracted.
* @return A boolean representation of the field.
*/
public Boolean getIsComputed(){
return (Boolean) this.get("is_computed");
Expand Down
43 changes: 39 additions & 4 deletions src/test/java/com/mindee/product/generated/GeneratedV1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.mindee.parsing.generated.GeneratedFeature;
import com.mindee.parsing.generated.GeneratedObject;
import com.mindee.parsing.standard.AmountField;
import com.mindee.parsing.standard.BooleanField;
import com.mindee.parsing.standard.ClassificationField;
import com.mindee.parsing.standard.DateField;
import com.mindee.parsing.standard.StringField;
Expand All @@ -24,12 +25,26 @@ protected AsyncPredictResponse<GeneratedV1> getAsyncPrediction(String name) thro
objectMapper.findAndRegisterModules();

JavaType type = objectMapper.getTypeFactory().constructParametricType(
AsyncPredictResponse.class,
GeneratedV1.class
AsyncPredictResponse.class,
GeneratedV1.class
);
return objectMapper.readValue(
new File("src/test/resources/products/generated/response_v1/" + name + "_international_id_v1.json"),
type
new File("src/test/resources/products/generated/response_v1/" + name + "_international_id_v1.json"),
type
);
}

protected AsyncPredictResponse<GeneratedV1> getUsMailPrediction() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules();

JavaType type = objectMapper.getTypeFactory().constructParametricType(
AsyncPredictResponse.class,
GeneratedV1.class
);
return objectMapper.readValue(
new File("src/test/resources/products/us_mail/response_v3/complete.json"),
type
);
}

Expand Down Expand Up @@ -244,6 +259,14 @@ else if (featureValue.isList()) {
}
}

@Test
void whenCompleteDeserializedWithBoolean_mustHaveValidProperties() throws IOException {
AsyncPredictResponse<GeneratedV1> response = getUsMailPrediction();
GeneratedV1Document docPrediction = response.getDocumentObj().getInference().getPrediction();
Map<String, GeneratedFeature> features = docPrediction.getFields();
Assertions.assertFalse(features.get("is_return_to_sender").asBooleanField().getValue());
}

@Test
void whenAmountDeserialized_mustCastToDouble() {
GeneratedObject intObject = new GeneratedObject();
Expand All @@ -260,4 +283,16 @@ void whenAmountDeserialized_mustCastToDouble() {
doubleObject.put("value", "I will fail miserably");
Assertions.assertThrows(ClassCastException.class, badStringObject::asAmountField);
}

@Test
void whenBooleanDeserialized_mustCastToBoolean() {
GeneratedObject boolObject = new GeneratedObject();
boolObject.put("value", true);
BooleanField booleanField = boolObject.asBooleanField();
Assertions.assertTrue(booleanField.getValue());

boolObject.put("value", false);
booleanField = boolObject.asBooleanField();
Assertions.assertFalse(booleanField.getValue());
}
}
Loading