extension function for Chatclient.CalLResponseSpec.entity(Class<?>) and Kotlin

This commit is contained in:
Josh Long
2024-11-12 12:02:50 -08:00
parent 6acd202c12
commit f50b43e256
2 changed files with 65 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ai.chat.client
/**
* Extension for [ChatClient] providing a `entity<Foo>()` variant.
*
* @author Josh Long
*/
inline fun <reified T> ChatClient.CallResponseSpec.entity(): T = entity(T::class.java) as T

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ai.chat.client
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.Test
class ChatClientExtensionsTests {
private val crs = mockk<ChatClient.CallResponseSpec>()
data class Joke(val setup: String, val punchline: String)
@Test
fun withEntityType() {
val joke = Joke(
setup = "Why did the scarecrow win an award?",
punchline = "Because he was outstanding in his field!"
)
every { crs.entity(any<Class<*>>()) } returns joke
crs.entity<Joke>()
verify { crs.entity(Joke::class.java) }
}
}