Skip to content

Commit e9233d6

Browse files
authored
Float fix (#120)
* 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
1 parent 0a10c6f commit e9233d6

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

json-to-go.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
2222

2323
try
2424
{
25-
data = JSON.parse(json.replace(/:(\s*\d*)\.0/g, ":$1.1")); // hack that forces floats to stay as floats
25+
data = JSON.parse(json.replace(/(:\s*\[?\s*-?\d*)\.0/g, "$1.1")); // hack that forces floats to stay as floats
2626
scope = data;
2727
}
2828
catch (e)

json-to-go.test.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,48 @@ function test(includeExampleData) {
7878
expectedWithExample:
7979
'type AutoGenerated struct {\n\tAge int `json:"age" example:"46"`\n}'
8080
},
81+
{
82+
input: '{"negativeFloat": -1.00}',
83+
expected:
84+
'type AutoGenerated struct {\n\tNegativeFloat float64 `json:"negativeFloat"`\n}',
85+
expectedWithExample:
86+
'type AutoGenerated struct {\n\tNegativeFloat float64 `json:"negativeFloat" example:"-1.1"`\n}'
87+
},
88+
{
89+
input: '{"zeroFloat": 0.00}',
90+
expected:
91+
'type AutoGenerated struct {\n\tZeroFloat float64 `json:"zeroFloat"`\n}',
92+
expectedWithExample:
93+
'type AutoGenerated struct {\n\tZeroFloat float64 `json:"zeroFloat" example:"0.1"`\n}'
94+
},
95+
{
96+
input: '{"positiveFloat": 1.00}',
97+
expected:
98+
'type AutoGenerated struct {\n\tPositiveFloat float64 `json:"positiveFloat"`\n}',
99+
expectedWithExample:
100+
'type AutoGenerated struct {\n\tPositiveFloat float64 `json:"positiveFloat" example:"1.1"`\n}'
101+
},
102+
{
103+
input: '{"negativeFloats": [-1.00, -2.00, -3.00]}',
104+
expected:
105+
'type AutoGenerated struct {\n\tNegativeFloats []float64 `json:"negativeFloats"`\n}',
106+
expectedWithExample:
107+
'type AutoGenerated struct {\n\tNegativeFloats []float64 `json:"negativeFloats"`\n}'
108+
},
109+
{
110+
input: '{"zeroFloats": [0.00, 0.00, 0.00]}',
111+
expected:
112+
'type AutoGenerated struct {\n\tZeroFloats []float64 `json:"zeroFloats"`\n}',
113+
expectedWithExample:
114+
'type AutoGenerated struct {\n\tZeroFloats []float64 `json:"zeroFloats"`\n}'
115+
},
116+
{
117+
input: '{"positiveFloats": [1.00, 2.00, 3.00]}',
118+
expected:
119+
'type AutoGenerated struct {\n\tPositiveFloats []float64 `json:"positiveFloats"`\n}',
120+
expectedWithExample:
121+
'type AutoGenerated struct {\n\tPositiveFloats []float64 `json:"positiveFloats"`\n}'
122+
},
81123
{
82124
input: '{"topLevel": { "secondLevel": "exampleDataHere"} }',
83125
expected:
@@ -112,4 +154,4 @@ function test(includeExampleData) {
112154
}
113155

114156
test(false);
115-
test(true)
157+
test(true)

0 commit comments

Comments
 (0)