Skip to content

Commit beea6fe

Browse files
committed
✨ add big decimal support to v2 fields
1 parent fe70392 commit beea6fe

File tree

3 files changed

+55
-39
lines changed

3 files changed

+55
-39
lines changed

src/main/java/com/mindee/parsing/v2/field/InferenceFields.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,10 @@ public String toString(int indent) {
6262
} else if (fieldInstance.getType() == DynamicField.FieldType.OBJECT_FIELD) {
6363
strBuilder.append(fieldInstance.getObjectField());
6464
} else if (fieldInstance.getType() == DynamicField.FieldType.SIMPLE_FIELD) {
65-
strBuilder
66-
.append(
67-
fieldInstance.getSimpleField().getValue() != null
68-
? fieldInstance.getSimpleField().toString()
69-
: ""
70-
);
71-
65+
strBuilder.append(fieldInstance.getSimpleField());
7266
}
7367
joiner.add(strBuilder);
7468
});
75-
7669
return SummaryHelper.cleanSummary(joiner.toString());
7770
}
7871

src/main/java/com/mindee/parsing/v2/field/SimpleField.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
44
import com.fasterxml.jackson.annotation.JsonProperty;
55
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
6+
import java.math.BigDecimal;
67
import java.util.List;
78
import lombok.EqualsAndHashCode;
89
import lombok.Getter;
@@ -28,7 +29,7 @@ public SimpleField(Object value, FieldConfidence confidence, List<FieldLocation>
2829
}
2930

