Skip to content

Commit

Permalink
Merge pull request #199 from Chuhaa/master
Browse files Browse the repository at this point in the history
Fix record type in stream next response
  • Loading branch information
LakshanSS authored Jun 28, 2022
2 parents a6c52e7 + 374db39 commit a42b515
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 26 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/dev-stg-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
./gradlew build
- name: Ballerina Build
uses: ballerina-platform/ballerina-action/@master
uses: ballerina-platform/ballerina-action/@2201.1.0
with:
args:
pack mongodb
Expand All @@ -46,7 +46,7 @@ jobs:

- name: Push to Staging
if: github.event.inputs.bal_central_environment == 'STAGE'
uses: ballerina-platform/ballerina-action/@master
uses: ballerina-platform/ballerina-action/@2201.1.0
with:
args:
push
Expand All @@ -57,7 +57,7 @@ jobs:

- name: Push to Dev
if: github.event.inputs.bal_central_environment == 'DEV'
uses: ballerina-platform/ballerina-action/@master
uses: ballerina-platform/ballerina-action/@2201.1.0
with:
args:
push
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
./gradlew build
- name: Ballerina Build
uses: ballerina-platform/ballerina-action/@master
uses: ballerina-platform/ballerina-action/@2201.1.0
with:
args:
pack mongodb
Expand All @@ -37,7 +37,7 @@ jobs:
JAVA_OPTS: -DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true

