From 9d2790104310eb4145d7633fe759d4307bac39c4 Mon Sep 17 00:00:00 2001 From: Tom Haile Date: Wed, 4 Oct 2023 09:50:00 -0500 Subject: [PATCH] move js generator to bindings module and rename js generator to fcl js generator --- .gitignore | 3 - javascript.go => bindings/fcl-js.go | 15 +-- javascript_test.go => bindings/fcl-js_test.go | 95 ++++++++++--------- .../templates}/js_fcl_main.tmpl | 0 .../templates}/js_fcl_script.tmpl | 0 .../templates}/js_fcl_tx.tmpl | 0 .../testdata}/TestJSGenArrayScript.golden | 0 .../testdata}/TestJSGenMinScript.golden | 0 .../testdata}/TestJSGenScript.golden | 0 .../testdata}/TestJSGenTransaction.golden | 0 flixkit_test.go | 41 -------- testdata/TestGenFlixJS.golden | 32 ------- testdata/TestGenRemoteFlixJS.golden | 32 ------- types.go | 2 +- 14 files changed, 57 insertions(+), 163 deletions(-) rename javascript.go => bindings/fcl-js.go (85%) rename javascript_test.go => bindings/fcl-js_test.go (73%) rename {templates => bindings/templates}/js_fcl_main.tmpl (100%) rename {templates => bindings/templates}/js_fcl_script.tmpl (100%) rename {templates => bindings/templates}/js_fcl_tx.tmpl (100%) rename {testdata => bindings/testdata}/TestJSGenArrayScript.golden (100%) rename {testdata => bindings/testdata}/TestJSGenMinScript.golden (100%) rename {testdata => bindings/testdata}/TestJSGenScript.golden (100%) rename {testdata => bindings/testdata}/TestJSGenTransaction.golden (100%) delete mode 100644 testdata/TestGenFlixJS.golden delete mode 100644 testdata/TestGenRemoteFlixJS.golden diff --git a/.gitignore b/.gitignore index a825806..4c60b9d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,2 @@ -# Ignore cmd directory -cmd/ - # ignore all .DS_Store files .DS_Store diff --git a/javascript.go b/bindings/fcl-js.go similarity index 85% rename from javascript.go rename to bindings/fcl-js.go index fb30ba7..364d8c3 100644 --- a/javascript.go +++ b/bindings/fcl-js.go @@ -1,4 +1,4 @@ -package flixkit +package bindings import ( "bytes" @@ -6,6 +6,7 @@ import ( "sort" "text/template" + "github.com/onflow/flixkit-go" "github.com/stoewer/go-strcase" ) @@ -30,15 +31,15 @@ type TemplateData struct { //go:embed templates/*.tmpl var templateFiles embed.FS -type JavascriptGenerator struct{} +type FclJSGenerator struct{} -func (g JavascriptGenerator) Generate(flix *FlowInteractionTemplate, templateLocation string, isLocal bool) (string, error) { +func (g FclJSGenerator) Generate(flix *flixkit.FlowInteractionTemplate, templateLocation string, isLocal bool) (string, error) { tmpl, err := template.ParseFS(templateFiles, "templates/*.tmpl") if err != nil { return "", err } - methodName := strcase.LowerCamelCase(flix.Data.Messages.getTitleValue("Request")) + methodName := strcase.LowerCamelCase(flix.Data.Messages.GetTitleValue("Request")) description := flix.GetDescription() data := TemplateData{ Version: flix.FVersion, @@ -55,7 +56,7 @@ func (g JavascriptGenerator) Generate(flix *FlowInteractionTemplate, templateLoc return buf.String(), err } -func transformArguments(args Arguments) []SimpleParameter { +func transformArguments(args flixkit.Arguments) []SimpleParameter { simpleArgs := []SimpleParameter{} var keys []string // get keys for sorting @@ -69,7 +70,7 @@ func transformArguments(args Arguments) []SimpleParameter { for _, key := range keys { arg := args[key] isArray, cType, jsType := isArrayParameter(arg) - desciption := arg.Messages.getTitleValue("") + desciption := arg.Messages.GetTitleValue("") if isArray { simpleArgs = append(simpleArgs, SimpleParameter{Name: key, CadType: cType, JsType: jsType, FclType: "Array(t." + cType + ")", Description: desciption}) } else { @@ -81,7 +82,7 @@ func transformArguments(args Arguments) []SimpleParameter { } -func isArrayParameter(arg Argument) (bool, string, string) { +func isArrayParameter(arg flixkit.Argument) (bool, string, string) { if arg.Type == "" || arg.Type[0] != '[' { return false, "", "" } diff --git a/javascript_test.go b/bindings/fcl-js_test.go similarity index 73% rename from javascript_test.go rename to bindings/fcl-js_test.go index 70fc233..d4d1f66 100644 --- a/javascript_test.go +++ b/bindings/fcl-js_test.go @@ -1,44 +1,45 @@ -package flixkit +package bindings import ( "testing" "github.com/hexops/autogold/v2" + "github.com/onflow/flixkit-go" "github.com/stretchr/testify/assert" ) -var parsedTemplateTX = &FlowInteractionTemplate{ +var parsedTemplateTX = &flixkit.FlowInteractionTemplate{ FType: "InteractionTemplate", FVersion: "1.0.0", ID: "290b6b6222b2a77b16db896a80ddf29ebd1fa3038c9e6625a933fa213fce51fa", - Data: Data{ + Data: flixkit.Data{ Type: "transaction", Interface: "", - Messages: Messages{ - Title: &Title{ + Messages: flixkit.Messages{ + Title: &flixkit.Title{ I18N: map[string]string{ "en-US": "Transfer Tokens", }, }, - Description: &Description{ + Description: &flixkit.Description{ I18N: map[string]string{ "en-US": "Transfer tokens from one account to another", }, }, }, Cadence: "import FungibleToken from 0xFUNGIBLETOKENADDRESS\ntransaction(amount: UFix64, to: Address) {\nlet vault: @FungibleToken.Vault\nprepare(signer: AuthAccount) {\nself.vault <- signer\n.borrow<&{FungibleToken.Provider}>(from: /storage/flowTokenVault)!\n.withdraw(amount: amount)\n}\nexecute {\ngetAccount(to)\n.getCapability(/public/flowTokenReceiver)!\n.borrow<&{FungibleToken.Receiver}>()!\n.deposit(from: <-self.vault)\n}\n}", - Dependencies: Dependencies{ - "0xFUNGIBLETOKENADDRESS": Contracts{ - "FungibleToken": Networks{ - "mainnet": Network{ + Dependencies: flixkit.Dependencies{ + "0xFUNGIBLETOKENADDRESS": flixkit.Contracts{ + "FungibleToken": flixkit.Networks{ + "mainnet": flixkit.Network{ Address: "0xf233dcee88fe0abe", FqAddress: "A.0xf233dcee88fe0abe.FungibleToken", Contract: "FungibleToken", Pin: "83c9e3d61d3b5ebf24356a9f17b5b57b12d6d56547abc73e05f820a0ae7d9cf5", PinBlockHeight: 34166296, }, - "testnet": Network{ + "testnet": flixkit.Network{ Address: "0x9a0766d93b6608b7", FqAddress: "A.0x9a0766d93b6608b7.FungibleToken", Contract: "FungibleToken", @@ -48,12 +49,12 @@ var parsedTemplateTX = &FlowInteractionTemplate{ }, }, }, - Arguments: Arguments{ - "amount": Argument{ + Arguments: flixkit.Arguments{ + "amount": flixkit.Argument{ Index: 0, Type: "UFix64", - Messages: Messages{ - Title: &Title{ + Messages: flixkit.Messages{ + Title: &flixkit.Title{ I18N: map[string]string{ "en-US": "The amount of FLOW tokens to send", }, @@ -61,11 +62,11 @@ var parsedTemplateTX = &FlowInteractionTemplate{ }, Balance: "", }, - "to": Argument{ + "to": flixkit.Argument{ Index: 1, Type: "Address", - Messages: Messages{ - Title: &Title{ + Messages: flixkit.Messages{ + Title: &flixkit.Title{ I18N: map[string]string{ "en-US": "The Flow account the tokens will go to", }, @@ -78,32 +79,32 @@ var parsedTemplateTX = &FlowInteractionTemplate{ } -var parsedTemplateScript = &FlowInteractionTemplate{ +var parsedTemplateScript = &flixkit.FlowInteractionTemplate{ FType: "InteractionTemplate", FVersion: "1.0.0", ID: "290b6b6222b2a77b16db896a80ddf29ebd1fa3038c9e6625a933fa213fce51fa", - Data: Data{ + Data: flixkit.Data{ Type: "script", Interface: "", - Messages: Messages{ - Title: &Title{ + Messages: flixkit.Messages{ + Title: &flixkit.Title{ I18N: map[string]string{ "en-US": "Multiply Two Integers", }, }, - Description: &Description{ + Description: &flixkit.Description{ I18N: map[string]string{ "en-US": "Multiply two numbers to another", }, }, }, Cadence: "pub fun main(x: Int, y: Int): Int { return x * y }", - Arguments: Arguments{ - "x": Argument{ + Arguments: flixkit.Arguments{ + "x": flixkit.Argument{ Index: 0, Type: "Int", - Messages: Messages{ - Title: &Title{ + Messages: flixkit.Messages{ + Title: &flixkit.Title{ I18N: map[string]string{ "en-US": "number to be multiplied", }, @@ -111,11 +112,11 @@ var parsedTemplateScript = &FlowInteractionTemplate{ }, Balance: "", }, - "y": Argument{ + "y": flixkit.Argument{ Index: 1, Type: "Int", - Messages: Messages{ - Title: &Title{ + Messages: flixkit.Messages{ + Title: &flixkit.Title{ I18N: map[string]string{ "en-US": "second number to be multiplied", }, @@ -128,32 +129,32 @@ var parsedTemplateScript = &FlowInteractionTemplate{ } -var ArrayTypeScript = &FlowInteractionTemplate{ +var ArrayTypeScript = &flixkit.FlowInteractionTemplate{ FType: "InteractionTemplate", FVersion: "1.0.0", ID: "290b6b6222b2a77b16db896a80ddf29ebd1fa3038c9e6625a933fa213fce51fa", - Data: Data{ + Data: flixkit.Data{ Type: "script", Interface: "", - Messages: Messages{ - Title: &Title{ + Messages: flixkit.Messages{ + Title: &flixkit.Title{ I18N: map[string]string{ "en-US": "Multiply Numbers", }, }, - Description: &Description{ + Description: &flixkit.Description{ I18N: map[string]string{ "en-US": "Multiply numbers in an array", }, }, }, Cadence: "pub fun main(numbers: [Int]): Int { var total = 1; for x in numbers { total = total * x }; return total }", - Arguments: Arguments{ - "numbers": Argument{ + Arguments: flixkit.Arguments{ + "numbers": flixkit.Argument{ Index: 0, Type: "[Int]", - Messages: Messages{ - Title: &Title{ + Messages: flixkit.Messages{ + Title: &flixkit.Title{ I18N: map[string]string{ "en-US": "Array of numbers to be multiplied", }, @@ -165,16 +166,16 @@ var ArrayTypeScript = &FlowInteractionTemplate{ }, } -var minimumTemplate = &FlowInteractionTemplate{ +var minimumTemplate = &flixkit.FlowInteractionTemplate{ FType: "InteractionTemplate", FVersion: "1.0.0", ID: "290b6b6222b2a77b16db896a80ddf29ebd1fa3038c9e6625a933fa213fce51fa", - Data: Data{ + Data: flixkit.Data{ Type: "script", Interface: "", Cadence: "pub fun main(numbers: [Int]): Int { var total = 1; for x in numbers { total = total * x }; return total }", - Arguments: Arguments{ - "numbers": Argument{ + Arguments: flixkit.Arguments{ + "numbers": flixkit.Argument{ Index: 0, Type: "[Int]", }, @@ -182,13 +183,13 @@ var minimumTemplate = &FlowInteractionTemplate{ }, } func TestJSGenTransaction(t *testing.T) { - generator := JavascriptGenerator{} + generator := FclJSGenerator{} got, _ := generator.Generate(parsedTemplateTX, "./transfer_token.json", true) autogold.ExpectFile(t, got) } func TestJSGenScript(t *testing.T) { - generator := JavascriptGenerator{} + generator := FclJSGenerator{} assert := assert.New(t) got, err:= generator.Generate(parsedTemplateScript, "./multiply_two_integers.template.json", true) assert.NoError(err, "ParseTemplate should not return an error") @@ -196,7 +197,7 @@ func TestJSGenScript(t *testing.T) { } func TestJSGenArrayScript(t *testing.T) { - generator := JavascriptGenerator{} + generator := FclJSGenerator{} assert := assert.New(t) out, err := generator.Generate(ArrayTypeScript, "./multiply-numbers.template.json", true) @@ -205,7 +206,7 @@ func TestJSGenArrayScript(t *testing.T) { } func TestJSGenMinScript(t *testing.T) { - generator := JavascriptGenerator{} + generator := FclJSGenerator{} assert := assert.New(t) out, err := generator.Generate(minimumTemplate, "./min.template.json", true) diff --git a/templates/js_fcl_main.tmpl b/bindings/templates/js_fcl_main.tmpl similarity index 100% rename from templates/js_fcl_main.tmpl rename to bindings/templates/js_fcl_main.tmpl diff --git a/templates/js_fcl_script.tmpl b/bindings/templates/js_fcl_script.tmpl similarity index 100% rename from templates/js_fcl_script.tmpl rename to bindings/templates/js_fcl_script.tmpl diff --git a/templates/js_fcl_tx.tmpl b/bindings/templates/js_fcl_tx.tmpl similarity index 100% rename from templates/js_fcl_tx.tmpl rename to bindings/templates/js_fcl_tx.tmpl diff --git a/testdata/TestJSGenArrayScript.golden b/bindings/testdata/TestJSGenArrayScript.golden similarity index 100% rename from testdata/TestJSGenArrayScript.golden rename to bindings/testdata/TestJSGenArrayScript.golden diff --git a/testdata/TestJSGenMinScript.golden b/bindings/testdata/TestJSGenMinScript.golden similarity index 100% rename from testdata/TestJSGenMinScript.golden rename to bindings/testdata/TestJSGenMinScript.golden diff --git a/testdata/TestJSGenScript.golden b/bindings/testdata/TestJSGenScript.golden similarity index 100% rename from testdata/TestJSGenScript.golden rename to bindings/testdata/TestJSGenScript.golden diff --git a/testdata/TestJSGenTransaction.golden b/bindings/testdata/TestJSGenTransaction.golden similarity index 100% rename from testdata/TestJSGenTransaction.golden rename to bindings/testdata/TestJSGenTransaction.golden diff --git a/flixkit_test.go b/flixkit_test.go index e879d14..7f45c18 100644 --- a/flixkit_test.go +++ b/flixkit_test.go @@ -3,12 +3,10 @@ package flixkit import ( "context" "io/fs" - "net" "net/http" "net/http/httptest" "testing" - "github.com/hexops/autogold/v2" "github.com/stretchr/testify/assert" ) @@ -321,42 +319,3 @@ func TestGetFlixByID(t *testing.T) { type MapFsReader struct { FS fs.FS } - -func TestGenFlixJS(t *testing.T) { - assert := assert.New(t) - - server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { - rw.Write([]byte(flix_template)) - })) - defer server.Close() - templatePath := "templateID" - - out, err := JavascriptGenerator.Generate(JavascriptGenerator{}, parsedTemplate, templatePath, true) - autogold.ExpectFile(t, out) - assert.NoError(err, "GenFlixBinding should not return an error") -} - -func TestGenRemoteFlixJS(t *testing.T) { - assert := assert.New(t) - - handler := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { - rw.Write([]byte(flix_template)) - }) - - server := httptest.NewUnstartedServer(handler) - - // Close the default listener and set a new one with the desired port - l, err := net.Listen("tcp", ":52718") - if err != nil { - t.Fatalf("Failed to listen on port 52718: %v", err) - } - server.Listener = l - - server.Start() - defer server.Close() - - endpoint := server.URL + "/tempateName" - out, err := JavascriptGenerator.Generate(JavascriptGenerator{}, parsedTemplate, endpoint, false) - assert.NoError(err, "GenFlixBinding should not return an error") - autogold.ExpectFile(t, out) -} \ No newline at end of file diff --git a/testdata/TestGenFlixJS.golden b/testdata/TestGenFlixJS.golden deleted file mode 100644 index f009058..0000000 --- a/testdata/TestGenFlixJS.golden +++ /dev/null @@ -1,32 +0,0 @@ -`/** - This binding file was auto generated based on FLIX template v1.0.0. - Changes to this file might get overwritten -**/ - -import * as fcl from "@onflow/fcl" -import flixTemplate from "templateID" - -// TODO: add runtime check for parameters consistency -const parameterNames = ["amount", "to"]; - -/** -* Transfer tokens from one account to another -* @param {Object} Parameters - parameters for the cadence -* @param { string } Parameters.amount - The amount of FLOW tokens to send: UFix64 -* @param { string } Parameters.to - The Flow account the tokens will go to: Address -* @returns {Promise} - returns a promise which resolves to the transaction id -*/ - -export async function transferTokens({amount, to}) { - const transactionId = await fcl.mutate({ - template: flixTemplate, - args: (arg, t) => [arg(amount, t.UFix64), arg(to, t.Address)] - }); - - return transactionId -} - - - - -` diff --git a/testdata/TestGenRemoteFlixJS.golden b/testdata/TestGenRemoteFlixJS.golden deleted file mode 100644 index 554bf5d..0000000 --- a/testdata/TestGenRemoteFlixJS.golden +++ /dev/null @@ -1,32 +0,0 @@ -`/** - This binding file was auto generated based on FLIX template v1.0.0. - Changes to this file might get overwritten -**/ - -import * as fcl from "@onflow/fcl" -const flixTemplate = "http://[::]:52718/tempateName" - -// TODO: add runtime check for parameters consistency -const parameterNames = ["amount", "to"]; - -/** -* Transfer tokens from one account to another -* @param {Object} Parameters - parameters for the cadence -* @param { string } Parameters.amount - The amount of FLOW tokens to send: UFix64 -* @param { string } Parameters.to - The Flow account the tokens will go to: Address -* @returns {Promise} - returns a promise which resolves to the transaction id -*/ - -export async function transferTokens({amount, to}) { - const transactionId = await fcl.mutate({ - template: flixTemplate, - args: (arg, t) => [arg(amount, t.UFix64), arg(to, t.Address)] - }); - - return transactionId -} - - - - -` diff --git a/types.go b/types.go index 2028220..9561edb 100644 --- a/types.go +++ b/types.go @@ -103,7 +103,7 @@ func (t *FlowInteractionTemplate) GetDescription() string { } -func (msgs *Messages)getTitleValue(placeholder string) string { +func (msgs *Messages) GetTitleValue(placeholder string) string { s := placeholder if msgs.Title != nil && msgs.Title.I18N != nil {