3031
/**
31-
* Retrieves the value of the field as a string.
32+
* Retrieves the value of the field as a {@link String}.
3233
*
3334
* @return the field value as a string
3435
* @throws ClassCastException if the value cannot be cast to a string
@@ -38,7 +39,7 @@ public String getStringValue() throws ClassCastException {
3839
}
3940

4041
/**
41-
* Retrieves the value of the field as a Double.
42+
* Retrieves the value of the field as a {@link Double}.
4243
*
4344
* @return the field value as a Double
4445
* @throws ClassCastException if the value cannot be cast to a Double
@@ -48,7 +49,20 @@ public Double getDoubleValue() throws ClassCastException {
4849
}
4950

5051
/**
51-
* Retrieves the value of the field as a Boolean.
52+
* Retrieves the value of the field as a {@link BigDecimal}.
53+
*
54+
* @return the field value as a BigDecimal
55+
* @throws ClassCastException if the value cannot be cast to a BigDecimal
56+
*/
57+
public BigDecimal getBigDecimalValue() throws NumberFormatException, ClassCastException {
58+
if (value == null) {
59+
return null;
60+
}
61+
return BigDecimal.valueOf(getDoubleValue());
62+
}
63+
64+
/**
65+
* Retrieves the value of the field as a {@link Boolean}.
5266
*
5367
* @return the field value as a Boolean
5468
* @throws ClassCastException if the value cannot be cast to a Boolean

src/test/java/com/mindee/parsing/v2/InferenceTest.java

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.mindee.parsing.v2.field.ObjectField;
1717
import com.mindee.parsing.v2.field.SimpleField;
1818
import java.io.IOException;
19+
import java.math.BigDecimal;
1920
import java.util.HashMap;
2021
import java.util.LinkedHashMap;
2122
import java.util.List;
@@ -93,51 +94,52 @@ class CompletePredictionTest {
9394
void asyncPredict_whenComplete_mustExposeAllProperties() throws IOException {
9495
InferenceResponse response = loadInference("products/financial_document/complete.json");
9596
Inference inference = response.getInference();
96-
assertNotNull(inference, "Inference must not be null");
97-
assertEquals(
98-
"12345678-1234-1234-1234-123456789abc",
99-
inference.getId(),
100-
"Inference ID mismatch"
101-
);
97+
assertNotNull(inference);
98+
assertEquals("12345678-1234-1234-1234-123456789abc", inference.getId());
10299

103100
InferenceModel model = inference.getModel();
104101
assertNotNull(model, "Model must not be null");
105-
assertEquals("12345678-1234-1234-1234-123456789abc", model.getId(), "Model ID mismatch");
102+
assertEquals("12345678-1234-1234-1234-123456789abc", model.getId());
106103

107104
InferenceFile file = inference.getFile();
108-
assertNotNull(file, "File must not be null");
109-
assertEquals("complete.jpg", file.getName(), "File name mismatch");
110-
assertEquals(1, file.getPageCount(), "Page count mismatch");
111-
assertEquals("image/jpeg", file.getMimeType(), "MIME type mismatch");
112-
assertNull(file.getAlias(), "File alias must be null for this payload");
105+
assertNotNull(file);
106+
assertEquals("complete.jpg", file.getName());
107+
assertEquals(1, file.getPageCount());
108+
assertEquals("image/jpeg", file.getMimeType());
109+
assertNull(file.getAlias());
113110

114111
InferenceFields fields = inference.getResult().getFields();
115-
assertEquals(21, fields.size(), "Expected 21 fields in the payload");
112+
assertEquals(21, fields.size());
116113

117114
SimpleField date = fields.get("date").getSimpleField();
118-
assertEquals("2019-11-02", date.getValue(), "'date' value mismatch");
115+
assertEquals("2019-11-02", date.getValue());
119116

117+
// list of objects
120118
DynamicField taxes = fields.get("taxes");
121-
assertNotNull(taxes, "'taxes' field must exist");
119+
assertNotNull(taxes);
122120
ListField taxesList = taxes.getListField();
123-
assertNotNull(taxesList, "'taxes' must be a ListField");
124-
assertEquals(1, taxesList.getItems().size(), "'taxes' list must contain exactly one item");
121+
assertNotNull(taxesList);
122+
assertEquals(1, taxesList.getItems().size());
125123
ObjectField taxItemObj = taxesList.getItems().get(0).getObjectField();
126-
assertNotNull(taxItemObj, "First item of 'taxes' must be an ObjectField");
127-
assertEquals(3, taxItemObj.getFields().size(), "Tax ObjectField must contain 3 sub-fields");
124+
assertNotNull(taxItemObj);
125+
assertEquals(3, taxItemObj.getFields().size());
128126
SimpleField baseTax = taxItemObj.getFields().get("base").getSimpleField();
129-
assertEquals(31.5, baseTax.getValue(), "'taxes.base' value mismatch");
130-
assertNotNull(taxes.toString(), "'taxes'.toString() must not be null");
127+
assertEquals(31.5, baseTax.getValue());
128+
assertEquals(31.5, baseTax.getDoubleValue());
129+
assertEquals(BigDecimal.valueOf(31.5), baseTax.getBigDecimalValue());
130+
assertNotNull(taxes.toString());
131131

132+
// single object
132133
DynamicField supplierAddress = fields.get("supplier_address");
133-
assertNotNull(supplierAddress, "'supplier_address' field must exist");
134+
assertNotNull(supplierAddress);
134135
ObjectField supplierObj = supplierAddress.getObjectField();
135-
assertNotNull(supplierObj, "'supplier_address' must be an ObjectField");
136+
assertEquals(9, supplierObj.getFields().size());
137+
assertNotNull(supplierObj);
136138
DynamicField country = supplierObj.getFields().get("country");
137-
assertNotNull(country, "'supplier_address.country' must exist");
138-
assertEquals("USA", country.getSimpleField().getValue(), "Country mismatch");
139-
assertEquals("USA", country.toString(), "'country'.toString() mismatch");
140-
assertNotNull(supplierAddress.toString(), "'supplier_address'.toString() must not be null");
139+
assertNotNull(country);
140+
assertEquals("USA", country.getSimpleField().getValue());
141+
assertEquals("USA", country.toString());
142+
assertNotNull(supplierAddress.toString());
141143

142144
ObjectField customerAddr = fields.get("customer_address").getObjectField();
143145
SimpleField city = customerAddr.getFields().get("city").getSimpleField();
@@ -214,7 +216,7 @@ void standardFieldTypes_mustExposeSimpleFieldValues() throws IOException {
214216

215217
assertNotNull(fields.get("field_simple_string").getSimpleField());
216218

217-
SimpleField fieldSimpleString = fields.get("field_simple_string").getSimpleField();
219+
SimpleField fieldSimpleString = fields.getSimpleField("field_simple_string");
218220
testSimpleFieldString(fieldSimpleString);
219221
assertEquals(FieldConfidence.Certain, fieldSimpleString.getConfidence());
220222
assertEquals(1, fieldSimpleString.getLocations().size());
@@ -232,25 +234,32 @@ void standardFieldTypes_mustExposeSimpleFieldValues() throws IOException {
232234
assertInstanceOf(Double.class, fieldSimpleInt.getValue());
233235
assertEquals(fieldSimpleInt.getValue(), fieldSimpleInt.getDoubleValue());
234236
assertEquals(FieldConfidence.Medium, fieldSimpleInt.getConfidence());
237+
assertThrows(ClassCastException.class, fieldSimpleInt::getStringValue);
235238

236239
SimpleField fieldSimpleZero = fields.get("field_simple_zero").getSimpleField();
237240
assertNotNull(fieldSimpleZero);
238241
assertEquals(FieldConfidence.Low, fieldSimpleZero.getConfidence());
239242
assertInstanceOf(Double.class, fieldSimpleZero.getValue());
243+
assertEquals(0.0, fieldSimpleZero.getDoubleValue());
244+
assertEquals(BigDecimal.valueOf(0.0), fieldSimpleZero.getBigDecimalValue());
245+
assertThrows(ClassCastException.class, fieldSimpleZero::getStringValue);
246+
assertThrows(ClassCastException.class, fieldSimpleZero::getBooleanValue);
240247

241248
SimpleField fieldSimpleBool = fields.get("field_simple_bool").getSimpleField();
242249
assertNotNull(fieldSimpleBool);
243250
assertInstanceOf(Boolean.class, fieldSimpleBool.getValue());
244251
assertEquals(fieldSimpleBool.getValue(), fieldSimpleBool.getBooleanValue());
245252
assertThrows(ClassCastException.class, fieldSimpleBool::getStringValue);
246253
assertThrows(ClassCastException.class, fieldSimpleBool::getDoubleValue);
254+
assertThrows(ClassCastException.class, fieldSimpleBool::getBigDecimalValue);
247255

248256
SimpleField fieldSimpleNull = fields.get("field_simple_null").getSimpleField();
249257
assertNotNull(fieldSimpleNull);
250258
assertNull(fieldSimpleNull.getValue());
251259
assertNull(fieldSimpleNull.getStringValue());
252260
assertNull(fieldSimpleNull.getBooleanValue());
253261
assertNull(fieldSimpleNull.getDoubleValue());
262+
assertNull(fieldSimpleNull.getBigDecimalValue());
254263
}
255264

256265
@Test

0 commit comments

Comments
 (0)