fix NPE in Jankson and finetune hjson loading

This commit is contained in:
nikky 2018-06-26 19:08:36 +02:00
parent 88bee22010
commit 1356e3682f
6 changed files with 52 additions and 49 deletions

@ -1 +1 @@
Subproject commit 90b600e2acdbba48a9a3c39d85a49a9ec24e8db1
Subproject commit 8771387a0568da140879c0104da32ba58e3bb717

View File

@ -57,7 +57,6 @@ val Exception.stackTraceString: String
}
fun <T : Any> JsonObject.getOrDefault(key: String, default: T, comment: String? = null): T {
instance.info("type: ${default.javaClass.name} key: $key json: >>>${this.getObject(key)?.toJson()}<<< default: $default")
putDefault(key, default, comment)!!
return get(default.javaClass, key)!!
// instance.info("type: ${default.javaClass.name} key: $key json: >>>${this.getObject(key)?.toJson()}<<< default: $default")
return putDefault(key, default, comment)!!
}

View File

@ -36,7 +36,7 @@ object PermCommand : IBridgeCommand {
if(currentPowerlevel < 0.0) {
MessageHandlerInst.transmit(
ApiMessage(
text = "Your poermission level is $currentPowerlevel seems like someone banned you from making any more requests"
text = "Your level is $currentPowerlevel seems like someone banned you from making any more requests"
)
)
return true

View File

@ -10,7 +10,6 @@ import matterlink.instance
import matterlink.stackTraceString
import java.io.File
import java.io.FileNotFoundException
import kotlin.system.exitProcess
lateinit var cfg: BaseConfig.MatterLinkConfig
lateinit var baseCfg: BaseConfig
@ -374,15 +373,18 @@ data class BaseConfig(val rootDir: File) {
instance.info("loaded config: $this")
}
} catch (e: SyntaxError) {
instance.error("error parsing config: ${e.completeMessage} ${e.stackTraceString}")
instance.error("error parsing config: ${e.completeMessage} ")
instance.error(e.stackTraceString)
cfgDirectory.resolve("error.matterlink.hjson").writeText(jsonObject.toJson(false, true))
MatterLinkConfig()
} catch (e: IllegalStateException) {
instance.error(e.stackTraceString)
cfgDirectory.resolve("error.matterlink.hjson").writeText(jsonObject.toJson(false, true))
MatterLinkConfig()
} catch (e: NullPointerException) {
instance.error("error loading config: ${e.stackTraceString}")
cfgDirectory.resolve("error.matterlink.hjson").writeText(jsonObject.toJson(false, true))
MatterLinkConfig()
exitProcess(-1)
}
// val defaultJsonObject = jankson.load("{}")

View File

@ -6,6 +6,7 @@ import blue.endless.jankson.JsonPrimitive
import blue.endless.jankson.impl.SyntaxError
import matterlink.bridge.command.CommandType
import matterlink.bridge.command.CustomCommand
import matterlink.getOrDefault
import matterlink.instance
import java.io.File
import java.io.FileNotFoundException
@ -111,7 +112,7 @@ object CommandConfig {
jsonObject.forEach { key, element ->
instance.trace("loading command '$key'")
val command = jsonObject.get(CustomCommand::class.java, key)
if(command != null)
if (command != null)
commands[key] = command
else {
instance.error("could not parse key: $key, value: '$element' as CustomCommand")
@ -124,20 +125,22 @@ object CommandConfig {
val command = commands[k]
if (command == null || command.defaultCommand == true) {
commands[k] = defCommand
val element = jankson.marshaller.serialize(defCommand)
jsonObject.putDefault(k, element, comment)
jsonObject.getOrDefault(k, defCommand, comment)
}
}
instance.debug("loaded jsonObj: $jsonObject")
instance.debug("loaded commandMap: $commands")
val nonDefaultJsonObj = jsonObject.getDelta(jankson.marshaller.serialize(default.mapValues { it.value.second }) as JsonObject)
val defaultJsonObject = jankson.marshaller.serialize(CustomCommand.DEFAULT) as JsonObject
val nonDefaultJsonObj = jsonObject.clone()
jsonObject.forEach { key, element ->
if (element is JsonObject) {
nonDefaultJsonObj[key] = element.getDelta(defaultJsonObject)
}
}
configFile.writeText(nonDefaultJsonObj.toJson(true, true))
return true
}
}

View File

@ -41,51 +41,41 @@ object PermissionConfig {
fun loadPermFile(): Boolean {
permissionRequests.clear()
val defaultJsonObject = JsonObject().apply {
default.forEach { platform, userMap ->
val jsonUserMap = this.getOrDefault(platform, JsonObject())
if (jsonUserMap is JsonObject) {
userMap.forEach { user, (powerlevel, comment) ->
instance.trace("loading platform: $platform user: $user powerlevel: $powerlevel")
val element = Marshaller.getFallback().serialize(powerlevel)
jsonUserMap.putDefault(user, element, comment.takeUnless { it.isBlank() })
}
this[platform] = jsonUserMap
} else {
instance.error("cannot parse platform: $platform , value: '$jsonUserMap' as Map, skipping")
}
}
}
var save = true
jsonObject = try {
jankson.load(configFile)
} catch (e: SyntaxError) {
instance.error("error parsing config: ${e.completeMessage}")
JsonObject()
save = false
defaultJsonObject
} catch (e: FileNotFoundException) {
instance.error("cannot find config: $configFile .. creating sample permissions mapping")
configFile.createNewFile()
JsonObject()
defaultJsonObject
}
default.forEach { platform, userMap ->
val jsonUserMap = jsonObject.getOrDefault(platform, JsonObject())
if(jsonUserMap is JsonObject) {
userMap.forEach { user, (powerlevel, comment) ->
instance.trace("loading platform: $platform user: $user powerlevel: $powerlevel")
val element = Marshaller.getFallback().serialize(powerlevel)
jsonUserMap.putDefault(user, element, comment.takeUnless { it.isBlank() })
}
jsonObject[platform] = jsonUserMap
} else {
instance.error("cannot parse platform: $platform , value: '$jsonUserMap' as Map, skipping")
}
}
jsonObject.forEach { platform, jsonUserMap ->
val userMap = perms[platform] ?: mutableMapOf()
if (jsonUserMap is JsonObject) {
jsonUserMap.forEach { user, powerlevel ->
instance.info("$platform $user $powerlevel")
userMap[user] = jsonUserMap.get(Double::class.java, user) ?: 0.0
}
}
perms[platform] = userMap
}
configFile.writeText(jsonObject.toJson(true, true))
load(save)
return true
}
fun add(platform: String, userid: String, powerlevel: Double, comment: String? = null) {
val platformObject = jsonObject.getObject(platform) ?: JsonObject()
platformObject.putDefault(userid, powerlevel, comment)
jsonObject[platform] = platformObject
private fun load(save: Boolean = true) {
perms.clear()
jsonObject.forEach { platform, jsonUserMap ->
val userMap = perms[platform] ?: mutableMapOf()
@ -99,6 +89,15 @@ object PermissionConfig {
}
instance.info("Permissions reloaded")
configFile.writeText(jsonObject.toJson(true, true))
if (save)
configFile.writeText(jsonObject.toJson(true, true))
}
fun add(platform: String, userid: String, powerlevel: Double, comment: String? = null) {
val platformObject = jsonObject.getObject(platform) ?: JsonObject()
platformObject.putDefault(userid, powerlevel, comment)
jsonObject[platform] = platformObject
load()
}
}