forked from CorentinTh/it-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix CorentinTh#655. Taken from CorentinTh#732
- Loading branch information
Showing
5 changed files
with
641 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Braces } from '@vicons/tabler'; | ||
import { defineTool } from '../tool'; | ||
|
||
export const tool = defineTool({ | ||
name: 'JSON to Go', | ||
path: '/json-to-go', | ||
description: 'Convert JSON to Go struct', | ||
keywords: ['json', 'parse', 'go', 'convert', 'transform'], | ||
component: () => import('./json-to-go.vue'), | ||
icon: Braces, | ||
createdAt: new Date('2024-04-02'), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { jsonToGo } from './json-to-go.service'; | ||
|
||
const testCases = [ | ||
{ | ||
input: '{"SourceCode": "exampleDataHere"}', | ||
expected: | ||
'type AutoGenerated struct {\n\tSourceCode string `json:"SourceCode"`\n}', | ||
expectedWithExample: | ||
'type AutoGenerated struct {\n\tSourceCode string `json:"SourceCode" example:"exampleDataHere"`\n}', | ||
}, | ||
{ | ||
input: '{"source_code": "exampleDataHere"}', | ||
expected: | ||
'type AutoGenerated struct {\n\tSourceCode string `json:"source_code"`\n}', | ||
expectedWithExample: | ||
'type AutoGenerated struct {\n\tSourceCode string `json:"source_code" example:"exampleDataHere"`\n}', | ||
}, | ||
{ | ||
input: '{"sourceCode": "exampleDataHere"}', | ||
expected: | ||
'type AutoGenerated struct {\n\tSourceCode string `json:"sourceCode"`\n}', | ||
expectedWithExample: | ||
'type AutoGenerated struct {\n\tSourceCode string `json:"sourceCode" example:"exampleDataHere"`\n}', | ||
}, | ||
{ | ||
input: '{"SOURCE_CODE": ""}', | ||
expected: | ||
'type AutoGenerated struct {\n\tSourceCode string `json:"SOURCE_CODE"`\n}', | ||
expectedWithExample: | ||
'type AutoGenerated struct {\n\tSourceCode string `json:"SOURCE_CODE"`\n}', | ||
}, | ||
{ | ||
input: '{"PublicIP": ""}', | ||
expected: | ||
'type AutoGenerated struct {\n\tPublicIP string `json:"PublicIP"`\n}', | ||
expectedWithExample: | ||
'type AutoGenerated struct {\n\tPublicIP string `json:"PublicIP"`\n}', | ||
}, | ||
{ | ||
input: '{"public_ip": ""}', | ||
expected: | ||
'type AutoGenerated struct {\n\tPublicIP string `json:"public_ip"`\n}', | ||
expectedWithExample: | ||
'type AutoGenerated struct {\n\tPublicIP string `json:"public_ip"`\n}', | ||
}, | ||
{ | ||
input: '{"publicIP": ""}', | ||
expected: | ||
'type AutoGenerated struct {\n\tPublicIP string `json:"publicIP"`\n}', | ||
expectedWithExample: | ||
'type AutoGenerated struct {\n\tPublicIP string `json:"publicIP"`\n}', | ||
}, | ||
{ | ||
input: '{"PUBLIC_IP": ""}', | ||
expected: | ||
'type AutoGenerated struct {\n\tPublicIP string `json:"PUBLIC_IP"`\n}', | ||
expectedWithExample: | ||
'type AutoGenerated struct {\n\tPublicIP string `json:"PUBLIC_IP"`\n}', | ||
}, | ||
{ | ||
input: | ||
'{"+1": "Fails", "-1": "This should not cause duplicate field name"}', | ||
expected: | ||
'type AutoGenerated struct {\n\tNum1 string `json:"+1"`\n\tNum10 string `json:"-1"`\n}', | ||
expectedWithExample: | ||
'type AutoGenerated struct {\n\tNum1 string `json:"+1" example:"Fails"`\n\tNum10 string `json:"-1" example:"This should not cause duplicate field name"`\n}', | ||
}, | ||
{ | ||
input: '{"age": 46}', | ||
expected: 'type AutoGenerated struct {\n\tAge int `json:"age"`\n}', | ||
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: | ||
'type AutoGenerated struct {\n\tTopLevel struct {\n\t\tSecondLevel string `json:"secondLevel"`\n\t} `json:"topLevel"`\n}', | ||
expectedWithExample: | ||
'type AutoGenerated struct {\n\tTopLevel struct {\n\t\tSecondLevel string `json:"secondLevel" example:"exampleDataHere"`\n\t} `json:"topLevel"`\n}', | ||
}, | ||
{ | ||
input: | ||
'{"people": [{ "name": "Frank"}, {"name": "Dennis"}, {"name": "Dee"}, {"name": "Charley"}, {"name":"Mac"}] }', | ||
expected: | ||
'type AutoGenerated struct {\n\tPeople []struct {\n\t\tName string `json:"name"`\n\t} `json:"people"`\n}', | ||
expectedWithExample: | ||
'type AutoGenerated struct {\n\tPeople []struct {\n\t\tName string `json:"name" example:"Frank"`\n\t} `json:"people"`\n}', | ||
}, | ||
]; | ||
|
||
describe('json-to-go', () => { | ||
describe('jsonToGo', () => { | ||
for (const includeExampleData of [true, false]) { | ||
it(`must return correct results (includeExampleData = ${includeExampleData})`, () => { | ||
for (const testCase of testCases) { | ||
const got = jsonToGo(testCase.input, '', false, includeExampleData); | ||
const expected = includeExampleData | ||
? testCase.expectedWithExample | ||
: testCase.expected; | ||
|
||
expect(got.go).to.equal(expected); | ||
} | ||
}); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.