Skip to content
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 support for JS code generation for flix #1222

Merged
merged 14 commits into from
Oct 30, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions internal/super/flix.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@

"github.com/onflow/flixkit-go"

"github.com/onflow/flow-cli/flowkit"
"github.com/onflow/flow-cli/flowkit/output"
"github.com/onflow/flow-cli/internal/command"
"github.com/onflow/flow-cli/internal/scripts"
"github.com/onflow/flow-cli/internal/transactions"

"github.com/onflow/flow-cli/flowkit"

"github.com/spf13/cobra"
)

Expand All @@ -47,8 +46,15 @@
Include []string `default:"" flag:"include" info:"Fields to include in the output"`
Exclude []string `default:"" flag:"exclude" info:"Fields to exclude from the output (events)"`
GasLimit uint64 `default:"1000" flag:"gas-limit" info:"transaction gas limit"`
Generate string `default:"" flag:"generate" info:"generate code for the given language"`
Save string `default:"" flag:"output" info:"output file for generated code"`
}

type flixResult struct {
output string
save string
}

var flags = flixFlags{}

var FlixCommand = &command.Command{
Expand Down Expand Up @@ -106,6 +112,7 @@
ctx := context.Background()
var template *flixkit.FlowInteractionTemplate
flixQuery := args[0]
isLocal := false

switch getType(flixQuery) {
case flixId:
Expand All @@ -121,6 +128,7 @@
}

case flixPath:
isLocal = true
file, err := os.ReadFile(flixQuery)
bthaile marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, fmt.Errorf("could not read flix file %s: %w", flixQuery, err)
Expand All @@ -134,6 +142,22 @@
return nil, fmt.Errorf("invalid flix query type: %s", flixQuery)
}

if flags.Generate != "" {
jsGen := flixkit.JavascriptGenerator{}

Check failure on line 146 in internal/super/flix.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

undefined: flixkit.JavascriptGenerator
out, err := jsGen.Generate(template, flixQuery, isLocal)

if flags.Save != "" {
err = os.WriteFile(flags.Save, []byte(out), 0644)
if err != nil {
return nil, fmt.Errorf("could not write to file %s: %w", flags.Save, err)
}
}
return &flixResult{
save: flags.Save,
output: out,
}, err
}

cadenceWithImportsReplaced, err := template.GetAndReplaceCadenceImports(flow.Network().Name)
if err != nil {
logger.Error("could not replace imports")
Expand Down Expand Up @@ -161,3 +185,25 @@
}
return transactions.SendTransaction([]byte(cadenceWithImportsReplaced), args[1:], "", flow, state, transactionFlags)
}

func (fr *flixResult) JSON() any {
result := make(map[string]any)
result["output"] = fr.output
result["save"] = fr.save
return result
}

func (fr *flixResult) String() string {
return isSaved(fr)
}

func (fr *flixResult) Oneliner() string {
return isSaved(fr)
}

func isSaved(fr *flixResult) string {
if fr.save != "" {
return fmt.Sprintf("Generated code saved to %s", fr.save)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the logger instead of fmt

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the isSaved method returns a string. It's a convenience method for String() and Oneliner() methods. it's not a log out statement specifically.

}
return fr.output
}
Loading