From 24f62864a67cb14483392e1327a7c169a08c5cb5 Mon Sep 17 00:00:00 2001 From: Tom Haile Date: Wed, 29 Nov 2023 15:44:57 -0600 Subject: [PATCH] simplify interfaces and method sig that consumers call to create binding files and generate templates --- bindings/fcl-js.go | 35 +++++++++++++++++++++++++++- bindings/fcl-js_test.go | 25 ++++++++++++++++---- flixkit.go | 6 ++++- generator/flixkitv1_0_0/generator.go | 2 +- 4 files changed, 60 insertions(+), 8 deletions(-) diff --git a/bindings/fcl-js.go b/bindings/fcl-js.go index f2b1f94..aa39f0b 100644 --- a/bindings/fcl-js.go +++ b/bindings/fcl-js.go @@ -2,6 +2,9 @@ package bindings import ( "bytes" + "fmt" + "net/url" + "path/filepath" "sort" "text/template" @@ -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() @@ -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 @@ -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 +} diff --git a/bindings/fcl-js_test.go b/bindings/fcl-js_test.go index 6b6d5d9..3c4cd03 100644 --- a/bindings/fcl-js_test.go +++ b/bindings/fcl-js_test.go @@ -1,6 +1,7 @@ package bindings import ( + "encoding/json" "testing" "github.com/hexops/autogold/v2" @@ -193,6 +194,8 @@ 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(), @@ -200,11 +203,14 @@ func TestJSGenTransaction(t *testing.T) { 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(), @@ -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(), @@ -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) } diff --git a/flixkit.go b/flixkit.go index 0cf7649..9818cc0 100644 --- a/flixkit.go +++ b/flixkit.go @@ -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 diff --git a/generator/flixkitv1_0_0/generator.go b/generator/flixkitv1_0_0/generator.go index 38e0d31..2708225 100644 --- a/generator/flixkitv1_0_0/generator.go +++ b/generator/flixkitv1_0_0/generator.go @@ -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)