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

int64 handling #164

Open
ronanyeah opened this issue Nov 10, 2024 · 6 comments
Open

int64 handling #164

ronanyeah opened this issue Nov 10, 2024 · 6 comments
Labels
enhancement New feature or request

Comments

@ronanyeah
Copy link
Contributor

int64 is being handled as an Elm Int, which is unsafe. It should be String or some BigInt type.

@wolfadex
Copy link
Owner

@ronanyeah do you have an example schema (or portion of one) with an int64?

@ronanyeah
Copy link
Contributor Author

@wolfadex wolfadex added the enhancement New feature or request label Nov 10, 2024
@wolfadex
Copy link
Owner

Thank you! This puts further emphasis on needing to figure out how to handle formats. There's been some discussion in Discord (most recent as of now) about having an elm-review like config of sorts. Essentially, we need a way for users of the CLI tool to be able to add semi-arbitrary encoding, decoding, and types.

I don't really know what this config looks like yet but more discussion in the Discord is very welcome.

@miniBill
Copy link
Collaborator

One issue specific to int64 is that Elm uses JSON.parse under the hood, so by the time we get a value it has already lost precision. The way to fix this would be to implement the JSON parsing in Elm directly which can be done but it's going to be slower for everyone else.

I'm personally up for considering it as a config option in the new config-file approach, but I'm not going to write a JSON parser in Elm from scratch 😅

Compare what is needed in JS to use BigInts for big numbers:

const bigJSON = '{"gross_gdp": 12345678901234567890}';
const bigObj = JSON.parse(bigJSON, (key, value, context) => {
  if (key === "gross_gdp") {
    // Ignore the value because it has already lost precision
    return BigInt(context.source);
  }
  return value;
});

@miniBill
Copy link
Collaborator

If you control the server the simplest path is to encode the number as a string and then parse it on the client after the JSON decoding

@miniBill
Copy link
Collaborator

This would be an int64 format. Notice the problematic encoder and decoder.

int64Format : CliMonad.Format
int64Format =
    { basicType = Common.Integer
    , format = "int64"
    , annotation = Gen.Int64.annotation_.int64
    , encode =
        \int64 ->
            int64
                |> Gen.Int64.toSignedString
                |> Gen.String.call_.toInt
                |> Gen.Maybe.withDefault (Elm.int 0)
                |> Gen.Json.Encode.call_.int
    , decoder = Gen.Json.Decode.int |> Gen.Json.Decode.map Gen.Int64.call_.fromInt
    , toParamString = Gen.Int64.toSignedString
    , sharedDeclarations = []
    , requiresPackages = [ "folkertdev/elm-int64" ]
    }

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

No branches or pull requests

3 participants