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

Float fix #120

Merged
merged 3 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion json-to-go.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty

try
{
data = JSON.parse(json.replace(/:(\s*\d*)\.0/g, ":$1.1")); // hack that forces floats to stay as floats
data = JSON.parse(json.replace(/(:\s*\[?\s*-?\d*)\.0/g, "$1.1")); // hack that forces floats to stay as floats
scope = data;
}
catch (e)
Expand Down
44 changes: 43 additions & 1 deletion json-to-go.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,48 @@ function test(includeExampleData) {
expectedWithExample:
'type AutoGenerated struct {\n\tAge int `json:"age" example:"46"`\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.1"`\n}'
},
{
input: '{"zeroFloat": 0.00}',
expected:
'type AutoGenerated struct {\n\tZeroFloat float64 `json:"zeroFloat"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tZeroFloat float64 `json:"zeroFloat" example:"0.1"`\n}'
},
{
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.1"`\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"`\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"`\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"`\n}'
},
{
input: '{"topLevel": { "secondLevel": "exampleDataHere"} }',
expected:
Expand Down Expand Up @@ -112,4 +154,4 @@ function test(includeExampleData) {
}

test(false);
test(true)
test(true)