Allow to convert a Set() to a List() in the Firestore rules
There is a very annoying limitation in the Firestore rules: you cannot convert a Set() to a List().
This prevents a lot of possibility especially when using Map().diff().affectedKeys(): there is no way to access the first element for example.
My use case is the following:
On my client side, I am incrementing the value of a map element, ie:
MapFieldValue resources;
resources["gold"] = FieldValue::Increment(-amount);
And on the rules side, I want to ensure that the resulting value is always positive (ie you cannot spend more gold than you have, even concurrently!):
let updatedKeys = (resource.data.diff(request.resource.data).affectedKeys());
return (updatedKeys.size() == 1 && request.resource.data[updatedKeys.toList()[0]] >= 0);
But without the "toList()" function, I cannot get the value of the updated key in the Map. There is no way to access a Set() element other than by knowing the key.
My actual workaround is to set a "temporary" String field in the request with the value of the updated key, but this is more a "hack"...