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

add support for all basic golang number types #137

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions json-to-go.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,17 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
// Determines the most appropriate Go type
function goType(val)
{
Number.prototype.numberOfDecimals = function () {
if(isNaN(this)) return NaN
if(!isFinite(this)) return Infinity
// example: 1 || 1e-1 || 4.361170766e+20
// try to split by "e" and "."
let exponentialString = this.toExponential().toString()
exponentialString = exponentialString.split("e")[0] || exponentialString
exponentialString = exponentialString.split(".")[1] || exponentialString
return exponentialString.length || 0;
}

if (val === null)
return "any";

Expand All @@ -360,15 +371,22 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
else
return "string";
case "number":
if (val % 1 === 0)
{
if (val > -2147483648 && val < 2147483647)
return "int";
else
return "int64";
}
else
// avoid type "int", as its size is platform dependent
if (val % 1 === 0 && val >= -128 && val <= 127)
return "int8"
if (val % 1 === 0 && val >= -32768 && val <= 32767)
return "int16"
if (val % 1 === 0 && val >= -2147483648 && val <= 2147483647)
return "int32"
if (val % 1 === 0 && val >= -9223372036854775808 && val <= 9223372036854775807)
return "int64"
if (val.numberOfDecimals() <= 7 && val > -3.4e+38 && val < 3.4e+38)
return "float32"
if (val.numberOfDecimals() <= 15 && val > -1.7e+308 && val < +1.7e+308)
return "float64";

console.error(`Warning: can't find matching Golang number type for '${val}'. Falling back to "any".`)
return "any";
case "boolean":
return "bool";
case "object":
Expand Down
29 changes: 15 additions & 14 deletions json-to-go.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,51 +76,51 @@ function test(includeExampleData) {
{
input: '{"age": 46}',
expected:
'type AutoGenerated struct {\n\tAge int `json:"age"`\n}\n',
'type AutoGenerated struct {\n\tAge int8 `json:"age"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tAge int `json:"age" example:"46"`\n}\n',
'type AutoGenerated struct {\n\tAge int8 `json:"age" example:"46"`\n}\n',
},
{
input: '{"negativeFloat": -1.00}',
expected:
'type AutoGenerated struct {\n\tNegativeFloat float64 `json:"negativeFloat"`\n}\n',
'type AutoGenerated struct {\n\tNegativeFloat float32 `json:"negativeFloat"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tNegativeFloat float64 `json:"negativeFloat" example:"-1.1"`\n}\n',
'type AutoGenerated struct {\n\tNegativeFloat float32 `json:"negativeFloat" example:"-1.1"`\n}\n',
},
{
input: '{"zeroFloat": 0.00}',
expected:
'type AutoGenerated struct {\n\tZeroFloat float64 `json:"zeroFloat"`\n}\n',
'type AutoGenerated struct {\n\tZeroFloat float32 `json:"zeroFloat"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tZeroFloat float64 `json:"zeroFloat" example:"0.1"`\n}\n',
'type AutoGenerated struct {\n\tZeroFloat float32 `json:"zeroFloat" example:"0.1"`\n}\n',
},
{
input: '{"positiveFloat": 1.00}',
expected:
'type AutoGenerated struct {\n\tPositiveFloat float64 `json:"positiveFloat"`\n}\n',
'type AutoGenerated struct {\n\tPositiveFloat float32 `json:"positiveFloat"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tPositiveFloat float64 `json:"positiveFloat" example:"1.1"`\n}\n',
'type AutoGenerated struct {\n\tPositiveFloat float32 `json:"positiveFloat" example:"1.1"`\n}\n',
},
{
input: '{"negativeFloats": [-1.00, -2.00, -3.00]}',
expected:
'type AutoGenerated struct {\n\tNegativeFloats []float64 `json:"negativeFloats"`\n}\n',
'type AutoGenerated struct {\n\tNegativeFloats []float32 `json:"negativeFloats"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tNegativeFloats []float64 `json:"negativeFloats"`\n}\n',
'type AutoGenerated struct {\n\tNegativeFloats []float32 `json:"negativeFloats"`\n}\n',
},
{
input: '{"zeroFloats": [0.00, 0.00, 0.00]}',
expected:
'type AutoGenerated struct {\n\tZeroFloats []float64 `json:"zeroFloats"`\n}\n',
'type AutoGenerated struct {\n\tZeroFloats []float32 `json:"zeroFloats"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tZeroFloats []float64 `json:"zeroFloats"`\n}\n',
'type AutoGenerated struct {\n\tZeroFloats []float32 `json:"zeroFloats"`\n}\n',
},
{
input: '{"positiveFloats": [1.00, 2.00, 3.00]}',
expected:
'type AutoGenerated struct {\n\tPositiveFloats []float64 `json:"positiveFloats"`\n}\n',
'type AutoGenerated struct {\n\tPositiveFloats []float32 `json:"positiveFloats"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tPositiveFloats []float64 `json:"positiveFloats"`\n}\n',
'type AutoGenerated struct {\n\tPositiveFloats []float32 `json:"positiveFloats"`\n}\n',
},
{
input: '{"topLevel": { "secondLevel": "exampleDataHere"} }',
Expand Down Expand Up @@ -162,6 +162,7 @@ function testFiles() {
const testCases = [
"duplicate-top-level-structs",
"double-nested-objects",
"supported-number-types",
];

for (const testCase of testCases) {
Expand Down
4 changes: 2 additions & 2 deletions tests/double-nested-objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ type Type struct {
Long string `json:"long"`
}
type First struct {
ID int `json:"id"`
ID int8 `json:"id"`
Type Type `json:"type"`
}
type SecondType struct {
Long string `json:"long"`
}
type Second struct {
ID int `json:"id"`
ID int8 `json:"id"`
SecondType SecondType `json:"type"`
}
6 changes: 3 additions & 3 deletions tests/duplicate-top-level-structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type AutoGenerated struct {
}
type Identifier struct {
Type string `json:"type"`
ID int `json:"id"`
ID int16 `json:"id"`
}
type Region struct {
Identifier Identifier `json:"identifier"`
Expand All @@ -20,11 +20,11 @@ type Municipality struct {
}
type Postal struct {
Type string `json:"type"`
ID int `json:"id"`
ID int8 `json:"id"`
}
type Road struct {
Name string `json:"name"`
ID int `json:"id"`
ID int8 `json:"id"`
}
type BuildingIdentifier struct {
Postal Postal `json:"postal"`
Expand Down
17 changes: 17 additions & 0 deletions tests/supported-number-types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type AutoGenerated struct {
Basedonscience bool `json:"basedonscience"`
SecondsPerYear float32 `json:"seconds per year"`
AgeOfTheUniverse AgeOfTheUniverse `json:"age of the universe"`
BasicallyInfinity any `json:"basically infinity"`
Date string `json:"date"`
}
type AgeOfTheUniverse struct {
InPlanckTime float64 `json:"in planck time"`
InVeryPreciseMilliseconds float64 `json:"in very precise milliseconds"`
InMilliseconds float32 `json:"in milliseconds"`
InSeconds int64 `json:"in seconds"`
InYears int64 `json:"in years"`
InMillenia int32 `json:"in millenia"`
InMegaannums int16 `json:"in megaannums"`
InGalacticYears int8 `json:"in galactic years"`
}
16 changes: 16 additions & 0 deletions tests/supported-number-types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"basedonscience": true,
"seconds per year": 3.6524219e+2,
"age of the universe": {
"in planck time": 8.0718985e+60,
"in very precise milliseconds": 4.35075327952992e+20,
"in milliseconds": 4.3507532e+20,
"in seconds": 4.35075327952992e+17,
"in years": 13787000000,
"in millenia": 13787000,
"in megaannums": 13787,
"in galactic years": 60
},
"basically infinity": 4.361170766e+400,
"date": "2024-07-25"
}