How to change state in a migration

I want to change the state of a protocol in a migration. I think I cannot do the following:

val invitationStateActive = "active"
val invitationStateRejected = "rejected"
val tenantStateDeactivated = "deactivated"

migration("NPL $fromVersion to $toVersion")
    .transformProtocol("/paas-$fromVersion?/paas/Invitation", "/paas-$toVersion?/paas/Invitation") {
        val invitationState = this.activeState
        val tenantState = this.get("tenant").value.activeState
        if (tenantState == tenantStateDeactivated) {
            this.activeState = invitationStateRejected
        }
    }.retag(protos)

because this.activeState is not reassignable.
what is the way to do this?

is not really clear on how to use it. could I do:

if (tenantState == tenantStateDeactivated) {
    state { invitationStateRejected }
}

?

Hi Ed,

Yes you can use the state { ... } syntax to modify states in the state of a protocol instance.

In more details, the state function takes a function as parameter that transforms the state of the instance to the next state of that instance.

Note: it has nothing to do with the list of available states in the protocol declaration. See below for details.

So valid syntaxes are

state { "myNewState" }
state { it }
state { it -> myFunction(it) }
state { myFunction(it) }
state { oldState ->
    if (oldState == "old") {
        "new"
    } else {
        oldState
    }
}

Or more complicated logic. This function can be applied to none, some or all of the protocol instances of a type, meaning the condition can be either surrounding or included in the state function.

Hope this helps

State machine migration
The list of available states in a protocol declaration can be updated in a migration with the following syntax

.transformEnum(
    currentDerivedTypeId = StatesEnumTypeId(
        protocolTypeId = "/npldemo-$fromVersion?/demo/HelloWorld"
    ),
    targetDerivedTypeId = StatesEnumTypeId( // state inactive removed
        protocolTypeId = "/npldemo-$toVersion?/demo/HelloWorld"
    ),
    mappings = mapOf(
        "INACTIVE" to "ACTIVE"
    )
)