-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parse variant or polymorphic variant #76
Comments
Hi @mattdamon108 – please see #36 As for a workaround, you can use |
Working lovely! Thanks! @decco
type orderStatus = PROCESSING | SUCCESS | FAIL
@decco
type order = {
@decco.key("order-status") orderStatus: @decco.codec(Decco.Codecs.magic) orderStatus,
} |
I found that the above my workaround is parsing it to a string, not a variant. Therefore, I had to make a custom encoder and decoder to parse as a result. type orderStatus = PROCESSING | SUCCESS | FAIL
let encoderOrderStatus = v =>
switch v {
| PROCESSING => "CREATE"
| SUCCESS => "STANDBY"
| FAIL => "DELIVERING"
}->Js.Json.string
let decoderOrderStatus = json => {
switch json |> Js.Json.classify {
| Js.Json.JSONString(str) =>
switch str {
| "PROCESSING" => PROCESSING->Ok
| "SUCCESS" => SUCCESS->Ok
| "FAIL" => FAIL->Ok
| _ => Error({Decco.path: "", message: "Expected JSONString", value: json})
}
| _ => Error({Decco.path: "", message: "Expected JSONString", value: json})
}
}
let codecOrderStatus: Decco.codec<orderStatus> = (encoderOrderStatus, decoderOrderStatus)
@decco
type order = {
@decco.key("order-status") orderStatus: @decco.codec(codecOrderStatus) orderStatus,,
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I realized that parsing the variant or polymorphic variant doesn't work with just a string that is the same as the variant or polymorphic variant. I found that the JSON value should be an array type. For example,
I think it is reasonable in a way. But, parsing "SUCCESS" into variant or polymorphic variant would be also reasonable. Is there any workaround to parse it in this way or any plan?
The text was updated successfully, but these errors were encountered: