Kotlin - enum 类,如何知道条目映射到什么类型的值?
Kotlin - enum classes, how to know to what type of values entries map to?
对于菜鸟问题,我深表歉意,但我是第一次使用 Kotlin 和枚举 classes,所以我想知道枚举中条目的值是多少 class映射到。这是枚举 class:
enum class OpenCommandTypes {
ADD_META_DATA,
PURCHASE_ORDER,
}
它用于从 url:
中提取的命令名称
path("/api/shipment/{shipment-number}") {
post("/{command-name}") { handleOpenCommand(ctx, redisWsHandler, conf) }
}
并且按 command-name
函数的类型被调用:
val result = when (openCommandType) {
PURCHASE_ORDER -> processOpenCommand(ipAddress, wsHandler, ctx, openCommandType, command, ::purchaseOrder)
此函数检查字符串是否与 valueOf 枚举条目匹配:
internal fun safeOpenCommandType(type: String): Either<java.lang.Exception, OpenCommandTypes> =
try {
OpenCommandTypes.valueOf(type).right()
} catch (e: IllegalArgumentException) {
e.left()
}
所以,这是命令名称映射到函数的整个函数:
internal fun delegateOpenCommand(ipAddress: String?, wsHandler: WSHandler, ctx: Ctx, type: String, command: JsonObject): Either<Failure, Response> {
logger.info("Open Command of type '$type'")
return safeOpenCommandType(type).fold({
logger.info("Unknown command type '$type'")
Either.Left(Failure.Ex(Exception("Unknown open command type '$type'"), CommandErrorCode.BAD_REQUEST))
}, { openCommandType ->
val result = when (openCommandType) {
PURCHASE_ORDER -> processOpenCommand(ipAddress, wsHandler, ctx, openCommandType, command, ::purchaseOrder)
我假设枚举条目的值是字符串 PURCHASE_ORDER,因为 when
条件中的 openCommandType
是 command-name
字符串的值 url。我不太了解枚举class,所以我想知道我如何知道每个条目映射到什么以及它持有什么样的值?
Kotlin 的枚举不会“映射到”任何东西(至少在某些其他语言的意义上不是)。它们就像常规的 类,但具有众所周知的有限数量的实例。所以枚举中的值是该枚举的类型(不是 String,也不是 Int)。
它们不同于 TypeScript 等语言中的枚举,其中枚举值映射到其他一些类型,如 Int 或 String。
在您的例子中,调用 OpenCommandTypes.valueOf(type)
显式地将字符串值 "ADD_META_DATA"
或 "PURCHASE_ORDER"
转换为类型 OpenCommandTypes
.
的相应枚举值
Side note: this is also usually why it's preferred to used singular for enum classes, because it's really just a type like any other class. ADD_META_DATA
is one value of type OpenCommandTypes
, so it would be better if it were named OpenCommandType
.
valueOf()
方法是所有枚举中自动生成的特殊方法类,它有助于将等于枚举值名称的字符串值转换为枚举值本身。
I assume that the value of enum entry is the string PURCHASE_ORDER, since the openCommandType in when condition is the value of the command-name string from the url.
不,openCommandType
变量不是来自 URL 的字符串,它实际上是来自 valueOf()
的结果的值,但它经过 [=20] =]+fold()
机器所以这里有点难看。
对于菜鸟问题,我深表歉意,但我是第一次使用 Kotlin 和枚举 classes,所以我想知道枚举中条目的值是多少 class映射到。这是枚举 class:
enum class OpenCommandTypes {
ADD_META_DATA,
PURCHASE_ORDER,
}
它用于从 url:
中提取的命令名称path("/api/shipment/{shipment-number}") {
post("/{command-name}") { handleOpenCommand(ctx, redisWsHandler, conf) }
}
并且按 command-name
函数的类型被调用:
val result = when (openCommandType) {
PURCHASE_ORDER -> processOpenCommand(ipAddress, wsHandler, ctx, openCommandType, command, ::purchaseOrder)
此函数检查字符串是否与 valueOf 枚举条目匹配:
internal fun safeOpenCommandType(type: String): Either<java.lang.Exception, OpenCommandTypes> =
try {
OpenCommandTypes.valueOf(type).right()
} catch (e: IllegalArgumentException) {
e.left()
}
所以,这是命令名称映射到函数的整个函数:
internal fun delegateOpenCommand(ipAddress: String?, wsHandler: WSHandler, ctx: Ctx, type: String, command: JsonObject): Either<Failure, Response> {
logger.info("Open Command of type '$type'")
return safeOpenCommandType(type).fold({
logger.info("Unknown command type '$type'")
Either.Left(Failure.Ex(Exception("Unknown open command type '$type'"), CommandErrorCode.BAD_REQUEST))
}, { openCommandType ->
val result = when (openCommandType) {
PURCHASE_ORDER -> processOpenCommand(ipAddress, wsHandler, ctx, openCommandType, command, ::purchaseOrder)
我假设枚举条目的值是字符串 PURCHASE_ORDER,因为 when
条件中的 openCommandType
是 command-name
字符串的值 url。我不太了解枚举class,所以我想知道我如何知道每个条目映射到什么以及它持有什么样的值?
Kotlin 的枚举不会“映射到”任何东西(至少在某些其他语言的意义上不是)。它们就像常规的 类,但具有众所周知的有限数量的实例。所以枚举中的值是该枚举的类型(不是 String,也不是 Int)。
它们不同于 TypeScript 等语言中的枚举,其中枚举值映射到其他一些类型,如 Int 或 String。
在您的例子中,调用 OpenCommandTypes.valueOf(type)
显式地将字符串值 "ADD_META_DATA"
或 "PURCHASE_ORDER"
转换为类型 OpenCommandTypes
.
Side note: this is also usually why it's preferred to used singular for enum classes, because it's really just a type like any other class.
ADD_META_DATA
is one value of typeOpenCommandTypes
, so it would be better if it were namedOpenCommandType
.
valueOf()
方法是所有枚举中自动生成的特殊方法类,它有助于将等于枚举值名称的字符串值转换为枚举值本身。
I assume that the value of enum entry is the string PURCHASE_ORDER, since the openCommandType in when condition is the value of the command-name string from the url.
不,openCommandType
变量不是来自 URL 的字符串,它实际上是来自 valueOf()
的结果的值,但它经过 [=20] =]+fold()
机器所以这里有点难看。