From ee81ac9937a7d087fbbe3891f595e87a2ba8d781 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 1 Nov 2019 22:58:30 +0100 Subject: [PATCH] =?utf8?q?=E2=99=BB=EF=B8=8F=20Refactor=20property=20and?= =?utf8?q?=20context=20parsing?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/freenet/wot/PluginWebOfTrustConnector.kt | 39 +++++++++------------- 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnector.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnector.kt index 96b906b..b0dfa37 100644 --- a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnector.kt +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnector.kt @@ -45,8 +45,8 @@ class PluginWebOfTrustConnector @Inject constructor(private val pluginConnector: val insertUri = fields.get("InsertURI$ownIdentityCounter") val nickname = fields.get("Nickname$ownIdentityCounter") val ownIdentity = DefaultOwnIdentity(id, nickname, requestUri, insertUri) - ownIdentity.setContexts(parseContexts("Contexts$ownIdentityCounter.", fields)) - ownIdentity.properties = parseProperties("Properties$ownIdentityCounter.", fields) + ownIdentity.setContexts(fields.contexts("Contexts$ownIdentityCounter.")) + ownIdentity.properties = fields.properties("Properties$ownIdentityCounter.") ownIdentities.add(ownIdentity) } return ownIdentities @@ -61,8 +61,8 @@ class PluginWebOfTrustConnector @Inject constructor(private val pluginConnector: val nickname = fields.get("Nickname$identityCounter") val requestUri = fields.get("RequestURI$identityCounter") val identity = DefaultIdentity(id, nickname, requestUri) - identity.setContexts(parseContexts("Contexts$identityCounter.", fields)) - identity.properties = parseProperties("Properties$identityCounter.", fields) + identity.setContexts(fields.contexts("Contexts$identityCounter.")) + identity.properties = fields.properties("Properties$identityCounter.") val trust = parseInt(fields.get("Trust$identityCounter"), null) val score = parseInt(fields.get("Score$identityCounter"), 0)!! val rank = parseInt(fields.get("Rank$identityCounter"), 0)!! @@ -145,23 +145,14 @@ class PluginWebOfTrustConnector @Inject constructor(private val pluginConnector: private const val WOT_PLUGIN_NAME = "plugins.WebOfTrust.WebOfTrust" -private fun parseContexts(prefix: String, fields: SimpleFieldSet): Set { - val contexts = HashSet() - var contextCounter = -1 - while (true) { - val context = fields.get(prefix + "Context" + ++contextCounter) ?: break - contexts.add(context) - } - return contexts -} - -private fun parseProperties(prefix: String, fields: SimpleFieldSet): Map { - val properties = HashMap() - var propertiesCounter = -1 - while (true) { - val propertyName = fields.get(prefix + "Property" + ++propertiesCounter + ".Name") ?: break - val propertyValue = fields.get(prefix + "Property" + propertiesCounter + ".Value") - properties[propertyName] = propertyValue - } - return properties -} +private fun SimpleFieldSet.contexts(prefix: String) = + generateSequence(0, Int::inc) + .map { get("${prefix}Context$it") } + .takeWhile { it != null } + .toList() + +private fun SimpleFieldSet.properties(prefix: String) = + generateSequence(0, Int::inc) + .takeWhile { get("${prefix}Property${it}.Name") != null } + .map { get("${prefix}Property${it}.Name") to get("${prefix}Property${it}.Value") } + .toMap() -- 2.7.4