Skip to content

Commit

Permalink
Update the code to map the values to the first suitable type in union…
Browse files Browse the repository at this point in the history
… types
  • Loading branch information
Nuvindu committed Oct 15, 2024
1 parent a05d61b commit c4b6014
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 60 deletions.
56 changes: 25 additions & 31 deletions ballerina/tests/record_tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,6 @@ public isolated function testOptionalMultipleFieldsInRecords() returns error? {
return verifyOperation(Lecturer6, lecturer6, schema);
}


@test:Config {
groups: ["record", "union"]
}
Expand Down Expand Up @@ -409,7 +408,7 @@ public isolated function testTypeCastingInRecords() returns error? {
};
Schema avro = check new (schema);
byte[] serializedValue = check avro.toAvro(recordValue);
var deserializedValue = check avro.fromAvro(serializedValue, Record);
Record deserializedValue = check avro.fromAvro(serializedValue);

json expected = {
"hrdCasinoGgrLt":4999.01,
Expand Down Expand Up @@ -443,32 +442,27 @@ public isolated function testTypeCastingInRecords() returns error? {
test:assertEquals(deserializedValue.toJson(), expected.toJson());
}

type Record record {
decimal? hrdCasinoGgrLt;
string? hrdAccountCreationTimestamp;
float? hrdSportsBetCountLifetime;
float? hrdSportsFreeBetAmountLifetime;
string? hriUnityId;
string? hrdLastRealMoneySportsbookBetTs;
string? hrdLoyaltyTier;
string? rowInsertTimestampEst;
float? ltvSports365Total;
string? hrdFirstDepositTimestamp;
boolean? hrdVipStatus;
string? hrdLastRealMoneyCasinoWagerTs;
float? hrdSportsCashBetAmountLifetime;
string? hrdAccountStatus;
string? hrdAccountId;
float? ltvAllVerticals365Total;
boolean? hrdOptInSms;
float? ltvCasino365Total;
string? hrdAccountSubStatus;
float? hrdCasinoTotalWagerLifetime;
float? hrdSportsGgrLt;
string? currentGeoSegment;
string? kycStatus;
string? signupGeoSegment;
float? hrdSportsBetAmountLifetime;
boolean? hrdOptInEmail;
boolean? hrdOptInPush;
};
@test:Config {
groups: ["record", "union"]
}
public isolated function testUnionsWithRecords() returns error? {
string schema = string `{
"type": "record",
"name": "DataRecord",
"namespace": "data",
"fields": [
{
"name": "shortValue",
"type": ["null", "int", "double"]
}
]
}`;
int:Signed16 short = 5555;
json value = {
"shortValue": short
};
Schema avro = check new (schema);
byte[] serializedValue = check avro.toAvro(value);
DataRecord deserializedValue = check avro.fromAvro(serializedValue);
test:assertEquals(deserializedValue, value);
}
1 change: 1 addition & 0 deletions ballerina/tests/types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,4 @@ type FloatArray float[];
type EnumArray Numbers[];
type Enum2DArray Numbers[][];
type ReadOnlyString2DArray string[][] & readonly;
type DataRecord record{};
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,26 @@ public class UnionRecordUtils {

public static void visitUnionRecords(Type type, BMap<BString, Object> ballerinaRecord,
Schema.Field field, Object fieldData) throws Exception {
int size = ballerinaRecord.size();
for (Schema schemaType : field.schema().getTypes()) {
if (fieldData == null) {
ballerinaRecord.put(StringUtils.fromString(field.name()), null);
break;
}
switch (schemaType.getType()) {
case BYTES:
handleBytesField(field, fieldData, ballerinaRecord);
break;
case FIXED:
handleFixedField(field, fieldData, ballerinaRecord);
break;
case ARRAY:
handleArrayField(field, fieldData, ballerinaRecord, schemaType);
break;
case MAP:
handleMapField(field, fieldData, ballerinaRecord);
break;
case RECORD:
handleRecordField(type, field, fieldData, ballerinaRecord, schemaType);
break;
case STRING:
handleStringField(field, fieldData, ballerinaRecord);
break;
case INT, LONG:
handleIntegerField(field, fieldData, ballerinaRecord);
break;
case FLOAT, DOUBLE:
handleFloatField(field, fieldData, ballerinaRecord);
break;
case ENUM:
handleEnumField(field, fieldData, ballerinaRecord);
break;
default:
handleDefaultField(field, fieldData, ballerinaRecord);
case BYTES -> handleBytesField(field, fieldData, ballerinaRecord);
case FIXED -> handleFixedField(field, fieldData, ballerinaRecord);
case ARRAY -> handleArrayField(field, fieldData, ballerinaRecord, schemaType);
case MAP -> handleMapField(field, fieldData, ballerinaRecord);
case RECORD -> handleRecordField(type, field, fieldData, ballerinaRecord, schemaType);
case STRING -> handleStringField(field, fieldData, ballerinaRecord);
case INT, LONG -> handleIntegerField(field, fieldData, ballerinaRecord);
case FLOAT, DOUBLE -> handleFloatField(field, fieldData, ballerinaRecord);
case ENUM -> handleEnumField(field, fieldData, ballerinaRecord);
default -> handleDefaultField(field, fieldData, ballerinaRecord);
}
if (ballerinaRecord.size() != size) {
break;
}
}
}
Expand Down

0 comments on commit c4b6014

Please sign in to comment.