Feat: new cadence to go transform backend (#363)

*   remove cadence officai parser wasm

*  update cadence to go

*  provide a option to generate all code, default is disabled
This commit is contained in:
LemonNeko
2023-05-04 12:21:22 +05:30
committed by GitHub
parent ade2922f12
commit d893a2baee
5 changed files with 84 additions and 146 deletions
+33 -49
View File
@@ -73,14 +73,14 @@ export const css2 = `.alert {
.button {
padding: 0.5rem 1rem;
width: 100%;
}
}
}
@media (min-width: 1280px) {
.button {
padding: 3rem 7rem;
margin-bottom: 2.4rem;
}
}
}
.username {
@@ -99,7 +99,7 @@ export const css2 = `.alert {
@media (min-width: 1280px) {
.username {
width: 50%;
}
}
}
.footer {
@@ -335,7 +335,7 @@ export const graphqlMongodb = `type User @entity {
chats: [Chat!]! @link
}
type Profile @entity(embedded: true,
type Profile @entity(embedded: true,
additionalFields: [
{ path: "dateOfBirth", type: "string" }
]) {
@@ -463,7 +463,7 @@ import { useState, useEffect } from 'react';
export const CounterExample: React.FC<{}> = () => {
const [count, setCount] = useState(0);
const handleClick = () => setCount(count + 1)
return (
@@ -499,50 +499,34 @@ completed = false
id = 12
name = "Transform Inc"`;
export const cadence = `pub struct StructContainsManyType {
pub var intValue: Int
pub var int8Value: Int8
pub var int16Value: Int16
pub var int32Value: Int32
pub var int64Value: Int64
pub var int128Value: Int128
pub var int256Value: Int256
pub var uintValue: UInt
pub var uint8Value: UInt8
pub var uint16Value: UInt16
pub var uint32Value: UInt32
pub var uint64Value: UInt64
pub var uint128Value: UInt128
pub var uint256Value: UInt256
pub var stringValue: String
pub var addressValue: Address
pub var boolValue: Bool
pub var characterValue: Character
pub var pathValue: Path
export const cadence = `// Do not remove top level contract
// Just paste your structs in this contract
pub contract Example {
pub struct ExampleStruct {
pub var url: String
pub twoCapacityArray: [String;2]
pub deeeeepArray: [[[[String]]]]
init() {
self.intValue = 127
self.int8Value = 127
self.int16Value = 127
self.int32Value = 127
self.int64Value = 127
self.int128Value = 127
self.int256Value = 127
self.uintValue = 127
self.uint8Value = 127
self.uint16Value = 127
self.uint32Value = 127
self.uint64Value = 127
self.uint128Value = 127
self.uint256Value = 127
self.stringValue = "LemonNeko"
self.addressValue = 0x0
self.boolValue = true
self.characterValue = "a"
self.pathValue = /storage/CollectionStorage
}
}
pub fun main(): StructContainsManyType {
return StructContainsManyType()
pub struct ExampleEmbed {
pub var aNumber: Int
init() {
self.aNumber = 0
}
}
init() {
self.url = ""
self.twoCapacityArray = []
self.deeeeepArray = []
}
}
pub event ExampleEvent(_ name: String, _ aDeeeeeepMap: {String:{String:{String:{String:String}}}})
// This function will be ignored if 'Generate Interaction Code With Functions' disabled
pub fun hello(): String {
return "Hello"
}
}
`;
+1 -1
View File
@@ -40,6 +40,7 @@
"@graphql-codegen/typescript-urql": "^2.0.9",
"@iarna/toml": "^3.0.0",
"@khanacademy/flow-to-ts": "^0.5.2",
"@lemonneko/easi-gen": "^0.0.36",
"@monaco-editor/react": "^4.2.1",
"@openapi-contrib/json-schema-to-openapi-schema": "^2.0.0",
"@svgr/core": "^5.5.0",
@@ -100,7 +101,6 @@
"yaml": "^1.10.2"
},
"devDependencies": {
"@lemonneko/flow__cadence-parser": "^0.24.1",
"@types/lodash": "^4.14.170",
"@types/node": "^15.12.5",
"@types/prettier": "^2.3.0",
+45 -62
View File
@@ -1,78 +1,61 @@
import ConversionPanel from "@components/ConversionPanel";
import * as React from "react";
import { useCallback } from "react";
import { Alert } from "evergreen-ui";
import gofmt from "gofmt.js";
import { CadenceParser } from "@lemonneko/flow__cadence-parser";
import { useCallback, useState } from "react";
import { newEasiGen } from "@lemonneko/easi-gen";
import { EditorPanelProps } from "@components/EditorPanel";
import Form, { InputType } from "@components/Form";
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'
}
function firstLetterUppercase(id: string): string {
return id.charAt(0).toUpperCase() + id.slice(1)
}
async function cadenceToGo(value: string): Promise<string> {
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 = ''
for (const dec of declarations) {
let decRet = ''
// only struct can be converted
if (dec.CompositeKind === "CompositeKindStructure") {
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`
}
}
decRet += '\n}'
}
ret += decRet
}
return ret
}
// return a promise
const transform = async(value: string) => {
const ret = await cadenceToGo(value)
return gofmt(ret)
interface Settings {
generateContractCode: boolean;
}
export default function CadenceToGo() {
const transformer = useCallback(({ value }) => transform(value),[]);
let generator: (source: string, ignoreContractGeneration: boolean) => string;
const [settings, setSettings] = useState<Settings>({
generateContractCode: false
});
React.useEffect(console.log, [settings]);
const transformer = useCallback(
async ({ value }) => {
if (!generator) generator = await newEasiGen();
const generated = generator(value, !settings.generateContractCode);
return generated;
},
[settings]
);
const outputSettingsElement = useCallback<EditorPanelProps["settingElement"]>(
({ open, toggle }) => {
return (
<Form<Partial<Settings>>
initialValues={settings}
open={open}
toggle={toggle}
title={"Output Settings"}
onSubmit={setSettings as any}
formsFields={[
{
key: "generateContractCode",
type: InputType.SWITCH,
label: "Generate Interaction Code With Functions"
}
]}
/>
);
},
[]
);
return (
<ConversionPanel
transformer={transformer}
editorTitle="Cadence"
editorTitle="Cadence types"
editorLanguage="text"
editorDefaultValue="cadence"
resultTitle="Go struct"
resultTitle="Go types"
resultLanguage={"go"}
settings={settings}
resultSettingsElement={outputSettingsElement}
/>
);
}
+1 -1
View File
@@ -386,7 +386,7 @@ export const categorizedRoutes = [
path: "/toml-to-yaml"
},
{
label: "Cadence struct to Go struct",
label: "Cadence to Go",
path: "/cadence-to-go"
}
]
+4 -33
View File
@@ -1199,12 +1199,10 @@
prettier "2.2.1"
trim-right "^1.0.1"
"@lemonneko/flow__cadence-parser@^0.24.1":
version "0.24.1"
resolved "https://registry.npmjs.org/@lemonneko/flow__cadence-parser/-/flow__cadence-parser-0.24.1.tgz#5c22aa59c241930b35cd6889487c2aaeebe67684"
integrity sha512-WsZp5NqtGWsq38j0XYAPOPe0YWwaVlUi5rBJygc7Ty1gcTZDLb+OdiDygkbPRnMlRsbEV9n/u17yf8zivm2jjg==
dependencies:
get-random-values "^2.0.0"
"@lemonneko/easi-gen@^0.0.36":
version "0.0.36"
resolved "https://registry.npmjs.org/@lemonneko/easi-gen/-/easi-gen-0.0.36.tgz#14461ed3b3df2e9c9057e1f9044d1cf8409a119e"
integrity sha512-ius5OBLoBkgX0GtBg3YBwKKHsOArGQu0OggWqmSh8nF53Yc6McPJrWC54L0oTalViHpJX1d+wbx7HUZOm09scA==
"@monaco-editor/loader@^1.1.1":
version "1.1.1"
@@ -3808,11 +3806,6 @@ dom-serializer@0:
domelementtype "^2.0.1"
entities "^2.0.0"
dom-walk@^0.1.0:
version "0.1.2"
resolved "https://registry.npmjs.org/dom-walk/-/dom-walk-0.1.2.tgz#0c548bef048f4d1f2a97249002236060daa3fd84"
integrity sha512-6QvTW9mrGeIegrFXdtQi9pk7O/nSK6lSdXW2eqUspN5LWD7UTji2Fqw5V2YLjBpHEoU9Xl/eUWNpDeZvoyOv2w==
domain-browser@4.19.0:
version "4.19.0"
resolved "https://registry.yarnpkg.com/domain-browser/-/domain-browser-4.19.0.tgz#1093e17c0a17dbd521182fe90d49ac1370054af1"
@@ -4864,13 +4857,6 @@ get-proxy@^2.0.0:
dependencies:
npm-conf "^1.1.0"
get-random-values@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/get-random-values/-/get-random-values-2.0.0.tgz#d4ae7033b7dbd3c1298a06c4c387353a56ed81a2"
integrity sha512-wWq8YlpDqoduAzHDnjwh1sX0xxsbChAHFhWK9sDlaxMi3SJchPOhFdMf3/J/xfpzSRcE4UE5BjfFZVDKmGRh/g==
dependencies:
global "^4.4.0"
get-stdin@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/get-stdin/-/get-stdin-4.0.1.tgz#b968c6b0a04384324902e8bf1a5df32579a450fe"
@@ -5021,14 +5007,6 @@ glob@^7.1.7:
once "^1.3.0"
path-is-absolute "^1.0.0"
global@^4.4.0:
version "4.4.0"
resolved "https://registry.npmjs.org/global/-/global-4.4.0.tgz#3e7b105179006a323ed71aafca3e9c57a5cc6406"
integrity sha512-wv/LAoHdRE3BeTGz53FAamhGlPLhlssK45usmGFThIi4XqnBmjKQ16u+RNbP7WvigRZDxUsM0J3gcQ5yicaL0w==
dependencies:
min-document "^2.19.0"
process "^0.11.10"
globals@^11.1.0:
version "11.12.0"
resolved "https://registry.yarnpkg.com/globals/-/globals-11.12.0.tgz#ab8795338868a0babd8525758018c2a7eb95c42e"
@@ -6848,13 +6826,6 @@ mimic-response@^1.0.0:
resolved "https://registry.yarnpkg.com/mimic-response/-/mimic-response-1.0.1.tgz#4923538878eef42063cb8a3e3b0798781487ab1b"
integrity sha512-j5EctnkH7amfV/q5Hgmoal1g2QHFJRraOtmx0JpIqkxhBhI/lJSl1nMpQ45hVarwNETOoWEimndZ4QK0RHxuxQ==
min-document@^2.19.0:
version "2.19.0"
resolved "https://registry.npmjs.org/min-document/-/min-document-2.19.0.tgz#7bd282e3f5842ed295bb748cdd9f1ffa2c824685"
integrity sha512-9Wy1B3m3f66bPPmU5hdA4DR4PB2OfDU/+GS3yAB7IQozE3tqXaVv2zOjgla7MEGSRv95+ILmOuvhLkOK6wJtCQ==
dependencies:
dom-walk "^0.1.0"
min-indent@1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/min-indent/-/min-indent-1.0.1.tgz#a63f681673b30571fbe8bc25686ae746eefa9869"