- name: Ballerina Push
uses: ballerina-platform/ballerina-action/@master
uses: ballerina-platform/ballerina-action/@2201.1.0
with:
args:
push
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
group=org.ballerinalang.mongodb
version=3.3.0
ballerinaLangVersion=2201.0.3
version=3.3.1
ballerinaLangVersion=2201.1.0
2 changes: 2 additions & 0 deletions mongodb-native/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ dependencies {
compile (group: 'org.ballerinalang', name: 'ballerina-runtime', version: project.ballerinaLangVersion) {
transitive = true
}
compile group: 'org.ballerinalang', name: 'value', version: project.ballerinaLangVersion
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.3'
compile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.8.2'
dist group: 'org.mongodb', name: 'mongo-java-driver', version: '3.8.2'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@

import static io.ballerina.runtime.api.utils.StringUtils.fromString;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.client.MongoCursor;

import io.ballerina.runtime.api.PredefinedTypes;
import io.ballerina.runtime.api.creators.TypeCreator;
import io.ballerina.runtime.api.creators.ValueCreator;
import io.ballerina.runtime.api.types.RecordType;
import io.ballerina.runtime.api.types.UnionType;
import io.ballerina.runtime.api.values.BTypedesc;
import org.ballerinalang.langlib.value.FromJsonStringWithType;
import org.bson.Document;

import io.ballerina.runtime.api.creators.ErrorCreator;
import io.ballerina.runtime.api.values.BObject;
import io.ballerina.runtime.internal.JsonParser;

/**
* This class provides functionality for the `RecordIterator` to iterate through the MongoCursor.
Expand All @@ -38,17 +45,26 @@ public RecordIteratorUtils() {

public static Object nextResult(BObject recordIterator) {
MongoCursor<Document> results = (MongoCursor<Document>) recordIterator.getNativeData(
MongoDBConstants.RESULT_SET_NATIVE_DATA_FIELD);
MongoDBConstants.RESULT_SET_NATIVE_DATA_FIELD);
RecordType recordType = (RecordType) recordIterator.getNativeData(MongoDBConstants.RECORD_TYPE_DATA_FIELD);
if (results.hasNext()) {
return JsonParser.parse(results.next().toJson());
try {
String result = new ObjectMapper().writeValueAsString(results.next());
UnionType responseType = TypeCreator.createUnionType(recordType, PredefinedTypes.TYPE_ERROR,
PredefinedTypes.TYPE_NULL);
BTypedesc responseTypedescValue = ValueCreator.createTypedescValue(responseType);
return FromJsonStringWithType.fromJsonStringWithType(fromString(result), responseTypedescValue);
} catch (Exception e) {
return ErrorCreator.createError(fromString("Error while iterating elements"), e);
}
} else {
return null;
}
}

public static Object closeResult(BObject recordIterator) {
MongoCursor<Document> results = (MongoCursor<Document>) recordIterator.getNativeData(
MongoDBConstants.RESULT_SET_NATIVE_DATA_FIELD);
MongoDBConstants.RESULT_SET_NATIVE_DATA_FIELD);
if (results != null) {
try {
results.close();
Expand Down
23 changes: 19 additions & 4 deletions mongodb/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
[package]
distribution = "2201.0.3"
distribution = "2201.1.0"
org = "ballerinax"
name = "mongodb"
version = "3.3.0"
version = "3.3.1"
license= ["Apache-2.0"]
authors = ["Ballerina"]
keywords = ["IT Operations/Databases", "Cost/Freemium"]
icon = "icon.png"
repository = "https://github.com/ballerina-platform/module-ballerinax-mongodb"

[[platform.java11.dependency]]
path = "../mongodb-native/build/libs/mongodb-native-3.3.0.jar"
path = "../mongodb-native/build/libs/mongodb-native-3.3.1.jar"
groupId = "org.ballerinalang"
artifactId = "mongodb-native"
version = "3.3.0"
version = "3.3.1"

[[platform.java11.dependency]]
groupId = "com.fasterxml.jackson.core"
artifactId = "jackson-core"
version = "2.13.3"

[[platform.java11.dependency]]
groupId = "com.fasterxml.jackson.core"
artifactId = "jackson-annotations"
version = "2.13.3"

[[platform.java11.dependency]]
groupId = "com.fasterxml.jackson.core"
artifactId = "jackson-databind"
version = "2.13.3"
19 changes: 10 additions & 9 deletions mongodb/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies-toml-version = "2"
[[package]]
org = "ballerina"
name = "crypto"
version = "2.2.0"
version = "2.2.2"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "time"}
Expand All @@ -21,9 +21,10 @@ modules = [
[[package]]
org = "ballerina"
name = "file"
version = "1.2.0"
version = "1.2.2"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "log"},
{org = "ballerina", name = "os"},
Expand All @@ -37,7 +38,7 @@ modules = [
[[package]]
org = "ballerina"
name = "io"
version = "1.2.1"
version = "1.2.2"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
Expand All @@ -64,7 +65,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "log"
version = "2.2.0"
version = "2.2.1"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "io"},
Expand All @@ -79,7 +80,7 @@ modules = [
[[package]]
org = "ballerina"
name = "observe"
version = "1.0.1"
version = "1.0.5"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
Expand All @@ -88,7 +89,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "os"
version = "1.2.0"
version = "1.2.1"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
Expand All @@ -100,7 +101,7 @@ modules = [
[[package]]
org = "ballerina"
name = "regex"
version = "1.2.0"
version = "1.2.1"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
Expand All @@ -121,15 +122,15 @@ modules = [
[[package]]
org = "ballerina"
name = "time"
version = "2.2.0"
version = "2.2.2"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerinax"
name = "mongodb"
version = "3.3.0"
version = "3.3.1"
dependencies = [
{org = "ballerina", name = "crypto"},
{org = "ballerina", name = "file"},
Expand Down
2 changes: 1 addition & 1 deletion mongodb/Package.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The MongoDB connector allows you to connect to a MongoDB database from Ballerina

| | Version |
|---------------------------|-----------------------------|
| Ballerina Language | Swan Lake 2201.0.3 |
| Ballerina Language | Swan Lake 2201.1.0 |
| Mongo DB | 4.2.0 |

## Report issues
Expand Down
2 changes: 1 addition & 1 deletion mongodb/errors.bal
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ public type DatabaseError distinct error<DatabaseErrorDetail>;
public type ApplicationError distinct error;

# Represents a database or application level error returned from the MongoDB client remote functions.
public type Error DatabaseError|ApplicationError;
public type Error DatabaseError|ApplicationError|error;
1 change: 1 addition & 0 deletions mongodb/tests/main_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public function testFindData() returns error? {
stream<Movie, error?> result = check mongoClient->find(COLLECTION_NAME,filter = findDoc);
check result.forEach(function(Movie data){
log:printInfo(data.year.toString());
test:assertTrue(data is Movie);
test:assertEquals(data.year,"2019","Querying year 2019 filter failed");
});
log:printInfo("Querying year 2019 filter tested successfully");
Expand Down

0 comments on commit a42b515

Please sign in to comment.