From 42240320343bf51de2e13a4bbc781327e5e9fd5b Mon Sep 17 00:00:00 2001 From: Ritesh Kumar Date: Sat, 29 Oct 2022 01:29:11 +0530 Subject: [PATCH] :bug: convert TS to JSON Schema --- constants/data.ts | 2 +- package.json | 2 +- pages/api/typescript-to-json-schema.ts | 18 +-- pages/cadence-to-go.tsx | 82 ++++++----- yarn.lock | 193 +++++++++++++++++++++---- 5 files changed, 219 insertions(+), 78 deletions(-) diff --git a/constants/data.ts b/constants/data.ts index 8e09b76..7b0955e 100644 --- a/constants/data.ts +++ b/constants/data.ts @@ -489,4 +489,4 @@ export const cadence = `pub struct StructContainsManyType { pub fun main(): StructContainsManyType { return StructContainsManyType() } -` +`; diff --git a/package.json b/package.json index a5e98d9..e51da42 100644 --- a/package.json +++ b/package.json @@ -94,9 +94,9 @@ "tempy": "^1.0.1", "text-encoding-utf-8": "^1.0.2", "transform-json-types": "^0.7.0", - "ts-json-schema-generator": "^0.93.0", "ts-to-zod": "^1.11.0", "typescript": "4.3.4", + "typescript-json-schema": "^0.54.0", "xml-js": "^1.6.11", "yaml": "^1.10.2" }, diff --git a/pages/api/typescript-to-json-schema.ts b/pages/api/typescript-to-json-schema.ts index 0872b31..a70ca54 100644 --- a/pages/api/typescript-to-json-schema.ts +++ b/pages/api/typescript-to-json-schema.ts @@ -1,29 +1,25 @@ import { NextApiRequest, NextApiResponse } from "next"; -import { Config } from "ts-json-schema-generator/dist/src/Config"; -import * as tsj from "ts-json-schema-generator"; import os from "os"; import crypto from "crypto"; import path from "path"; import fs from "fs"; +import util from "util"; +import { exec } from "child_process"; + +const _exec = util.promisify(exec); const tmpDir = os.tmpdir?.(); -export default (req: NextApiRequest, res: NextApiResponse) => { +export default async (req: NextApiRequest, res: NextApiResponse) => { const filePath = path.join(tmpDir, crypto.randomBytes(16).toString("hex")) + ".ts"; try { fs.writeFileSync(filePath, req.body, { encoding: "utf-8" }); - const config: Config = { - path: filePath, - expose: "all", - jsDoc: "extended", - type: "*" - }; - const schema = tsj.createGenerator(config).createSchema(config.type); - res.status(200).send(JSON.stringify(schema, null, 2)); + const stdout = await _exec(`typescript-json-schema ${filePath} Root`); + res.status(200).send(stdout.stdout); } catch (e) { res.status(500).send(e.message); } diff --git a/pages/cadence-to-go.tsx b/pages/cadence-to-go.tsx index e93f123..f3f6d77 100644 --- a/pages/cadence-to-go.tsx +++ b/pages/cadence-to-go.tsx @@ -6,64 +6,68 @@ import gofmt from "gofmt.js"; import { CadenceParser } from "@lemonneko/flow__cadence-parser"; const typeMap = { - UInt: '*big.Int', - UInt8: 'uint8', - UInt16: 'uint16', - UInt32: 'uint32', - UInt64: 'uint64', - UInt128: '*big.Int', - UInt256: '*big.Int', - Int: '*big.Int', - Int8: 'int8', - Int16: 'int16', - Int32: 'int32', - Int64: 'int64', - Int128: '*big.Int', - Int256: '*big.Int', - UFix64: 'uint64', - Fix64: 'int64', - String: 'string', - Character: 'string', - Bool: 'bool', - Path: 'string', - Address: '[8]uint8' -} + UInt: "*big.Int", + UInt8: "uint8", + UInt16: "uint16", + UInt32: "uint32", + UInt64: "uint64", + UInt128: "*big.Int", + UInt256: "*big.Int", + Int: "*big.Int", + Int8: "int8", + Int16: "int16", + Int32: "int32", + Int64: "int64", + Int128: "*big.Int", + Int256: "*big.Int", + UFix64: "uint64", + Fix64: "int64", + String: "string", + Character: "string", + Bool: "bool", + Path: "string", + Address: "[8]uint8" +}; function firstLetterUppercase(id: string): string { - return id.charAt(0).toUpperCase() + id.slice(1) + return id.charAt(0).toUpperCase() + id.slice(1); } async function cadenceToGo(value: string): Promise { - const parser = await CadenceParser.create("https://cdn.jsdelivr.net/npm/@lemonneko/flow__cadence-parser@0.24.1/dist/cadence-parser.wasm") + const parser = await CadenceParser.create( + "https://cdn.jsdelivr.net/npm/@lemonneko/flow__cadence-parser@0.24.1/dist/cadence-parser.wasm" + ); const ast = parser.parse(value); - const program = ast.program - const declarations = program.Declarations - let ret = '' + const program = ast.program; + const declarations = program.Declarations; + let ret = ""; for (const dec of declarations) { - let decRet = '' + let decRet = ""; // only struct can be converted if (dec.CompositeKind === "CompositeKindStructure") { - decRet += `type ${dec.Identifier.Identifier} struct {\n` + decRet += `type ${dec.Identifier.Identifier} struct {\n`; for (const member of dec.Members.Declarations) { - if (member.Type === 'FieldDeclaration') { - decRet += `${firstLetterUppercase(member.Identifier.Identifier)} ${typeMap[member.TypeAnnotation.AnnotatedType.Identifier.Identifier]} \`godence:"${member.Identifier.Identifier}"\`\n` + if (member.Type === "FieldDeclaration") { + decRet += `${firstLetterUppercase(member.Identifier.Identifier)} ${ + typeMap[member.TypeAnnotation.AnnotatedType.Identifier.Identifier] + } \`godence:"${member.Identifier.Identifier}"\`\n`; } } - decRet += '\n}' + decRet += "\n}"; } - ret += decRet + ret += decRet; } - return ret + return ret; } // return a promise -const transform = async(value: string) => { - const ret = await cadenceToGo(value) - return gofmt(ret) -} +const transform = async (value: string) => { + const ret = await cadenceToGo(value); + return gofmt(ret); +}; export default function CadenceToGo() { - const transformer = useCallback(({ value }) => transform(value),[]); + const transformer = useCallback(({ value }) => transform(value), []); return ( = 2.1.2 < 3", "safer-buffer@>= 2.1.2 < 3.0.0", safer-buffer@^2.0.2, safer-buffer@^2.1.0, safer-buffer@~2.1.0: version "2.1.2" resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a" @@ -9447,7 +9534,7 @@ string-width@^1.0.1: is-fullwidth-code-point "^1.0.0" strip-ansi "^3.0.0" -string-width@^4.0.0: +string-width@^4.0.0, string-width@^4.2.3: version "4.2.3" resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== @@ -10077,17 +10164,24 @@ trim-right@^1.0.1: resolved "https://registry.yarnpkg.com/trim-right/-/trim-right-1.0.1.tgz#cb2e1203067e0c8de1f614094b9fe45704ea6003" integrity sha1-yy4SAwZ+DI3h9hQJS5/kVwTqYAM= -ts-json-schema-generator@^0.93.0: - version "0.93.0" - resolved "https://registry.yarnpkg.com/ts-json-schema-generator/-/ts-json-schema-generator-0.93.0.tgz#3d6adf99446a1b3d0887dbad7cca015a49394d3a" - integrity sha512-JYacSIgw4KqsOXF/zRSY4pE/v6jUk7aMDXhwK5MdopN0UeKH58TRZHrQADy9uxTf78jqUfFLzARQKNOb9H+jVQ== +ts-node@^10.2.1: + version "10.9.1" + resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-10.9.1.tgz#e73de9102958af9e1f0b168a6ff320e25adcff4b" + integrity sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw== dependencies: - "@types/json-schema" "^7.0.7" - commander "^7.2.0" - fast-json-stable-stringify "^2.1.0" - glob "^7.1.7" - json-stable-stringify "^1.0.1" - typescript "~4.3.2" + "@cspotcode/source-map-support" "^0.8.0" + "@tsconfig/node10" "^1.0.7" + "@tsconfig/node12" "^1.0.7" + "@tsconfig/node14" "^1.0.0" + "@tsconfig/node16" "^1.0.2" + acorn "^8.4.1" + acorn-walk "^8.1.1" + arg "^4.1.0" + create-require "^1.1.0" + diff "^4.0.1" + make-error "^1.1.1" + v8-compile-cache-lib "^3.0.1" + yn "3.1.1" ts-pnp@^1.1.6: version "1.2.0" @@ -10229,7 +10323,21 @@ typescript-compiler@^1.4.1-2: resolved "https://registry.yarnpkg.com/typescript-compiler/-/typescript-compiler-1.4.1-2.tgz#ba4f7db22d91534a1929d90009dce161eb72fd3f" integrity sha1-uk99si2RU0oZKdkACdzhYety/T8= -typescript@4.3.4, typescript@^4.0.2, typescript@~4.3.2: +typescript-json-schema@^0.54.0: + version "0.54.0" + resolved "https://registry.yarnpkg.com/typescript-json-schema/-/typescript-json-schema-0.54.0.tgz#b3fc42ad90df6a0f6ab57571ebc8b4d41125df4f" + integrity sha512-/MNhm1pjdxXiVspjjyRCrQAA1B768cRzHU83aIqN5vQqQEW2NgyyKOfcguiRIMM64lseIZIelegnHOHEu7YDCg== + dependencies: + "@types/json-schema" "^7.0.9" + "@types/node" "^16.9.2" + glob "^7.1.7" + path-equal "^1.1.2" + safe-stable-stringify "^2.2.0" + ts-node "^10.2.1" + typescript "~4.6.0" + yargs "^17.1.1" + +typescript@4.3.4, typescript@^4.0.2: version "4.3.4" resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.3.4.tgz#3f85b986945bcf31071decdd96cf8bfa65f9dcbc" integrity sha512-uauPG7XZn9F/mo+7MrsRjyvbxFpzemRjKEZXS4AK83oP2KKOJPvb+9cO/gmnv8arWZvhnjVOXz7B49m1l0e9Ew== @@ -10244,6 +10352,11 @@ typescript@^4.5.2: resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.7.4.tgz#1a88596d1cf47d59507a1bcdfb5b9dfe4d488235" integrity sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ== +typescript@~4.6.0: + version "4.6.4" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.6.4.tgz#caa78bbc3a59e6a5c510d35703f6a09877ce45e9" + integrity sha512-9ia/jWHIEbo49HfjrLGfKbZSuWo9iTMwXO+Ca3pRsSpbsMbc7/IU8NKdCZVRRBafVPGnoJeFL76ZOAA84I9fEg== + ua-parser-js@^0.7.18: version "0.7.21" resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.21.tgz#853cf9ce93f642f67174273cc34565ae6f308777" @@ -10509,6 +10622,11 @@ uuid@^3.0.1, uuid@^3.3.2: resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee" integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A== +v8-compile-cache-lib@^3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz#6336e8d71965cb3d35a1bbb7868445a7c05264bf" + integrity sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg== + validate-npm-package-license@^3.0.1: version "3.0.4" resolved "https://registry.yarnpkg.com/validate-npm-package-license/-/validate-npm-package-license-3.0.4.tgz#fc91f6b9c7ba15c857f4cb2c5defeec39d4f410a" @@ -10821,6 +10939,11 @@ yargs-parser@^20.2.2: resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.9.tgz#2eb7dc3b0289718fc295f362753845c41a0c94ee" integrity sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w== +yargs-parser@^21.0.0: + version "21.1.1" + resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-21.1.1.tgz#9096bceebf990d21bb31fa9516e0ede294a77d35" + integrity sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw== + yargs@^15.3.1: version "15.4.1" resolved "https://registry.yarnpkg.com/yargs/-/yargs-15.4.1.tgz#0d87a16de01aee9d8bec2bfbf74f67851730f4f8" @@ -10851,6 +10974,19 @@ yargs@^16.1.0: y18n "^5.0.5" yargs-parser "^20.2.2" +yargs@^17.1.1: + version "17.6.0" + resolved "https://registry.yarnpkg.com/yargs/-/yargs-17.6.0.tgz#e134900fc1f218bc230192bdec06a0a5f973e46c" + integrity sha512-8H/wTDqlSwoSnScvV2N/JHfLWOKuh5MVla9hqLjK3nsfyy6Y4kDSYSvkU5YCUEPOSnRXfIyx3Sq+B/IWudTo4g== + dependencies: + cliui "^8.0.1" + escalade "^3.1.1" + get-caller-file "^2.0.5" + require-directory "^2.1.1" + string-width "^4.2.3" + y18n "^5.0.5" + yargs-parser "^21.0.0" + yargs@~4.6.0: version "4.6.0" resolved "https://registry.yarnpkg.com/yargs/-/yargs-4.6.0.tgz#cb4050c0159bfb6bb649c0f4af550526a84619dc" @@ -10877,6 +11013,11 @@ yauzl@^2.4.2: buffer-crc32 "~0.2.3" fd-slicer "~1.1.0" +yn@3.1.1: + version "3.1.1" + resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50" + integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q== + yocto-queue@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"