Closed jxsl13 closed 1 year ago
json-to-go is interpreting your first data array as 0 instead of 0.00 and is using that for all your other data arrays. However, the way json-to-go interprets significant figures and negatives is inconsistent.
JSON:
{
"neg": -1.00,
"pos": 1.00,
"zero": 0.00,
"tinyneg": -0.01,
"arrayofzero": [
0.00
],
"arrayofneg": [
-1.00
],
"arrayofpos": [
1.00
],
"tinyarray": [
0.01
],
"tinynegarray": [
-0.01
]
}
Struct:
type AutoGenerated struct {
Neg int `json:"neg"`
Pos float64 `json:"pos"`
Zero float64 `json:"zero"`
Tinyneg float64 `json:"tinyneg"`
Arrayofzero []int `json:"arrayofzero"`
Arrayofneg []int `json:"arrayofneg"`
Arrayofpos []int `json:"arrayofpos"`
Tinyarray []float64 `json:"tinyarray"`
Tinynegarray []float64 `json:"tinynegarray"`
}
Oops, wow, that's fun. Someone with a few minutes want to pitch in a fix?
I haven't worked with js in 15 years, but I'm giving it a go. I've tried to build some test cases, test(false) is working as I would expect, but i'm not sure what test(true) is trying to do. I'm getting some results that I'm not sure what to do with:
{
input: '{"positiveFloat": 1.00}',
expected:
'type AutoGenerated struct {\n\tPositiveFloat float64 `json:"positiveFloat"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tPositiveFloat float64 `json:"positiveFloat" example:"1.00"`\n}'
},
{
input: '{"positiveFloats": [1.00, 2.00, 3.00]}',
expected:
'type AutoGenerated struct {\n\tPositiveFloats []float64 `json:"positiveFloats"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tPositiveFloats []float64 `json:"positiveFloats" example:"[1.00, 2.00, 3.00]"`\n}'
},
{
input: '{"negativeFloat": -1.00}',
expected:
'type AutoGenerated struct {\n\tNegativeFloat float64 `json:"negativeFloat"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tNegativeFloat float64 `json:"negativeFloat" example:"-1.00"`\n}'
},
{
input: '{"negativeFloats": [-1.00, -2.00, -3.00]}',
expected:
'type AutoGenerated struct {\n\tNegativeFloats []float64 `json:"negativeFloats"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tNegativeFloats []float64 `json:"negativeFloats" example:"[-1.00, -2.00, -3.00]"`\n}'
},
{
input: '{"zeroFloats": [0.00, 0.00, 0.00]}',
expected:
'type AutoGenerated struct {\n\tZeroFloats []float64 `json:"zeroFloats"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tZeroFloats []float64 `json:"zeroFloats" example:"[0.00, 0.00, 0.00]"`\n}'
}
Assertion failed: format('{"positiveFloats": [1.00, 2.00, 3.00]}'):
got: 'type AutoGenerated struct {\n\tPositiveFloats []int `json:"positiveFloats"`\n}'
want: 'type AutoGenerated struct {\n\tPositiveFloats []float64 `json:"positiveFloats"`\n}'
Assertion failed: format('{"negativeFloat": -1.00}'):
got: 'type AutoGenerated struct {\n\tNegativeFloat int `json:"negativeFloat"`\n}'
want: 'type AutoGenerated struct {\n\tNegativeFloat float64 `json:"negativeFloat"`\n}'
Assertion failed: format('{"negativeFloats": [-1.00, -2.00, -3.00]}'):
got: 'type AutoGenerated struct {\n\tNegativeFloats []int `json:"negativeFloats"`\n}'
want: 'type AutoGenerated struct {\n\tNegativeFloats []float64 `json:"negativeFloats"`\n}'
Assertion failed: format('{"zeroFloats": [0.00, 0.00, 0.00]}'):
want: 'type AutoGenerated struct {\n\tZeroFloats []float64 `json:"zeroFloats"`\n}'
done
Assertion failed: format('{"positiveFloat": 1.00}'):
got: 'type AutoGenerated struct {\n\tPositiveFloat float64 `json:"positiveFloat" example:"1.1"`\n}'
want: 'type AutoGenerated struct {\n\tPositiveFloat float64 `json:"positiveFloat" example:"1.00"`\n}'
Assertion failed: format('{"positiveFloats": [1.00, 2.00, 3.00]}'):
got: 'type AutoGenerated struct {\n\tPositiveFloats []int `json:"positiveFloats"`\n}'
want: 'type AutoGenerated struct {\n\tPositiveFloats []float64 `json:"positiveFloats" example:"[1.00, 2.00, 3.00]"`\n}'
Assertion failed: format('{"negativeFloat": -1.00}'):
got: 'type AutoGenerated struct {\n\tNegativeFloat int `json:"negativeFloat" example:"-1"`\n}'
want: 'type AutoGenerated struct {\n\tNegativeFloat float64 `json:"negativeFloat" example:"-1.00"`\n}'
Assertion failed: format('{"negativeFloats": [-1.00, -2.00, -3.00]}'):
got: 'type AutoGenerated struct {\n\tNegativeFloats []int `json:"negativeFloats"`\n}'
want: 'type AutoGenerated struct {\n\tNegativeFloats []float64 `json:"negativeFloats" example:"[-1.00, -2.00, -3.00]"`\n}'
Assertion failed: format('{"zeroFloats": [0.00, 0.00, 0.00]}'):
got: 'type AutoGenerated struct {\n\tZeroFloats []int `json:"zeroFloats"`\n}'
want: 'type AutoGenerated struct {\n\tZeroFloats []float64 `json:"zeroFloats" example:"[0.00, 0.00, 0.00]"`\n}'
Are you able to provide any guidance here?
@apxamccallum I think test(true)
is testing the case with an example
portion added to the struct tag.
First, try to get the tests passing with test(false)
and then see about test(true)
-- that's probably a different problem.
The hack is also what is causing the test(true) to fail. Adding 0.1 to the value makes it no longer match the test case.
I don't have much time to think about it right now, but I'd also be ok with a way that doesn't use the regexp hack. Maybe even if it's converting the value to a string so we don't have to deal with float messiness.
After the first PR I thought of a way to deal with arrays.
Agreed a way to not use regex would probably be better, that looks like a bit bigger of a refactor then I can handle right now. I'm focusing on learning Go, so this was more of an interesting side quest.
I've put some thought to this. There doesn't seem to be a way to have JSON.parse() retain significant figures. The only way I can see to achieve this is to modify the JSON before it is parsed., which is what is currently happening with the hack (fixed or not).
The hack is currently checking if it is a value (looks for a ":") checks if the value is a number followed by a decimal and a zero, then replaces that zero with a 1.
My patch allows for values or arrays of values, then does the same replacement.
Wrapping the number in quotes, would require new code to validate if a string is also a number. This would have the side effect of potentially converting some values that were intentionally strings into numbers, which may be undesirable, and would still require the regex hack.
I think the fix addresses the current issue, without creating unintended side effects. Perhaps the better solution is to add a toggle that allows the user to decide if they want to maintain the significant figures as floats, (use the hack) or allow them to be dropped and converted to an Int.
@mholt Thank for pushing the fix to the live page, I just ran the original issue through the page and it now provides the correct structs. Is it possible to close this issue now?
Really appreciate having this tool.
type AutoGenerated struct {
Values []Values `json:"values"`
}
type StatKey struct {
Key string `json:"key"`
}
type IntervalUnit struct {
Quantifier int `json:"quantifier"`
IntervalType string `json:"intervalType"`
}
type Stat struct {
Timestamps []int64 `json:"timestamps"`
StatKey StatKey `json:"statKey"`
RollUpType string `json:"rollUpType"`
IntervalUnit IntervalUnit `json:"intervalUnit"`
Data []float64 `json:"data"`
}
type StatList struct {
Stat []Stat `json:"stat"`
}
type Values struct {
ResourceID string `json:"resourceId"`
StatList StatList `json:"stat-list"`
}
Yeah! Sorry it took me so long 😅
json
structs