Skip to content

Commit

Permalink
Remove jackson dependency in mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
manojlds committed Dec 5, 2019
1 parent 44d8d69 commit 8540820
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 14 deletions.
8 changes: 7 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,19 @@ subprojects {

}

project(":thepill-common") {
}

project(":thepill-procedure") {
dependencies {
compile project(':thepill-common')
}
}

project(":thepill-extension") {
dependencies {
compileOnly group: 'javax.ws.rs', name: 'javax.ws.rs-api', version: '2.0'
compile group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version: '1.9.7'
compile project(':thepill-common')
testCompile project(':thepill-procedure')
testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'
}
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
rootProject.name = 'thepill'

include 'thepill-common'
include 'thepill-procedure'
include 'thepill-extension'
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.stacktoheap.thepill.utils
import com.stacktoheap.thepill.models.Parameter
import kotlinx.serialization.UnstableDefault
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.list
import org.neo4j.graphdb.Node

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ object JsonUtils {
is Number -> JsonPrimitive(this)
is String -> JsonPrimitive(this)
is Boolean -> JsonPrimitive(this)
is Map<*, *> -> this.toJsonObject()
is Iterable<*> -> JsonArray(this.map { it.toJsonElement() })
is Array<*> -> JsonArray(this.map { it.toJsonElement() })
else -> {
val jsonParser = Json(JsonConfiguration.Stable)
val serializer = jsonParser.context.getContextualOrDefault(this)
Expand All @@ -21,6 +24,8 @@ object JsonUtils {

fun JsonElement.toPrimitive(): Any? = when (this) {
is JsonNull -> null
is JsonObject -> this.toPrimitiveMap()
is JsonArray -> this.map { it.toPrimitive() }
is JsonLiteral -> {
if (isString) {
contentOrNull
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.stacktoheap.thepill
import org.codehaus.jackson.map.ObjectMapper
import com.stacktoheap.thepill.utils.JsonUtils
import kotlinx.serialization.UnstableDefault
import kotlinx.serialization.json.Json
import kotlinx.serialization.list
import org.neo4j.graphdb.GraphDatabaseService
import org.neo4j.helpers.collection.MapUtil
import javax.ws.rs.*
Expand All @@ -10,23 +13,25 @@ import javax.ws.rs.core.Response
@Path("")
class ThePillExtension(@Context val graphDb: GraphDatabaseService) {

@UnstableDefault
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/make_decision/{decisionName}")
fun make_decision(@PathParam("decisionName") decisionName: String, body: String): Response {
fun makeDecision(@PathParam("decisionName") decisionName: String, body: String): Response {
val queryArguments = MapUtil.map("decisionName", decisionName, "facts", parseMap(body))
val result = graphDb.execute(DECISION_QUERY, queryArguments)
val response = result.asSequence().map { it.mapValues { it.value } }.toList()

return asResponse(response)
}

@UnstableDefault
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/next_step/{decisionName}")
fun next_step(@PathParam("decisionName") decisionName: String, @QueryParam("decisionId") decisionId: Long?, body: String): Response {
fun nextStep(@PathParam("decisionName") decisionName: String, @QueryParam("decisionId") decisionId: Long?, body: String): Response {
val (query, queryArguments) =
when(decisionId) {
null -> Pair(STEP_QUERY, MapUtil.map("decisionName", decisionName, "facts", parseMap(body)))
Expand All @@ -40,20 +45,22 @@ class ThePillExtension(@Context val graphDb: GraphDatabaseService) {
}

companion object {
val OBJECT_MAPPER: ObjectMapper = ObjectMapper()
val DECISION_QUERY = "CALL com.stacktoheap.thepill.make_decision(\$decisionName, \$facts, true) yield path return last(nodes(path)).value as result"
val STEP_QUERY = "CALL com.stacktoheap.thepill.next_step(\$decisionName, \$facts) yield result return result as stepResult"
val STEP_QUERY_WITH_ID = "CALL com.stacktoheap.thepill.next_step(\$decisionName, \$facts, \$decisionId) yield result return result as stepResult"
const val DECISION_QUERY = "CALL com.stacktoheap.thepill.make_decision(\$decisionName, \$facts, true) yield path return last(nodes(path)).value as result"
const val STEP_QUERY = "CALL com.stacktoheap.thepill.next_step(\$decisionName, \$facts) yield result return result as stepResult"
const val STEP_QUERY_WITH_ID = "CALL com.stacktoheap.thepill.next_step(\$decisionName, \$facts, \$decisionId) yield result return result as stepResult"
}

fun asResponse(result: List<Map<String, Any?>>) = Response.ok().entity(formatMap(result)).build()
@UnstableDefault
private fun asResponse(result: List<Map<String, Any>>) = Response.ok().entity(formatMap(result)).build()

private fun formatMap(result: List<Map<String, Any?>>) = OBJECT_MAPPER.writeValueAsString(result)
@UnstableDefault
private fun formatMap(result: List<Map<String, Any>>) = Json.stringify(JsonUtils.MapSerializer.list, result)

@UnstableDefault
private fun parseMap(value: String?): Map<String, Any> =
if (value == null || value.isNullOrBlank() || value == "null") emptyMap()
else {
val v = value.trim('"', ' ', '\t', '\n', '\r')
OBJECT_MAPPER.readValue(v, Map::class.java) as Map<String, Any>
Json.parse(JsonUtils.MapSerializer, v)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ class ThePillExtensionTest {
val decisionResult = result[0]["stepResult"]

assertThat(decisionResult!!["value"]).isEqualTo("knowledge")


}
}
}
Expand Down

0 comments on commit 8540820

Please sign in to comment.