Skip to content
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

Suggestion: Add decode.number #801

Open
sbergen opened this issue Feb 6, 2025 · 2 comments
Open

Suggestion: Add decode.number #801

sbergen opened this issue Feb 6, 2025 · 2 comments

Comments

@sbergen
Copy link

sbergen commented Feb 6, 2025

Context

Given that

  1. the decode API is used with formats like JSON, where there is no distinction between floats and ints, and
  2. the target (Erlang vs ES) is a somewhat leaky abstraction when it comes to handling numbers,

it can be confusing that on the Erlang target decode.float will not accept e.g. 1, but requires 1.0 to work properly.

Suggestion

Add decode.number: Decoder(Float), which would work equivalently to

decode.one_of(decode.float, or: [decode.int |> decode.map(int.to_float)])

This would both make it more convenient to decode formats where there's no distinction between ints and floats, and act as documentation about the context outlined above.

If this sounds like a good idea, I'd be happy to create a PR for it.

@lpil
Copy link
Member

lpil commented Feb 8, 2025

Could be good! The plan is to let the stdlib and the new decoder API settle for a while so we can see how people find it before making any changes.

@ethanthoma
Copy link
Contributor

I also run into this issue. Sometimes JSON endpoints add a '.0' and sometimes they don't so I have to use the one_of func for it often

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants