Fix data comparison, fix commands

This commit is contained in:
Him188 2020-09-09 22:49:48 +08:00
parent 6ec3390b8d
commit a6bd7b7d63
6 changed files with 64 additions and 9 deletions

View File

@ -183,7 +183,7 @@ public object PluginDataExtensions {
private fun <K, V> createDelegateInstance(
origin: SerializerAwareValue<MutableMap<K, V>>,
defaultValueComputer: (K) -> V
defaultValueComputer: (K) -> V,
): MutableMap<K, V> {
return object : MutableMap<K, V>, AbstractMap<K, V>() {
override val entries: MutableSet<MutableMap.MutableEntry<K, V>> get() = origin.value.entries

View File

@ -58,6 +58,26 @@ public class SerializableValue<T>(
) : Value<T> by delegate, SerializerAwareValue<T> {
public override fun toString(): String = delegate.toString()
public override fun equals(other: Any?): Boolean {
if (other === this) return true
if (other?.javaClass != this.javaClass) return false
@Suppress("UNCHECKED_CAST")
other as SerializableValue<T>
if (other.delegate != this.delegate) return false
// if (other.serializer != this.serializer) return false
// TODO: 2020/9/9 serializers should be checked here, but it will cause incomparable issue when putting a SerializableValue as a Key
return true
}
override fun hashCode(): Int {
@Suppress("UnnecessaryVariable", "CanBeVal")
var result = delegate.hashCode()
// result = 31 * result + serializer.hashCode()
// TODO: 2020/9/9 serializers should be checked here, but it will cause incomparable issue when putting a SerializableValue as a Key
return result
}
public companion object {
@JvmStatic
@JvmName("create")

View File

@ -196,4 +196,21 @@ internal class LazyReferenceValueImpl<T> : ReferenceValue<T>, AbstractValueImpl<
initialied = true
valueField = value
}
override fun toString(): String {
return valueField.toString()
}
override fun equals(other: Any?): Boolean {
if (other === this) return true
if (other?.javaClass != this.javaClass) return false
other as LazyReferenceValueImpl<*>
if (other.valueField != valueField) return false
return true
}
override fun hashCode(): Int {
return valueField?.hashCode() ?: 0
}
}

View File

@ -17,7 +17,9 @@ import net.mamoe.mirai.console.util.ConsoleExperimentalAPI
/**
* 一个权限节点.
*
* [PermissionService] 实现不同, [Permission] 可能会有多种实例. 但一个权限总是拥有确定的 [id]
* [PermissionService] 实现不同, [Permission] 可能会有多种实例. 但一个权限总是拥有确定的 [id].
*
* 请不要手动实现这个接口. 总是从 [PermissionService.register] 获得实例.
*/
@ExperimentalPermission
public interface Permission {
@ -26,15 +28,20 @@ public interface Permission {
public val parentId: PermissionId
}
@OptIn(ExperimentalPermission::class)
private val ROOT_PERMISSION_ID = PermissionId("*", "*")
/**
* 所有权限的父权限.
*/
@get:JvmName("getRootPermission")
@ExperimentalPermission
public object RootPermission :
Permission {
override val id: PermissionId = PermissionId("*", "*")
override val description: String get() = "The parent of any permission"
override val parentId: PermissionId get() = id
public val RootPermission: Permission by lazy {
PermissionService.INSTANCE.register(
ROOT_PERMISSION_ID,
"The parent of any permission",
ROOT_PERMISSION_ID
)
}
@ConsoleExperimentalAPI

View File

@ -189,7 +189,7 @@ internal class TestCommand {
val composite = object : CompositeCommand(
ConsoleCommandOwner,
"test",
"test22",
overrideContext = buildCommandArgumentContext {
add(object : CommandArgumentParser<MyClass> {
override fun parse(raw: String, sender: CommandSender): MyClass {
@ -234,7 +234,7 @@ internal class TestCommand {
simple.withRegistration {
// assertEquals("xxx", withTesting { simple.execute(sender, "xxx") })
assertEquals("xxx", withTesting { println(sender.executeCommand("/test xxx")) })
assertEquals("xxx", withTesting { assertSuccess(sender.executeCommand("/test xxx")) })
}
}
}

View File

@ -0,0 +1,11 @@
package net.mamoe.mirai.console.permission
import org.junit.jupiter.api.Test
internal class PermissionsBasicsTest {
@Test
fun parentsWithSelfSequence() {
}
}