mirror of
https://github.com/ritz078/transform.git
synced 2026-11-22 17:11:32 +03:00
✨ Add support for Cadence struct to Go struct (#338)
* 🚧 add parser for cadence * ✨ add transform * 👌 remove console.log Co-authored-by: Ritesh Kumar <ritz078@users.noreply.github.com> * 👌 remove console.log Co-authored-by: Ritesh Kumar <ritz078@users.noreply.github.com> * 👌 change editor language to text Co-authored-by: Ritesh Kumar <ritz078@users.noreply.github.com>
This commit is contained in:
1 parent
331649a379
commit
6188042be6
5 files changed
+165
No files matched your search
@@ -442,3 +442,51 @@ completed = false
|
||||
[company]
|
||||
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
|
||||
|
||||
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()
|
||||
}
|
||||
`
|
||||
@@ -101,6 +101,7 @@
|
||||
"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",
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
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";
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
export default function CadenceToGo() {
|
||||
const transformer = useCallback(({ value }) => transform(value),[]);
|
||||
|
||||
return (
|
||||
<ConversionPanel
|
||||
transformer={transformer}
|
||||
editorTitle="Cadence"
|
||||
editorLanguage="text"
|
||||
editorDefaultValue="cadence"
|
||||
resultTitle="Go struct"
|
||||
resultLanguage={"go"}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -378,6 +378,10 @@ export const categorizedRoutes = [
|
||||
{
|
||||
label: "TOML to YAML",
|
||||
path: "/toml-to-yaml"
|
||||
},
|
||||
{
|
||||
label: "Cadence struct to Go struct",
|
||||
path: "/cadence-to-go"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -1214,6 +1214,13 @@
|
||||
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"
|
||||
|
||||
"@monaco-editor/loader@^1.1.1":
|
||||
version "1.1.1"
|
||||
resolved "https://registry.yarnpkg.com/@monaco-editor/loader/-/loader-1.1.1.tgz#37db648c81a86946d0febd391de00df9c28a0a3d"
|
||||
@@ -3889,6 +3896,11 @@ 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"
|
||||
@@ -4943,6 +4955,13 @@ 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"
|
||||
@@ -5093,6 +5112,14 @@ 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"
|
||||
@@ -6954,6 +6981,13 @@ 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"
|
||||
|
||||
Reference in new issue
Block a user