Skip to content

Commit ce7d634

Browse files
Fix: Explicitly reconstruct map for FunctionCallPartDto mapping
Addresses a persistent compilation type mismatch error in PhotoReasoningMappers.kt when mapping FunctionCallPartDto to the SDK's FunctionCallPart, specifically for the 'args' field. Previous attempts using direct assignment and explicit casting did not resolve the issue where the compiler reported a mismatch despite seemingly identical types (Map<String, String?>?). This commit modifies the `PartDto.toSdk()` function for the `FunctionCallPartDto` case. Instead of directly passing or casting `this.args`, a new `Map<String, String?>` is explicitly constructed by iterating over the entries of `this.args` (if not null) and populating the new map. This explicit reconstruction provides a 'fresh' map instance with a clearly defined type to the SDK's `FunctionCallPart` constructor, aiming to resolve any subtle type system incompatibilities that the compiler might have been struggling with.
1 parent 8cff988 commit ce7d634

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

app/src/main/kotlin/com/google/ai/sample/feature/multimodal/dtos/PhotoReasoningMappers.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,18 @@ fun PartDto.toSdk(): Part { // No context needed here as path is absolute
5555
?: throw IllegalArgumentException("Failed to load Bitmap from file path: ${this.imageFilePath}, or file was invalid.")
5656
}
5757
is BlobPartDto -> BlobPart(mimeType = this.mimeType, blob = this.data)
58-
is FunctionCallPartDto -> FunctionCallPart(name = this.name, args = this.args as Map<String, String?>?)
58+
is FunctionCallPartDto -> {
59+
val sdkArgs: Map<String, String?>? = this.args?.let { dtoArgs ->
60+
// Explicitly create a new map to ensure the compiler understands the type correctly.
61+
val newMap = mutableMapOf<String, String?>()
62+
dtoArgs.entries.forEach { entry -> // Iterate through entries
63+
newMap[entry.key] = entry.value
64+
}
65+
newMap.toMap() // Convert to immutable Map if constructor expects Map, or just newMap if MutableMap is fine.
66+
// Assuming FunctionCallPart constructor takes Map<String, String?>?
67+
}
68+
FunctionCallPart(name = this.name, args = sdkArgs)
69+
}
5970
is FunctionResponsePartDto -> {
6071
// Convert responseJson String back to org.json.JSONObject
6172
val jsonObject = try {

0 commit comments

Comments
 (0)