OneMap
OneMap
This function takes two generic types:
T
(the type of data held by theResource
) andR
(the type of data after transformation).It accepts a lambda function
transform
that takes a value of typeT
and returns a value of typeR
.It then uses a
when
expression to handle different states of theResource
:Success: If the
Resource
is successful, it applies thetransform
function to the data and creates a newResource.Success
with the transformed data of typeR
.Failure: If the
Resource
has failed, it creates a newResource.Failure
with the original error message.Loading, Default, Empty: It simply creates a new
Resource
of the same type (Loading, Default, or Empty) without modifying the state.
Usage:
OneMapList
This function is similar to
oneMap
but specifically designed forResource<List<T>>
.It transforms a list of data within the
Resource
.It uses the
map
function on the data list to apply thetransform
function to each element and create a new list of transformed data (List<R>
)Similar to
oneMap
, it handles different states of theResource
using awhen
expression.
Usage:
This example shows how to use oneMapList
to transform a list of strings within a Resource
to uppercase. You can use oneMap
in a similar way for other data types.
These extension functions promote cleaner code by avoiding repetitive checks for resource state and transforming data within the Resource
type itself.
Last updated