From 09094af5f839a7f0dc05e0ef6fe44b8fd664d84e Mon Sep 17 00:00:00 2001 From: apxamccallum Date: Mon, 24 Oct 2022 11:31:13 -0700 Subject: [PATCH 1/3] updated regex hack to allow negative numbers added a number of float tests --- json-to-go.js | 2 +- json-to-go.test.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/json-to-go.js b/json-to-go.js index 458a703..5dc7f43 100644 --- a/json-to-go.js +++ b/json-to-go.js @@ -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*-?\d*)\.0/g, ":$1.1")); // hack that forces floats to stay as floats scope = data; } catch (e) diff --git a/json-to-go.test.js b/json-to-go.test.js index 105e19b..79758ee 100644 --- a/json-to-go.test.js +++ b/json-to-go.test.js @@ -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.00"`\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.00"`\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.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}' + }, + { + 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: '{"topLevel": { "secondLevel": "exampleDataHere"} }', expected: From 243c06f51128252a00aa91269ce0282e85841a4c Mon Sep 17 00:00:00 2001 From: apxamccallum Date: Mon, 24 Oct 2022 12:02:20 -0700 Subject: [PATCH 2/3] added check for begining of array to regex hack corrected tests for floats (arrays don't get an example) --- json-to-go.js | 2 +- json-to-go.test.js | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/json-to-go.js b/json-to-go.js index 5dc7f43..fe2c6fe 100644 --- a/json-to-go.js +++ b/json-to-go.js @@ -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*-?\d*)\.0/g, "$1$2.1")); // hack that forces floats to stay as floats scope = data; } catch (e) diff --git a/json-to-go.test.js b/json-to-go.test.js index 79758ee..4005e43 100644 --- a/json-to-go.test.js +++ b/json-to-go.test.js @@ -83,42 +83,42 @@ function test(includeExampleData) { expected: 'type AutoGenerated struct {\n\tNegativeFloat float64 `json:"negativeFloat"`\n}', expectedWithExample: - 'type AutoGenerated struct {\n\tNegativeFloat float64 `json:"negativeFloat" example:"-1.00"`\n}' + '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.00"`\n}' + '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.00"`\n}' + '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" example:"[-1.00, -2.00, -3.00]"`\n}' + '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" example:"[0.00, 0.00, 0.00]"`\n}' + '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" example:"[1.00, 2.00, 3.00]"`\n}' + 'type AutoGenerated struct {\n\tPositiveFloats []float64 `json:"positiveFloats"`\n}' }, { input: '{"topLevel": { "secondLevel": "exampleDataHere"} }', @@ -154,4 +154,4 @@ function test(includeExampleData) { } test(false); -test(true) +test(true) \ No newline at end of file From 3c2468dc0eb5084de6d189fa21a25b8ea4dc5446 Mon Sep 17 00:00:00 2001 From: apxamccallum Date: Mon, 24 Oct 2022 14:13:01 -0700 Subject: [PATCH 3/3] updated to specificly look for array as value --- json-to-go.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json-to-go.js b/json-to-go.js index fe2c6fe..8fffdce 100644 --- a/json-to-go.js +++ b/json-to-go.js @@ -22,7 +22,7 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty try { - data = JSON.parse(json.replace(/([\[:])(\s*-?\d*)\.0/g, "$1$2.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)