Formatting fields into correct form #36
-
So I am in the process of getting started with the library and am a bit confused about how we could map on the fields to convert them to a usable format. So here is a situation where I want to uppercase the from wallet value before initialising my enum as the enum has an uppercase raw value requirement. Is this possible? Tried to look at the docs but with no luck. Full Error: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I think this is because There's a special conversation you can use. Something like:
|
Beta Was this translation helpful? Give feedback.
-
Yep, @robfeldmann is right! You can also extract it out to an extension if doing all of that inline is too intense: extension Conversion where Self == AnyConversion<Substring, Substring> {
static var uppercase: AnyConversion<Input, Output> {
AnyConversion(apply: { $0.uppercased()[...] }, unapply: { $0.uppercased()[...] })
}
} Then you can use it like so: Field("fromWallet").map(.uppercase) Also note that what you put in the |
Beta Was this translation helpful? Give feedback.
I think this is because
map
by itself isn't enough for parsing and printing. You will have to define how to go both ways.There's a special conversation you can use. Something like: