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

strconv.ParseFloat value out of range (big.Int) #192

Open
razorbohan opened this issue Oct 3, 2024 · 2 comments
Open

strconv.ParseFloat value out of range (big.Int) #192

razorbohan opened this issue Oct 3, 2024 · 2 comments
Assignees

Comments

@razorbohan
Copy link

System (please complete the following information):

  • OS: macOS 14.6.1
  • GO Version: 1.21.6
  • Pkg Version: 2.2.5

Describe the bug

The library emits error trying to parse big.Int value:

strconv.ParseFloat: parsing "23707729876828933003792990320594511132013137629744363463325945636682800546201191581706241551352734654762086038344743940857801503840360878427584255703013924373301145683882034301334533678253123777083489887967659929148298684008991665609773532863485728577470710590688325197694460521376123072613857785739366064688074459399408762960887169067851291291611970194076234580897060365318108861340336375060983779163595033605894055218557648363640361256922411962394084268360413547861005069585285713253756167043430574673046032573767256949834558011358364591391964981157578571244295339467926678988648036459748021538498339258036608168313": value out of range

My guess is that it is because the lib is trying to parse it to that type of object:

data := make(map[string]any) // parseSourceToMap function (load.go, line 505)

To Reproduce

Have a json file (configPath) with a big.Int value in any field

config.AddDriver(json.Driver)
config.WithOptions(config.ParseDefault)
if err := config.LoadFiles(configPath); err != nil { // <- returns error
     return err
}

Expected behavior

A big.Int variable successfully parsed

@inhere inhere added the bug Something isn't working label Oct 5, 2024
@inhere
Copy link
Member

inhere commented Nov 9, 2024

hi @razorbohan The error is from the encoding/json package.

You can custom driver for parse the json. such as:

	s := `{
			"key": 23707729876828933003792990320594511132013137629744363463325945636682800546201191581706241551352734654762086038344743940857801503840360878427584255703013924373301145683882034301334533678253123777083489887967659929148298684008991665609773532863485728577470710590688325197694460521376123072613857785739366064688074459399408762960887169067851291291611970194076234580897060365318108861340336375060983779163595033605894055218557648363640361256922411962394084268360413547861005069585285713253756167043430574673046032573767256949834558011358364591391964981157578571244295339467926678988648036459748021538498339258036608168313
		}`

	jsd := config.NewDriver("json", func(blob []byte, v any) (err error) {
		jnd := json.NewDecoder(bytes.NewReader(blob))
		jnd.UseNumber()
		return jnd.Decode(v)
	}, config.JSONEncoder)
	cfg1 := config.NewEmpty("test1").WithDriver(jsd)
	err = cfg1.LoadStrings(config.JSON, s)
	assert.NoErr(t, err)
	dump.P(cfg1.Data())

	// to big.Int
	bi := new(big.Int)
	_, ok := bi.SetString(cfg1.String("key"), 10)
	assert.True(t, ok)

Output:

=== RUN   TestIssues_192
PRINT AT github.com/gookit/config/v2_test.TestIssues_192(issues_test.go:632)
map[string]interface {} { #len=1
  "key": json.Number("23707729876828933003792990320594511132013137629744363463325945636682800546201191581706241551352734654762086038344743940857801503840360878427584255703013924373301145683882034301334533678253123777083489887967659929148298684008991665609773532863485728577470710590688325197694460521376123072613857785739366064688074459399408762960887169067851291291611970194076234580897060365318108861340336375060983779163595033605894055218557648363640361256922411962394084268360413547861005069585285713253756167043430574673046032573767256949834558011358364591391964981157578571244295339467926678988648036459748021538498339258036608168313"), #len=617
},
--- PASS: TestIssues_192 (0.00s)

@inhere
Copy link
Member

inhere commented Nov 9, 2024

@razorbohan Or do you have other solutions?

@inhere inhere removed the bug Something isn't working label Nov 9, 2024
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

2 participants