Handle float values without decimal point
This commit is contained in:
parent
aed9b6413f
commit
de20cb5001
2 changed files with 12 additions and 2 deletions
|
@ -1,4 +1,5 @@
|
|||
import gleam/dynamic/decode.{type Decoder}
|
||||
import gleam/int
|
||||
import gleam/json.{type Json}
|
||||
|
||||
pub opaque type ShipComponentCondition {
|
||||
|
@ -17,7 +18,11 @@ pub fn parse(value: Float) -> Result(ShipComponentCondition, Nil) {
|
|||
}
|
||||
|
||||
pub fn decoder() -> Decoder(ShipComponentCondition) {
|
||||
use value <- decode.then(decode.float)
|
||||
use value <- decode.then(
|
||||
decode.one_of(decode.float, [
|
||||
decode.then(decode.int, fn(i) { decode.success(int.to_float(i)) }),
|
||||
]),
|
||||
)
|
||||
case parse(value) {
|
||||
Ok(ship_component_condition) -> decode.success(ship_component_condition)
|
||||
Error(Nil) ->
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import gleam/dynamic/decode.{type Decoder}
|
||||
import gleam/int
|
||||
import gleam/json.{type Json}
|
||||
|
||||
pub opaque type ShipComponentIntegrity {
|
||||
|
@ -17,7 +18,11 @@ pub fn parse(value: Float) -> Result(ShipComponentIntegrity, Nil) {
|
|||
}
|
||||
|
||||
pub fn decoder() -> Decoder(ShipComponentIntegrity) {
|
||||
use value <- decode.then(decode.float)
|
||||
use value <- decode.then(
|
||||
decode.one_of(decode.float, [
|
||||
decode.then(decode.int, fn(i) { decode.success(int.to_float(i)) }),
|
||||
]),
|
||||
)
|
||||
case parse(value) {
|
||||
Ok(ship_component_integrity) -> decode.success(ship_component_integrity)
|
||||
Error(Nil) ->
|
||||
|
|
Loading…
Reference in a new issue