Skip to content

Commit

Permalink
simplify interfaces and method sig that consumers call to create bind…
Browse files Browse the repository at this point in the history
…ing files and generate templates
  • Loading branch information
bthaile committed Nov 29, 2023
1 parent 4200c89 commit 24f6286
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
35 changes: 34 additions & 1 deletion bindings/fcl-js.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package bindings

import (
"bytes"
"fmt"
"net/url"
"path/filepath"
"sort"
"text/template"

Expand Down Expand Up @@ -44,11 +47,19 @@ func NewFclJSGenerator() *FclJSGenerator {
}
}

func (g FclJSGenerator) Generate(flix *flixkit.FlowInteractionTemplate, templateLocation string, isLocal bool) (string, error) {
func (g FclJSGenerator) Generate(flixString string, templateLocation string) (string, error) {
tmpl, err := parseTemplates(g.Templates)
if err != nil {
return "", err
}
if flixString == "" {
return "", fmt.Errorf("no flix template provided")
}
flix, err := flixkit.ParseFlix(flixString)
if err != nil {
return "", err
}
isLocal := !isUrl(templateLocation)

methodName := strcase.LowerCamelCase(flix.Data.Messages.GetTitleValue("Request"))
description := flix.GetDescription()
Expand All @@ -67,6 +78,11 @@ func (g FclJSGenerator) Generate(flix *flixkit.FlowInteractionTemplate, template
return buf.String(), err
}

func isUrl(str string) bool {
u, err := url.Parse(str)
return err == nil && u.Scheme != "" && u.Host != ""
}

func transformArguments(args flixkit.Arguments) []simpleParameter {
simpleArgs := []simpleParameter{}
var keys []string
Expand Down Expand Up @@ -133,3 +149,20 @@ func parseTemplates(templates []string) (*template.Template, error) {

return baseTemplate, nil
}

// GetRelativePath computes the relative path from generated file to flix json file.
// This path is used in the binding file to reference the flix json file.
func GetRelativePath(configFile, bindingFile string) (string, error) {
relPath, err := filepath.Rel(filepath.Dir(bindingFile), configFile)
if err != nil {
return "", err
}

// If the file is in the same directory and doesn't start with "./", prepend it.
if !filepath.IsAbs(relPath) && relPath[0] != '.' {
relPath = "./" + relPath
}

// Currently binding files are js, we need to convert the path to unix style
return filepath.ToSlash(relPath), nil
}
25 changes: 20 additions & 5 deletions bindings/fcl-js_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bindings

import (
"encoding/json"
"testing"

"github.com/hexops/autogold/v2"
Expand Down Expand Up @@ -193,18 +194,23 @@ var minimumNoParamTemplate = &flixkit.FlowInteractionTemplate{
}

func TestJSGenTransaction(t *testing.T) {
ttemp, err := json.Marshal(parsedTemplateTX)
assert.NoError(t, err, "marshal template to json should not return an error")
generator := FclJSGenerator{
Templates: []string{
bindings.GetJsFclMainTemplate(),
bindings.GetJsFclScriptTemplate(),
bindings.GetJsFclTxTemplate(),
},
}
got, _ := generator.Generate(parsedTemplateTX, "./transfer_token.json", true)
got, _ := generator.Generate(string(ttemp), "./transfer_token.json")
autogold.ExpectFile(t, got)
}

func TestJSGenScript(t *testing.T) {
ttemp, err := json.Marshal(parsedTemplateScript)
assert.NoError(t, err, "marshal template to json should not return an error")

generator := FclJSGenerator{
Templates: []string{
bindings.GetJsFclMainTemplate(),
Expand All @@ -213,12 +219,15 @@ func TestJSGenScript(t *testing.T) {
},
}
assert := assert.New(t)
got, err := generator.Generate(parsedTemplateScript, "./multiply_two_integers.template.json", true)
got, err := generator.Generate(string(ttemp), "./multiply_two_integers.template.json")
assert.NoError(err, "ParseTemplate should not return an error")
autogold.ExpectFile(t, got)
}

func TestJSGenArrayScript(t *testing.T) {
ttemp, err := json.Marshal(ArrayTypeScript)
assert.NoError(t, err, "marshal template to json should not return an error")

generator := FclJSGenerator{
Templates: []string{
bindings.GetJsFclMainTemplate(),
Expand All @@ -228,24 +237,30 @@ func TestJSGenArrayScript(t *testing.T) {
}
assert := assert.New(t)

out, err := generator.Generate(ArrayTypeScript, "./multiply-numbers.template.json", true)
out, err := generator.Generate(string(ttemp), "./multiply-numbers.template.json")
assert.NoError(err, "ParseTemplate should not return an error")
autogold.ExpectFile(t, out)
}

func TestJSGenMinScript(t *testing.T) {
ttemp, err := json.Marshal(minimumTemplate)
assert.NoError(t, err, "marshal template to json should not return an error")

generator := NewFclJSGenerator()
assert := assert.New(t)

out, err := generator.Generate(minimumTemplate, "./min.template.json", true)
out, err := generator.Generate(string(ttemp), "./min.template.json")
assert.NoError(err, "ParseTemplate should not return an error")
autogold.ExpectFile(t, out)
}
func TestJSGenNoParamsScript(t *testing.T) {
ttemp, err := json.Marshal(minimumNoParamTemplate)
assert.NoError(t, err, "marshal template to json should not return an error")

generator := NewFclJSGenerator()
assert := assert.New(t)

out, err := generator.Generate(minimumNoParamTemplate, "./min.template.json", true)
out, err := generator.Generate(string(ttemp), "./min.template.json")
assert.NoError(err, "ParseTemplate should not return an error")
autogold.ExpectFile(t, out)
}
6 changes: 5 additions & 1 deletion flixkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ type FlowInteractionTemplateVersion struct {
FVersion string `json:"f_version"`
}

type Generator interface {
type GenerateTemplate interface {
Generate(ctx context.Context, code string, preFill string) (string, error)
}

type GenerateBinding interface {
Generate(flixString string, templateLocation string) (string, error)
}

type FlowInteractionTemplateCadence interface {
GetAndReplaceCadenceImports(templateName string) (string, error)
IsTransaction() bool
Expand Down
2 changes: 1 addition & 1 deletion generator/flixkitv1_0_0/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Generator struct {
}

// stubb to pass in parameters
func NewGenerator(deployedContracts []flixkit.Contracts, coreContracts flixkit.Contracts, logger output.Logger) (flixkit.Generator, error) {
func NewGenerator(deployedContracts []flixkit.Contracts, coreContracts flixkit.Contracts, logger output.Logger) (flixkit.GenerateTemplate, error) {
loader := afero.Afero{Fs: afero.NewOsFs()}

gwt, err := gateway.NewGrpcGateway(config.TestnetNetwork)
Expand Down

0 comments on commit 24f6286

Please sign in to comment.