Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add binding gen js for remote and local #12

Merged
merged 35 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
a2c8c05
init check in for generating binding files based on flix
bthaile Sep 1, 2023
6f03fb0
add sub templates for js files, gen binding file in same dir as templ…
bthaile Sep 1, 2023
389069b
organization of files and ignores
bthaile Sep 1, 2023
7451e2a
ignore built file
bthaile Sep 1, 2023
4ab973f
ignore template files
bthaile Sep 1, 2023
016c059
ignore template files
bthaile Sep 1, 2023
5a50239
add file reader to config for unit testing
bthaile Sep 1, 2023
50d5bcb
verify that remote templates can gen js files
bthaile Sep 5, 2023
1b9b440
fix git ignore file
bthaile Sep 5, 2023
37a9f3d
ignore cmd dir and all .DS_Store files
bthaile Sep 5, 2023
6c242b7
remove .DS_Store files
bthaile Sep 5, 2023
f9d4bf4
add testing gen js binding with remote flix
bthaile Sep 5, 2023
d15e7ca
support array of simple types
bthaile Sep 5, 2023
d4848cf
first batch of changes based on feedback
bthaile Sep 6, 2023
2098974
collapse files and keep everything in flixkit package
bthaile Sep 7, 2023
530a385
clean up
bthaile Sep 7, 2023
b7a142c
add js docs to provide types for typescript
bthaile Sep 8, 2023
870b6ff
do not rely on message to be there and that us-en exists, update temp…
bthaile Sep 8, 2023
805a993
fix consistency for testing
bthaile Sep 8, 2023
dac9742
simplify conversion to js types, fcl uses strings for all numbers
bthaile Sep 14, 2023
4a9cf9a
add golden for unit tests
bthaile Sep 26, 2023
bd1a7a9
pass in generator method to generate code
bthaile Sep 26, 2023
5aef7f0
use interface and set specific port for testing remote and use golden…
bthaile Sep 27, 2023
e7cd1bc
fixed arguments key sorting issue, that was causing test failures
bthaile Sep 27, 2023
4932623
fix case typo in generator name
bthaile Sep 28, 2023
64e11a5
remove need to read from disk to get template
bthaile Sep 28, 2023
aa66b9f
tidy
bthaile Sep 29, 2023
9d27901
move js generator to bindings module and rename js generator to fcl j…
bthaile Oct 4, 2023
8c19379
formatting
bthaile Oct 16, 2023
0593679
pass in template directory to be more flexible
bthaile Oct 18, 2023
5a9f441
add comment that fcl version 1.3.0 or greater is needed to use templates
bthaile Oct 18, 2023
8857770
add helper func to create fcl js code generator
bthaile Oct 18, 2023
9cd4a66
removed todo: added named return values from method
bthaile Oct 21, 2023
5ad7e6a
update readme to include bindings module
bthaile Oct 21, 2023
6619788
use filepath.Walk to get files from template dir
bthaile Oct 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 68 additions & 7 deletions javascript.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ import (

type SimpleParameter struct {
Name string
Type string
JsType string
bthaile marked this conversation as resolved.
Show resolved Hide resolved
Description string
FclType string
CadType string
}

type TemplateData struct {
Version string
Parameters []SimpleParameter
Title string
Description string
Location string
IsScript bool
IsLocalTemplate bool
Expand All @@ -34,10 +38,12 @@ func GenerateJavaScript(flix *FlowInteractionTemplate, templateLocation string,
}

methodName := TitleToMethodName(flix.Data.Messages.Title.I18N["en-US"])
bthaile marked this conversation as resolved.
Show resolved Hide resolved
description := flix.Data.Messages.Description.I18N["en-US"]
data := TemplateData{
Version: flix.FVersion,
Parameters: TransformArguments(flix.Data.Arguments),
Title: methodName,
Description: description,
Location: templateLocation,
IsScript: flix.IsScript(),
IsLocalTemplate: isLocal,
Expand Down Expand Up @@ -73,22 +79,77 @@ func TitleToMethodName(s string) string {
func TransformArguments(args Arguments) []SimpleParameter {
bthaile marked this conversation as resolved.
Show resolved Hide resolved
simpleArgs := []SimpleParameter{}
for name, arg := range args {
isArray, arrayType := IsArrayParameter(arg)
isArray, cType, jsType := IsArrayParameter(arg)
desciption := arg.Messages.Title.I18N["en-US"]
if isArray {
simpleArgs = append(simpleArgs, SimpleParameter{Name: name, Type: "Array(t." + arrayType + ")"})
simpleArgs = append(simpleArgs, SimpleParameter{Name: name, CadType: cType, JsType: jsType, FclType: "Array(t." + cType + ")", Description: desciption})
} else {
simpleArgs = append(simpleArgs, SimpleParameter{Name: name, Type: arg.Type})
jsType := ConvertCadenceTypeToJS(arg.Type)
simpleArgs = append(simpleArgs, SimpleParameter{Name: name, CadType: arg.Type, JsType: jsType, FclType: arg.Type, Description: desciption})
}
}
return simpleArgs
}


func IsArrayParameter(arg Argument) (bool, string) {
func IsArrayParameter(arg Argument) (bool, string, string) {
isArray := arg.Type[0] == '[' && arg.Type[len(arg.Type)-1] == ']'
bthaile marked this conversation as resolved.
Show resolved Hide resolved
if (!isArray) {
return isArray, ""
return isArray, "", ""
}
return isArray, arg.Type[1 : len(arg.Type)-1]
cadenceType := arg.Type[1 : len(arg.Type)-1]
jsType := "Array<" + ConvertCadenceTypeToJS(cadenceType) + ">"
return isArray, cadenceType, jsType
}

// ConvertCadenceTypeToJS takes a Cadence type as a string and returns its JavaScript equivalent
func ConvertCadenceTypeToJS(cadenceType string) string {
switch cadenceType {
case "Int":
return "Number"
case "Int8":
return "Number"
case "Int16":
return "Number"
case "Int32":
return "Number"
case "Int64":
return "BigInt"
case "Int128":
return "BigInt"
case "Int256":
return "BigInt"
case "UInt":
return "string"
case "UInt8":
return "string"
case "UInt16":
return "string"
case "UInt32":
return "string"
case "UInt64":
return "string"
case "UInt128":
return "string"
case "UInt256":
return "string"
case "UFix64":
return "string"
case "Fix64":
return "BigInt"
bthaile marked this conversation as resolved.
Show resolved Hide resolved
case "String":
return "string"
case "Character":
return "string"
case "Bool":
return "boolean"
case "Address":
return "string"
case "Void":
return "void"
default:
// For composite and resource types, you can customize further.
// For now, let's just return 'any' for unknown or complex types
return "any"
}
}
11 changes: 8 additions & 3 deletions javascript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ var parsedTemplateScript = &FlowInteractionTemplate{
Messages: Messages{
Title: &Title{
I18N: map[string]string{
"en-US": "Transfer Tokens",
"en-US": "Multiple Two Integers",
},
},
Description: &Description{
I18N: map[string]string{
"en-US": "Transfer tokens from one account to another",
"en-US": "Multiply two numbers to another",
},
},
},
Expand Down Expand Up @@ -171,16 +171,20 @@ func TestJSGenTransaction(t *testing.T) {
assert.NoError(err, "ParseTemplate should not return an error")
assert.NotNil(contents, "Parsed template should not be nil")
assert.True(strings.Contains(contents, "await fcl.mutate("), "Expected '%s'", "await fcl.mutate(")


}


func TestJSGenScript(t *testing.T) {
assert := assert.New(t)

contents, err := GenerateJavaScript(parsedTemplateScript, "./transfer_token.json", true)
contents, err := GenerateJavaScript(parsedTemplateScript, "./multiply_two_integers.json", true)
assert.NoError(err, "ParseTemplate should not return an error")
assert.NotNil(contents, "Parsed template should not be nil")
assert.True(strings.Contains(contents, "await fcl.query("), "Expected '%s'", "await fcl.query(")


}

func TestJSGenArrayScript(t *testing.T) {
Expand All @@ -191,4 +195,5 @@ func TestJSGenArrayScript(t *testing.T) {
assert.NotNil(contents, "Parsed template should not be nil")
assert.True(strings.Contains(contents, "await fcl.query("), "Expected '%s'", "await fcl.query(")
assert.True(strings.Contains(contents, `args: (arg, t) => [arg(numbers, t.Array(t.Int))]`), "Expected '%s'", `args: (arg, t) => [arg(numbers, t.Array(t.Int))]`)

}
15 changes: 14 additions & 1 deletion templates/js_main.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,30 @@ import flixTemplate from "{{.Location}}"
const flixTemplate = "{{.Location}}"
{{- end}}

// TODO: add runtime check for parameters consistency
const parameterNames = [
{{- range $index, $ele := .Parameters -}}
{{if $index}}, {{end}}"{{$ele.Name}}"
{{- end -}}
];
{{"\n"}}/**
* {{.Description}}{{"\n"}}
{{- if gt (len .Parameters) 0 -}}
* @param {Object} Parameters - parameters for the cadence
{{- range $index, $ele := .Parameters -}}
{{"\n"}}* @param {{"{"}} {{$ele.JsType}} {{"}"}} Parameters.{{$ele.Name}} - {{$ele.Description}}: {{$ele.CadType}}
{{- end -}}
{{ end -}}
{{- if not .IsScript -}}
{{"\n"}}* @returns {Promise<string>} - returns a promise which resolves to the transaction id
{{- end -}}
{{"\n"}}*/

{{if .IsScript}}
{{- template "js_script.tmpl" .}}
bthaile marked this conversation as resolved.
Show resolved Hide resolved
{{else}}
{{- template "js_tx.tmpl" .}}
{{end}}
{{- end}}



2 changes: 1 addition & 1 deletion templates/js_script.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function {{.Title}}({
{{ if len .Parameters -}}
args: (arg, t) => [
{{- range $index, $ele := .Parameters -}}
{{if $index}}, {{end}}arg({{.Name}}, t.{{.Type}})
{{if $index}}, {{end}}arg({{.Name}}, t.{{.FclType}})
{{- end -}}
]
{{- end }}
Expand Down
2 changes: 1 addition & 1 deletion templates/js_tx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function {{.Title}}({
{{ if len .Parameters -}}
args: (arg, t) => [
{{- range $index, $ele := .Parameters -}}
{{if $index}}, {{end}}arg({{.Name}}, t.{{.Type}})
{{if $index}}, {{end}}arg({{.Name}}, t.{{.FclType}})
{{- end -}}
]
{{- end }}
Expand Down