Skip to content

Commit

Permalink
move js generator to bindings module and rename js generator to fcl j…
Browse files Browse the repository at this point in the history
…s generator
  • Loading branch information
bthaile committed Oct 4, 2023
1 parent aa66b9f commit 9d27901
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 163 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
# Ignore cmd directory
cmd/

# ignore all .DS_Store files
.DS_Store
15 changes: 8 additions & 7 deletions javascript.go → bindings/fcl-js.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package flixkit
package bindings

import (
"bytes"
"embed"
"sort"
"text/template"

"github.com/onflow/flixkit-go"
"github.com/stoewer/go-strcase"
)

Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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, "", ""
}
Expand Down
95 changes: 48 additions & 47 deletions javascript_test.go → bindings/fcl-js_test.go
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -48,24 +49,24 @@ 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",
},
},
},
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",
},
Expand All @@ -78,44 +79,44 @@ 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",
},
},
},
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",
},
Expand All @@ -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",
},
Expand All @@ -165,38 +166,38 @@ 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]",
},
},
},
}
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")
autogold.ExpectFile(t, got)
}

func TestJSGenArrayScript(t *testing.T) {
generator := JavascriptGenerator{}
generator := FclJSGenerator{}
assert := assert.New(t)

out, err := generator.Generate(ArrayTypeScript, "./multiply-numbers.template.json", true)
Expand All @@ -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)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
41 changes: 0 additions & 41 deletions flixkit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
32 changes: 0 additions & 32 deletions testdata/TestGenFlixJS.golden

This file was deleted.

Loading

0 comments on commit 9d27901

Please sign in to comment.