pelletier / go-toml

Go library for the TOML file format
https://github.com/pelletier/go-toml
Other
1.69k stars 206 forks source link

Cannot decode integers into floats #835

Closed horgh closed 1 year ago

horgh commented 1 year ago

Describe the bug I have a config with entries like

foo = 5

And I would like to decode them into a float64. This errors with: toml: cannot decode TOML integer into struct field main.conf.Foo of type float64

To Reproduce

package main

import (
        "log"

        toml "github.com/pelletier/go-toml/v2"
)

func main() {
        type conf struct {
                Foo float64 `json:"foo"`
        }
        var c conf

        buf := []byte(`foo = 5`)

        if err := toml.Unmarshal(buf, &c); err != nil {
                log.Fatal(err)
        }
        log.Printf("%+v", c)
}

Expected behavior I think it would be good to be able to deserialize ints into floats. encoding/json works that way for instance.

Versions

Additional context I could write the config values as 5.0 to get around this. However the same config is also used by a Perl parser, which has a different issue when expressing numbers as 5.0. It would be helpful in my case to be able to leave it as "5".

Thank you!

pelletier commented 1 year ago

This should be fixed as of https://github.com/pelletier/go-toml/commit/9f5726004ef124b8d3a4844c90c8700ba20d154e. Feel free to reopen if it doesn't solve your problem!

horgh commented 1 year ago

Thank you!