Handle float values without decimal point

This commit is contained in:
LilyRose2798 2025-06-25 12:03:04 +10:00
parent aed9b6413f
commit de20cb5001
2 changed files with 12 additions and 2 deletions

View file

@ -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) ->

View file

@ -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) ->