Skip to content

Instantly share code, notes, and snippets.

@sfitts
Created May 29, 2015 03:44
Show Gist options
  • Save sfitts/73d4a2e2229466f5ccd3 to your computer and use it in GitHub Desktop.
Save sfitts/73d4a2e2229466f5ccd3 to your computer and use it in GitHub Desktop.
Reproduction of issue with Vert-x3 MongoDB client

These are the parts for a gradle project designed to mimic the gradle-verticle example. In order to see the issue you need to use the JS script to load data into the test db in mongo via mongo setup.js. Then when you build and run the fat jar you should see the following output:

Result is a LinkedHashMap
Object is a JsonObject
List is a JsonArray

The issue is the nested object and array, which aren't in a form that Groovy can handle natively.

plugins {
id 'groovy'
id 'com.github.johnrengelman.shadow' version '1.2.1'
}
version = '0.0.1-SNAPSHOT'
if (!JavaVersion.current().java8Compatible) {
throw new IllegalStateException('''A Haiku:
| This needs Java 8,
| You are using something else,
| Refresh. Try again.'''.stripMargin())
}
repositories {
jcenter()
mavenCentral()
maven {
url = 'http://oss.sonatype.org/content/repositories/snapshots/'
}
}
ext {
vertxVersion = '3.0.0-SNAPSHOT'
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.3'
compile "io.vertx:vertx-core:$vertxVersion"
compile "io.vertx:vertx-lang-groovy:$vertxVersion"
compile "io.vertx:vertx-mongo-client:$vertxVersion"
testCompile 'junit:junit:4.12'
}
shadowJar {
classifier = 'fat'
manifest {
attributes 'Main-Class': 'io.vertx.core.Starter'
attributes 'Main-Verticle': 'groovy:bugs.SimpleMongoQuery'
}
mergeServiceFiles {
include 'META-INF/services/io.vertx.core.spi.VerticleFactory'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
use test
db.createCollection("MyCollection")
db.MyCollection.save({"obj": {"key": value"}, "list": ["v1", "v2"]})
package bugs
import io.vertx.core.AsyncResult
import io.vertx.core.Future;
import io.vertx.groovy.ext.mongo.MongoClient
import io.vertx.lang.groovy.GroovyVerticle
/**
* @author sfitts
*/
public class SimpleMongoQuery extends GroovyVerticle {
private MongoClient mongo
@Override
public void start(Future<Void> startFuture) {
// Create a Mongo Client
def config = [db_name: 'test']
mongo = MongoClient.createShared(vertx, config)
doMongoQuery()
startFuture.complete()
}
private void doMongoQuery() {
mongo.findOne('MyCollection', [:], null, this.&handleMongoResults)
}
private void handleMongoResults(AsyncResult<Map<String, Object>> res) {
if (res.succeeded()) {
def x = res.result()
if (x) println("Result is a ${x.getClass().simpleName}")
if (x.obj) println("Object is a ${x.obj.getClass().simpleName}")
if (x.list) println("List is a ${x.list.getClass().simpleName}")
}
}
}
@vietj
Copy link

vietj commented May 29, 2015

there is an missing quote in setup.js for { "key": "value" }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment