Skip to content

Commit dee76a3

Browse files
committed
midi-ci CommonRules property binaries are now retrieved via a virtual function.
- instead of linkedResources map.
1 parent 277b11c commit dee76a3

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

ktmidi-ci/src/commonMain/kotlin/dev/atsushieno/ktmidi/ci/propertycommonrules/PropertyCommonRules.Service.kt

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ private val defaultPropertyList = listOf(
1212
CommonRulesPropertyMetadata(PropertyResourceNames.JSON_SCHEMA).apply { originator = CommonRulesPropertyMetadata.Originator.SYSTEM }
1313
)
1414

15-
class CommonRulesPropertyService(private val device: MidiCIDevice)
15+
open class CommonRulesPropertyService(private val device: MidiCIDevice)
1616
: MidiCIServicePropertyRules {
1717
private val helper = CommonRulesPropertyHelper(device)
1818
private val logger by device::logger
@@ -70,7 +70,7 @@ class CommonRulesPropertyService(private val device: MidiCIDevice)
7070
return Result.failure(ex)
7171
}
7272

73-
val result = getPropertyData(jsonInquiry)
73+
val result = getPropertyDataEncoded(jsonInquiry)
7474

7575
val replyHeader = MidiCIConverter.encodeStringToASCII(Json.serialize(result.first)).toASCIIByteArray().toList()
7676
val replyBody = result.second
@@ -136,6 +136,18 @@ class CommonRulesPropertyService(private val device: MidiCIDevice)
136136

137137
val linkedResources = mutableMapOf<String, List<Byte>>()
138138

139+
/**
140+
* Gets a binary resource by resource ID.
141+
* Override this method to provide dynamic resource retrieval.
142+
* The default implementation uses the linkedResources map.
143+
*
144+
* @param resId The resource ID to retrieve
145+
* @return The binary data for the resource, or null if not found
146+
*/
147+
open fun getBinaryResource(resId: String?): List<Byte>? {
148+
return if (resId != null) linkedResources[resId] else null
149+
}
150+
139151
private fun getPropertyHeader(json: Json.JsonValue) =
140152
PropertyCommonRequestHeader(
141153
json.getObjectValue(PropertyCommonHeaderKeys.RESOURCE)?.stringValue ?: "",
@@ -170,7 +182,7 @@ class CommonRulesPropertyService(private val device: MidiCIDevice)
170182
PropertyResourceNames.CHANNEL_LIST -> FoundationalResources.toJsonValue(channelList)
171183
PropertyResourceNames.JSON_SCHEMA -> if (device.config.jsonSchemaString.isNotBlank()) Json.parse(device.config.jsonSchemaString) else null
172184
else -> {
173-
val bytes = linkedResources[header.resId] ?: values.firstOrNull { it.id == header.resource }?.body
185+
val bytes = getBinaryResource(header.resId) ?: values.firstOrNull { it.id == header.resource }?.body
174186
?: throw PropertyExchangeException("Unknown property: ${header.resource} (resId: ${header.resId}")
175187
if (bytes.any()) Json.parse(bytes.toByteArray().decodeToString()) else Json.EmptyObject
176188
}
@@ -189,16 +201,15 @@ class CommonRulesPropertyService(private val device: MidiCIDevice)
189201
return Pair(getReplyHeaderJson(PropertyCommonReplyHeader(PropertyExchangeStatus.OK, mutualEncoding = header.mutualEncoding, totalCount = totalCount)), paginatedBody ?: Json.EmptyObject)
190202
}
191203

192-
// It returns the *encoded* result.
193-
private fun getPropertyData(headerJson: Json.JsonValue): Pair<Json.JsonValue, List<Byte>> {
204+
private fun getPropertyDataEncoded(headerJson: Json.JsonValue): Pair<Json.JsonValue, List<Byte>> {
194205
val header = getPropertyHeader(headerJson)
195206
if (header.mediaType == null || header.mediaType == CommonRulesKnownMimeTypes.APPLICATION_JSON) {
196207
val ret = getPropertyDataJson(header)
197208
val body = MidiCIConverter.encodeStringToASCII(Json.serialize(ret.second)).toASCIIByteArray().toList()
198209
val encodedBody = encodeBody(body, header.mutualEncoding)
199210
return Pair(ret.first, encodedBody)
200211
} else {
201-
val body = linkedResources[header.resId] ?: values.firstOrNull { it.id == header.resource }?.body
212+
val body = getBinaryResource(header.resId) ?: values.firstOrNull { it.id == header.resource }?.body
202213
?: listOf()
203214
val encodedBody = encodeBody(body, header.mutualEncoding)
204215
val replyHeader = getReplyHeaderJson(PropertyCommonReplyHeader(PropertyExchangeStatus.OK, mutualEncoding = header.mutualEncoding))

0 commit comments

Comments
 (0)