Reputation: 1360
I apologise for the noobie question, but I am working for the first time with Kotlin and enum classes, so I am wondering what does the value of an entry in an enum class map to. This is the enum class:
enum class OpenCommandTypes {
ADD_META_DATA,
PURCHASE_ORDER,
}
It is being used for command name that is extracted from a url:
path("/api/shipment/{shipment-number}") {
post("/{command-name}") { handleOpenCommand(ctx, redisWsHandler, conf) }
}
And by type of command-name
function is being called:
val result = when (openCommandType) {
PURCHASE_ORDER -> processOpenCommand(ipAddress, wsHandler, ctx, openCommandType, command, ::purchaseOrder)
This function does the check if the string matches the valueOf enum entry:
internal fun safeOpenCommandType(type: String): Either<java.lang.Exception, OpenCommandTypes> =
try {
OpenCommandTypes.valueOf(type).right()
} catch (e: IllegalArgumentException) {
e.left()
}
So, this is the whole function where command name is mapped to function:
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)
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. I don't know much about enum class, so I wonder how do I know what each entry maps to and what kind of type of value it holds?
Upvotes: 1
Views: 392
Reputation: 37799
Kotlin's enums don't "map to" anything (at least not in the sense of some other languages). They are just like regular classes, but with a well-known finite amount of instances. So the values in an enum are of the type of that enum (not String, nor Int).
They are different from enums in languages like TypeScript where the enum values map to some other types like Int or String.
In your case, the call OpenCommandTypes.valueOf(type)
explicitly converts the string value "ADD_META_DATA"
or "PURCHASE_ORDER"
into the corresponding enum value of type 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
.
The valueOf()
method is a special auto-generated method in all enum classes that help convert string values equal to the name of an enum value into the enum value itself.
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.
No, the openCommandType
variable is not the string from the URL, it's actually a value that comes from the result of valueOf()
, but it goes through the Either
+fold()
machinery so it's kinda hard to see here.
Upvotes: 2