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
Prev Previous commit
Next Next commit
dry up code
  • Loading branch information
bthaile committed Oct 25, 2023
commit 9c2ad0a0624749e6c115788cb8542209c0eeb34f
99 changes: 43 additions & 56 deletions internal/super/flix.go
Original file line number Diff line number Diff line change
@@ -130,35 +130,10 @@ func executeCmd(
state *flowkit.State,
) (result command.Result, err error) {
flixService := flixkit.NewFlixService(&flixkit.Config{})
ctx := context.Background()
var template *flixkit.FlowInteractionTemplate
flixQuery := args[0]

switch getType(flixQuery) {
case flixId:
template, err = flixService.GetFlixByID(ctx, flixQuery)
if err != nil {
return nil, fmt.Errorf("could not find flix with id %s: %w", flixQuery, err)
}

case flixName:
template, err = flixService.GetFlix(ctx, flixQuery)
if err != nil {
return nil, fmt.Errorf("could not find flix with name %s: %w", flixQuery, err)
}

case flixPath:
file, err := os.ReadFile(flixQuery)
if err != nil {
return nil, fmt.Errorf("could not read flix file %s: %w", flixQuery, err)
}
template, err = flixkit.ParseFlix(string(file))
if err != nil {
return nil, fmt.Errorf("could not parse flix from file %s: %w", flixQuery, err)
}

default:
return nil, fmt.Errorf("invalid flix query type: %s", flixQuery)
template, err := getTemplate(state, flixService, flixQuery)
if err != nil {
return nil, err
}

cadenceWithImportsReplaced, err := template.GetAndReplaceCadenceImports(flow.Network().Name)
@@ -197,45 +172,24 @@ func bindingsCmd(
state *flowkit.State,
) (result command.Result, err error) {
flixService := flixkit.NewFlixService(&flixkit.Config{})
ctx := context.Background()
var template *flixkit.FlowInteractionTemplate
flixQuery := args[0]
isLocal := false

switch getType(flixQuery) {
case flixId:
template, err = flixService.GetFlixByID(ctx, flixQuery)
if err != nil {
return nil, fmt.Errorf("could not find flix with id %s: %w", flixQuery, err)
}

case flixName:
template, err = flixService.GetFlix(ctx, flixQuery)
if err != nil {
return nil, fmt.Errorf("could not find flix with name %s: %w", flixQuery, err)
}
template, err := getTemplate(state, flixService, flixQuery)
if err != nil {
return nil, err
}

case flixPath:
isLocal := false
if getType(flixQuery) == flixPath {
isLocal = true
file, err := os.ReadFile(flixQuery)
if err != nil {
return nil, fmt.Errorf("could not read flix file %s: %w", flixQuery, err)
}
template, err = flixkit.ParseFlix(string(file))
if err != nil {
return nil, fmt.Errorf("could not parse flix from file %s: %w", flixQuery, err)
}

default:
return nil, fmt.Errorf("invalid flix query type: %s", flixQuery)
}

fclJsGen := bindings.NewFclJSGenerator()

out, err := fclJsGen.Generate(template, flixQuery, isLocal)

if flags.Save != "" {
err = os.WriteFile(flags.Save, []byte(out), 0644)
err = state.ReaderWriter().WriteFile(flags.Save, []byte(out), 0644)
if err != nil {
return nil, fmt.Errorf("could not write to file %s: %w", flags.Save, err)
}
@@ -267,3 +221,36 @@ func isSaved(fr *flixResult) string {
}
return fr.result
}

func getTemplate(state *flowkit.State, flixService flixkit.FlixService, flixQuery string) (*flixkit.FlowInteractionTemplate, error) {
var template *flixkit.FlowInteractionTemplate
var err error
ctx := context.Background()
switch getType(flixQuery) {
case flixId:
template, err = flixService.GetFlixByID(ctx, flixQuery)
if err != nil {
return nil, fmt.Errorf("could not find flix with id %s: %w", flixQuery, err)
}

case flixName:
template, err = flixService.GetFlix(ctx, flixQuery)
if err != nil {
return nil, fmt.Errorf("could not find flix with name %s: %w", flixQuery, err)
}

case flixPath:
file, err := state.ReadFile(flixQuery)
if err != nil {
return nil, fmt.Errorf("could not read flix file %s: %w", flixQuery, err)
}
template, err = flixkit.ParseFlix(string(file))
if err != nil {
return nil, fmt.Errorf("could not parse flix from file %s: %w", flixQuery, err)
}

default:
return nil, fmt.Errorf("invalid flix query type: %s", flixQuery)
}
return template, nil
}