Skip to content

Commit

Permalink
Merge pull request #27 from Nuvindu/union-fix
Browse files Browse the repository at this point in the history
Fix handling union type values in the Avro payload
  • Loading branch information
Nuvindu authored Oct 18, 2024
2 parents afc32a7 + c4b6014 commit 5d1cd6e
Show file tree
Hide file tree
Showing 8 changed files with 463 additions and 133 deletions.
6 changes: 3 additions & 3 deletions ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "ballerina"
name = "avro"
version = "1.0.1"
version = "1.0.2"
authors = ["Ballerina"]
export=["avro"]
keywords = ["avro", "serialization", "deserialization", "serdes"]
Expand All @@ -18,8 +18,8 @@ graalvmCompatible = true
[[platform.java17.dependency]]
groupId = "io.ballerina.lib"
artifactId = "avro-native"
version = "1.0.1"
path = "../native/build/libs/avro-native-1.0.1.jar"
version = "1.0.2"
path = "../native/build/libs/avro-native-1.0.2-SNAPSHOT.jar"

[[platform.java17.dependency]]
groupId = "org.apache.avro"
Expand Down
4 changes: 2 additions & 2 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ distribution-version = "2201.9.0"
[[package]]
org = "ballerina"
name = "avro"
version = "1.0.1"
version = "1.0.2"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
Expand All @@ -23,7 +23,7 @@ modules = [
[[package]]
org = "ballerina"
name = "io"
version = "1.6.0"
version = "1.6.1"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
Expand Down
64 changes: 64 additions & 0 deletions ballerina/tests/primitive_tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,67 @@ public isolated function testNullValuesWithNonNullData() returns error? {
byte[]|error serializedValue = avro.toAvro("string");
test:assertTrue(serializedValue is error);
}

@test:Config {
groups: ["primitive"]
}
public isolated function testUnsignedShortValue() returns error? {
string schema = string `
{
"type": "int",
"name" : "shortValue",
"namespace": "data"
}`;

int:Unsigned8 value = 255;
return verifyOperation(int:Unsigned8, value, schema);
}

@test:Config {
groups: ["primitive", "check"]
}
public isolated function testSignedShortValue() returns error? {
string schema = string `
{
"type": "int",
"name" : "shortValue",
"namespace": "data"
}`;

int:Signed32 value = 555950000;
return verifyOperation(int:Signed32, value, schema);
}

@test:Config {
groups: ["primitive", "check"]
}
public isolated function testSignedMinusShortValue() returns error? {
string schema = string `
{
"type": "double",
"name" : "shortValue",
"namespace": "data"
}`;

int:Signed32 value = -2147483;
Schema avro = check new (schema);
byte[] serializedValue = check avro.toAvro(value);
float deserializedValue = check avro.fromAvro(serializedValue);
test:assertEquals(deserializedValue, -2147483.0);
}

@test:Config {
groups: ["primitive", "check"]
}
public isolated function testByteValue() returns error? {
string schema = string `
{
"type": "bytes",
"name" : "byteValue",
"namespace": "data"
}`;
byte[] data = [];
byte value = 2;
data.push(value);
return verifyOperation(ByteArray, data, schema);
}
96 changes: 96 additions & 0 deletions ballerina/tests/record_tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,99 @@ public isolated function testOptionalMultipleFieldsInRecords() returns error? {
};
return verifyOperation(Lecturer6, lecturer6, schema);
}

@test:Config {
groups: ["record", "union"]
}
public isolated function testTypeCastingInRecords() returns error? {
string jsonFileName = string `tests/resources/schema_complex.json`;
string schema = (check io:fileReadJson(jsonFileName)).toString();
Record recordValue = {
hrdCasinoGgrLt: 4999.01,
hrdAccountCreationTimestamp: "2024-02-25T20:50:37.891782",
hrdSportsBetCountLifetime: 77,
hrdSportsFreeBetAmountLifetime: 0,
hriUnityId: "807612199",
hrdLastRealMoneySportsbookBetTs: "2024-04-24T20:15:51.932",
hrdLoyaltyTier: "MEMBER_TIER",
rowInsertTimestampEst: "2024-09-23T03:29:43.431",
ltvSports365Total: 0,
hrdFirstDepositTimestamp: "2024-02-25T20:56:24.109021",
hrdVipStatus: true,
hrdLastRealMoneyCasinoWagerTs: null,
hrdSportsCashBetAmountLifetime: 731,
hrdAccountStatus: "ACTIVE",
hrdAccountId: "1362347172559585332",
ltvAllVerticals365Total: 0,
hrdOptInSms: false,
ltvCasino365Total: null,
hrdAccountSubStatus: null,
hrdCasinoTotalWagerLifetime: null,
hrdSportsGgrLt: 4999.01,
currentGeoSegment: "HRD_FLORIDA",
kycStatus: "VERIFIED",
signupGeoSegment: "HRD_FLORIDA",
hrdSportsBetAmountLifetime: 731,
hrdOptInEmail: false,
hrdOptInPush: false
};
Schema avro = check new (schema);
byte[] serializedValue = check avro.toAvro(recordValue);
Record deserializedValue = check avro.fromAvro(serializedValue);

json expected = {
"hrdCasinoGgrLt":4999.01,
"hrdAccountCreationTimestamp":"2024-02-25T20:50:37.891782",
"hrdSportsFreeBetAmountLifetime":0.0,
"hriUnityId":"807612199",
"hrdLastRealMoneySportsbookBetTs":"2024-04-24T20:15:51.932",
"hrdLoyaltyTier":"MEMBER_TIER",
"rowInsertTimestampEst":"2024-09-23T03:29:43.431",
"ltvSports365Total":0.0,
"hrdVipStatus":true,
"hrdSportsBetCountLifetime":77.0,
"hrdLastRealMoneyCasinoWagerTs":null,
"hrdSportsCashBetAmountLifetime":731.0,
"hrdAccountStatus":"ACTIVE",
"hrdAccountId":"1362347172559585332",
"ltvAllVerticals365Total":0.0,
"ltvCasino365Total":null,
"hrdAccountSubStatus":null,
"hrdCasinoTotalWagerLifetime":null,
"hrdSportsGgrLt":4999.01,
"currentGeoSegment":"HRD_FLORIDA",
"kycStatus":"VERIFIED",
"hrdOptInSms":false,
"hrdFirstDepositTimestamp":"2024-02-25T20:56:24.109021",
"signupGeoSegment":"HRD_FLORIDA",
"hrdSportsBetAmountLifetime":731.0,
"hrdOptInEmail":false,
"hrdOptInPush":false
};
test:assertEquals(deserializedValue.toJson(), expected.toJson());
}

@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);
}
Loading

0 comments on commit 5d1cd6e

Please sign in to comment.