-
Notifications
You must be signed in to change notification settings - Fork 7
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
Changes from all commits
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 6f03fb0
add sub templates for js files, gen binding file in same dir as templ…
bthaile 389069b
organization of files and ignores
bthaile 7451e2a
ignore built file
bthaile 4ab973f
ignore template files
bthaile 016c059
ignore template files
bthaile 5a50239
add file reader to config for unit testing
bthaile 50d5bcb
verify that remote templates can gen js files
bthaile 1b9b440
fix git ignore file
bthaile 37a9f3d
ignore cmd dir and all .DS_Store files
bthaile 6c242b7
remove .DS_Store files
bthaile f9d4bf4
add testing gen js binding with remote flix
bthaile d15e7ca
support array of simple types
bthaile d4848cf
first batch of changes based on feedback
bthaile 2098974
collapse files and keep everything in flixkit package
bthaile 530a385
clean up
bthaile b7a142c
add js docs to provide types for typescript
bthaile 870b6ff
do not rely on message to be there and that us-en exists, update temp…
bthaile 805a993
fix consistency for testing
bthaile dac9742
simplify conversion to js types, fcl uses strings for all numbers
bthaile 4a9cf9a
add golden for unit tests
bthaile bd1a7a9
pass in generator method to generate code
bthaile 5aef7f0
use interface and set specific port for testing remote and use golden…
bthaile e7cd1bc
fixed arguments key sorting issue, that was causing test failures
bthaile 4932623
fix case typo in generator name
bthaile 64e11a5
remove need to read from disk to get template
bthaile aa66b9f
tidy
bthaile 9d27901
move js generator to bindings module and rename js generator to fcl j…
bthaile 8c19379
formatting
bthaile 0593679
pass in template directory to be more flexible
bthaile 5a9f441
add comment that fcl version 1.3.0 or greater is needed to use templates
bthaile 8857770
add helper func to create fcl js code generator
bthaile 9cd4a66
removed todo: added named return values from method
bthaile 5ad7e6a
update readme to include bindings module
bthaile 6619788
use filepath.Walk to get files from template dir
bthaile File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# ignore all .DS_Store files | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package bindings | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"sort" | ||
"text/template" | ||
|
||
"github.com/onflow/flixkit-go" | ||
"github.com/stoewer/go-strcase" | ||
) | ||
|
||
type simpleParameter struct { | ||
Name string | ||
JsType string | ||
Description string | ||
FclType string | ||
CadType string | ||
} | ||
|
||
type templateData struct { | ||
Version string | ||
Parameters []simpleParameter | ||
Title string | ||
Description string | ||
Location string | ||
IsScript bool | ||
IsLocalTemplate bool | ||
} | ||
|
||
type FclJSGenerator struct { | ||
TemplateDir string | ||
} | ||
|
||
func NewFclJSGenerator() *FclJSGenerator { | ||
_, currentFilePath, _, _ := runtime.Caller(0) | ||
baseDir := filepath.Dir(currentFilePath) | ||
templateDir := filepath.Join(baseDir, "templates") | ||
|
||
return &FclJSGenerator{ | ||
TemplateDir: templateDir, | ||
} | ||
} | ||
|
||
func (g FclJSGenerator) Generate(flix *flixkit.FlowInteractionTemplate, templateLocation string, isLocal bool) (string, error) { | ||
templateFiles, err := getAllFiles(g.TemplateDir) | ||
if err != nil { | ||
return "", err | ||
} | ||
tmpl, err := template.ParseFiles(templateFiles...) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
methodName := strcase.LowerCamelCase(flix.Data.Messages.GetTitleValue("Request")) | ||
description := flix.GetDescription() | ||
data := templateData{ | ||
Version: flix.FVersion, | ||
Parameters: transformArguments(flix.Data.Arguments), | ||
Title: methodName, | ||
Description: description, | ||
Location: templateLocation, | ||
IsScript: flix.IsScript(), | ||
IsLocalTemplate: isLocal, | ||
} | ||
|
||
var buf bytes.Buffer | ||
err = tmpl.Execute(&buf, data) | ||
return buf.String(), err | ||
} | ||
|
||
func transformArguments(args flixkit.Arguments) []simpleParameter { | ||
simpleArgs := []simpleParameter{} | ||
var keys []string | ||
// get keys for sorting | ||
for k := range args { | ||
keys = append(keys, k) | ||
} | ||
|
||
sort.SliceStable(keys, func(i, j int) bool { | ||
return args[keys[i]].Index < args[keys[j]].Index | ||
}) | ||
for _, key := range keys { | ||
arg := args[key] | ||
isArray, cType, jsType := isArrayParameter(arg) | ||
desciption := arg.Messages.GetTitleValue("") | ||
if isArray { | ||
simpleArgs = append(simpleArgs, simpleParameter{Name: key, CadType: cType, JsType: jsType, FclType: "Array(t." + cType + ")", Description: desciption}) | ||
} else { | ||
jsType := convertCadenceTypeToJS(arg.Type) | ||
simpleArgs = append(simpleArgs, simpleParameter{Name: key, CadType: arg.Type, JsType: jsType, FclType: arg.Type, Description: desciption}) | ||
} | ||
} | ||
return simpleArgs | ||
} | ||
|
||
func isArrayParameter(arg flixkit.Argument) (isArray bool, cType string, jsType string) { | ||
if arg.Type == "" || arg.Type[0] != '[' { | ||
return false, "", "" | ||
} | ||
cadenceType := arg.Type[1 : len(arg.Type)-1] | ||
javascriptType := "Array<" + convertCadenceTypeToJS(cadenceType) + ">" | ||
return true, cadenceType, javascriptType | ||
} | ||
|
||
func convertCadenceTypeToJS(cadenceType string) string { | ||
// need to determine js type based on fcl supported types | ||
// looking at fcl types and how arguments work as parameters | ||
// https://github.com/onflow/fcl-js/blob/master/packages/types/src/types.js | ||
switch cadenceType { | ||
case "Bool": | ||
return "boolean" | ||
case "Void": | ||
return "void" | ||
case "Dictionary": | ||
return "object" | ||
case "Struct": | ||
return "object" | ||
case "Enum": | ||
return "object" | ||
default: | ||
return "string" | ||
} | ||
} | ||
|
||
func getAllFiles(dir string) ([]string, error) { | ||
var files []string | ||
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
// If it's a directory, skip it | ||
if info.IsDir() { | ||
return nil | ||
} | ||
files = append(files, path) | ||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return files, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be lowercased?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's publicly assessable. I thought it needed to be uppercase so calls can get to ti.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has to be uppercased for other packages to access it yes.