Skip to content

Commit

Permalink
Handle null pathElements gracefully in message passing API (#480)
Browse files Browse the repository at this point in the history
In messages "List Resources Response" and "List Modules Response",
if `pathElements` and `error` are both null, default to an empty list.
  • Loading branch information
bioball authored May 8, 2024
1 parent 21aa44c commit 7da643f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,8 @@ The response to <<list-resources-request>>.
If successful, `pathElements` is set.
Otherwise, `error` is set.

If neither are set, `pathElements` default to an empty list.

[source,pkl]
----
/// A number identifying this request.
Expand Down Expand Up @@ -507,6 +509,8 @@ The response to <<list-modules-request>>.
If successful, `pathElements` is set.
Otherwise, `error` is set.

If neither are set, `pathElements` default to an empty list.

[source,pkl]
----
/// A number identifying this request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ internal class ClientModuleKeyFactory(
if (response.error != null) {
completeExceptionally(IOException(response.error))
} else {
complete(response.pathElements!!)
complete(response.pathElements ?: emptyList())
}
}
else -> completeExceptionally(ProtocolException("unexpected response"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ internal class ClientResourceReader(
transport.send(request) { response ->
when (response) {
is ListResourcesResponse ->
if (response.pathElements != null) {
complete(response.pathElements)
} else {
if (response.error != null) {
completeExceptionally(IOException(response.error))
} else {
complete(response.pathElements ?: emptyList())
}
else -> completeExceptionally(ProtocolException("Unexpected response"))
}
Expand Down
78 changes: 78 additions & 0 deletions pkl-server/src/test/kotlin/org/pkl/server/ServerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,43 @@ class ServerTest {
)
}

@Test
fun `glob resources -- null pathElements and null error`() {
val reader = ResourceReaderSpec(scheme = "bird", hasHierarchicalUris = true, isGlobbable = true)
val evaluatorId = client.sendCreateEvaluatorRequest(resourceReaders = listOf(reader))
client.send(
EvaluateRequest(
requestId = 1,
evaluatorId = evaluatorId,
moduleUri = URI("repl:text"),
moduleText =
"""
res = read*("bird:/**.txt").keys
"""
.trimIndent(),
expr = "res"
)
)
val listResourcesRequest = client.receive<ListResourcesRequest>()
client.send(
ListResourcesResponse(
requestId = listResourcesRequest.requestId,
evaluatorId = listResourcesRequest.evaluatorId,
pathElements = null,
error = null
)
)
val evaluateResponse = client.receive<EvaluateResponse>()
assertThat(evaluateResponse.result!!.debugYaml)
.isEqualTo(
"""
- 6
- []
"""
.trimIndent()
)
}

@Test
fun `glob resource error`() {
val reader = ResourceReaderSpec(scheme = "bird", hasHierarchicalUris = true, isGlobbable = true)
Expand Down Expand Up @@ -505,6 +542,47 @@ class ServerTest {
)
}

@Test
fun `glob module -- null pathElements and null error`() {
val reader =
ModuleReaderSpec(
scheme = "bird",
hasHierarchicalUris = true,
isLocal = true,
isGlobbable = true
)
val evaluatorId = client.sendCreateEvaluatorRequest(moduleReaders = listOf(reader))

client.send(
EvaluateRequest(
requestId = 1,
evaluatorId = evaluatorId,
moduleUri = URI("repl:text"),
moduleText = """res = import*("bird:/**.pkl").keys""",
expr = "res"
)
)
val listModulesMsg = client.receive<ListModulesRequest>()
client.send(
ListModulesResponse(
requestId = listModulesMsg.requestId,
evaluatorId = evaluatorId,
pathElements = null,
error = null
)
)

val evaluateResponse = client.receive<EvaluateResponse>()
assertThat(evaluateResponse.result!!.debugRendering)
.isEqualTo(
"""
- 6
- []
"""
.trimIndent()
)
}

@Test
fun `glob module error`() {
val reader =
Expand Down

0 comments on commit 7da643f

Please sign in to comment.