Skip to content

Commit

Permalink
feat(new tool): Json to Go
Browse files Browse the repository at this point in the history
  • Loading branch information
sharevb committed May 15, 2024
1 parent cb5b462 commit abe7ec4
Show file tree
Hide file tree
Showing 5 changed files with 641 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { tool as asciiTextDrawer } from './ascii-text-drawer';

import { tool as textToUnicode } from './text-to-unicode';
import { tool as safelinkDecoder } from './safelink-decoder';
import { tool as jsonToGo } from './json-to-go';
import { tool as pdfSignatureChecker } from './pdf-signature-checker';
import { tool as numeronymGenerator } from './numeronym-generator';
import { tool as macAddressGenerator } from './mac-address-generator';
Expand Down Expand Up @@ -107,6 +108,7 @@ export const toolsByCategory: ToolCategory[] = [
listConverter,
tomlToJson,
tomlToYaml,
jsonToGo,
],
},
{
Expand Down
12 changes: 12 additions & 0 deletions src/tools/json-to-go/index.ts
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'),
});
149 changes: 149 additions & 0 deletions src/tools/json-to-go/json-to-go.service.test.ts
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);
}
});
}
});
});
Loading

0 comments on commit abe7ec4

Please sign in to comment.