Skip to content

Commit

Permalink
Float fix (#120)
Browse files Browse the repository at this point in the history
* updated regex hack to allow negative numbers
added a number of float tests

* added check for begining of array to regex hack
corrected tests for floats (arrays don't get an example)

* updated to specificly look for array as value
  • Loading branch information
apxamccallum committed Oct 27, 2022
1 parent 0a10c6f commit e9233d6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
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)

0 comments on commit e9233d6

Please sign in to comment.