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

Add support for JS code generation for flix #1222

merged 14 commits into from
Oct 30, 2023

Conversation

bthaile
Copy link
Contributor

@bthaile bthaile commented Sep 29, 2023

closes: #1232

Depends on flixkit PR onflow/flixkit-go#12

js templates will be on going iterative process in flixkit-go repo.

Description

to follow the same pattern as transactions and account, using ...

command help when running ./flow flix

execute , bindings

Usage:
  flow flix [command]

Available Commands:
  bindings    generate binding file for FLIX template fcl-js is default
  execute     execute FLIX template with a given id, name, or local filename

// Note for the future
There will be a new verb generate that will generate FLIX, so I'm reserving that word.

To Test

executecommand

  • flow flix execute transfer-flow 1 0x123456789
    bindings command
  • flow flix bindings multiply.template.json

download this file to test local js generation,
https://github.com/onflow/flow-interaction-template-service/blob/master/templates/test/multiply.template.json

flow flix bindings multiply.template.json

/**
    This binding file was auto generated based on FLIX template. 
    Changes to this file might get overwritten 
**/

import * as fcl from "@onflow/fcl"
const flixTemplate = "https://flix.flow.com/v1/templates?name=transfer-flow"

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<Object>} - 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
}
/**
    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 "./multiply_two_integers.template.json"
 
/**
* Multiply two numbers to another
* @param {Object} Parameters - parameters for the cadence
* @param { string } Parameters.x - number to be multiplied: Int
* @param { string } Parameters.y - second number to be multiplied: Int
*/
export async function multiplyTwoIntegers({x, y}) {
  const info = await fcl.query({
    template: flixTemplate,
    args: (arg, t) => [arg(x, t.Int), arg(y, t.Int)]
  });

  return info
}

script example


For contributor use:

  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work
  • Code follows the standards mentioned here
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

Copy link
Member

@chasefleming chasefleming left a comment

Choose a reason for hiding this comment

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

I think the generate flag type needs to be fcl, not js since this is specifically for fcl and there could be other js libs.

Can you also link to a larger issue (probably on flixkit) which shows what the expected output is and describes the use case please?


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.

@chasefleming
Copy link
Member

chasefleming commented Oct 3, 2023

Can you also create a CLI issue as well please and link it? Thanks.

@bthaile
Copy link
Contributor Author

bthaile commented Oct 16, 2023

Can you also create a CLI issue as well please and link it? Thanks.

added #1232 and referenced it in the PR description

@bthaile bthaile marked this pull request as ready for review October 24, 2023 15:44
@chasefleming chasefleming changed the title add support for js code generation for flix Add support for JS code generation for flix Oct 24, 2023
Copy link
Member

@chasefleming chasefleming left a comment

Choose a reason for hiding this comment

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

@bthaile I'm getting unknown flag: --generate when running go run cmd/flow/main.go flix multiply.template.json --generate fcl-js --output multiply.template.js --network testnet.

I'm also wondering if commands like generate should be as flags --generate. Those are for things like args. generate feels like a command say flow flix generate... or flow flix execute.... Now that we are getting lots of flix items it feel like they should be subcommands otherwise you can run flow flix --bindings --execute --generate and what is it supposed to do?

internal/super/flix.go Outdated Show resolved Hide resolved
@jribbink
Copy link
Contributor

I'm also wondering if commands like generate should be as flags --generate. Those are for things like args. generate feels like a command say flow flix generate... or flow flix execute.... Now that we are getting lots of flix items it feel like they should be subcommands otherwise you can run flow flix --bindings --execute --generate and what is it supposed to do?

+1 on this point. In my opinion, these are distinct commands without significant functionality overlap, so they should probably be namespaced accordingly instead of distinguishing using flags.

internal/super/flix.go Outdated Show resolved Hide resolved
@bjartek
Copy link
Collaborator

bjartek commented Oct 24, 2023

@bthaile I'm getting unknown flag: --generate when running go run cmd/flow/main.go flix multiply.template.json --generate fcl-js --output multiply.template.js --network testnet.

I'm also wondering if commands like generate should be as flags --generate. Those are for things like args. generate feels like a command say flow flix generate... or flow flix execute.... Now that we are getting lots of flix items it feel like they should be subcommands otherwise you can run flow flix --bindings --execute --generate and what is it supposed to do?

generate should be a subcommand of flix, not a flag.

internal/super/flix.go Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Oct 25, 2023

Codecov Report

Attention: 53 lines in your changes are missing coverage. Please review.

Comparison is base (c2d0040) 38.65% compared to head (9c2ad0a) 38.07%.
Report is 5 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1222      +/-   ##
==========================================
- Coverage   38.65%   38.07%   -0.59%     
==========================================
  Files          38       38              
  Lines        1948     1983      +35     
==========================================
+ Hits          753      755       +2     
- Misses       1107     1140      +33     
  Partials       88       88              
Flag Coverage Δ
unittests 38.07% <3.63%> (-0.59%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
internal/super/flix.go 2.19% <3.63%> (+2.19%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bthaile
Copy link
Contributor Author

bthaile commented Oct 25, 2023

@bthaile I'm getting unknown flag: --generate when running go run cmd/flow/main.go flix multiply.template.json --generate fcl-js --output multiply.template.js --network testnet.

I'm also wondering if commands like generate should be as flags --generate. Those are for things like args. generate feels like a command say flow flix generate... or flow flix execute.... Now that we are getting lots of flix items it feel like they should be subcommands otherwise you can run flow flix --bindings --execute --generate and what is it supposed to do?

Updated description and PR. It makes more sense to break out verbs than add flags.

@bthaile
Copy link
Contributor Author

bthaile commented Oct 25, 2023

@bjartek removed local --save flag and using global save flag. Added more structure to json output.

@chasefleming
Copy link
Member

Cool @bthaile can you make an issue to remember to update the docs with this new syntax? We'll also need to add the bindings stuff to the docs.

@bthaile
Copy link
Contributor Author

bthaile commented Oct 25, 2023

Cool @bthaile can you make an issue to remember to update the docs with this new syntax? We'll also need to add the bindings stuff to the docs.

good call, Added docs issue. I can write it up when this PR gets merged up and released.
onflow/docs#419

@chasefleming
Copy link
Member

@bthaile There isn't much to test here but as a best practice does it make sense to at least add a test file maybe mocking the library was called so that its in place for whoever touches the code next? Apologies I should have added that when I originally put in the execute code originally. I don't think it matters that much right now since most of the logic is in the flixkit library, but if you are feeling like it wouldn't hurt.

@bthaile
Copy link
Contributor Author

bthaile commented Oct 26, 2023

@bthaile There isn't much to test here but as a best practice does it make sense to at least add a test file maybe mocking the library was called so that its in place for whoever touches the code next? Apologies I should have added that when I originally put in the execute code originally. I don't think it matters that much right now since most of the logic is in the flixkit library, but if you are feeling like it wouldn't hurt.

I don't think it's needed at this time. I could add tests when adding flix generate.

@chasefleming
Copy link
Member

Sounds good fine with that. Is this ready for me to manually test it again?

internal/super/flix.go Outdated Show resolved Hide resolved
chasefleming

This comment was marked as resolved.

@bjartek
Copy link
Collaborator

bjartek commented Oct 27, 2023

One thing i was thinking about here is that the usecase we talked about in the developer office hours yesterday.

  • could a client be generated for more then 1 flix?
  • should a client have a name or other information. Like if we generate a go file it needs a package. If we generate jvm it needs a artifactId and a name

@chasefleming
Copy link
Member

True @bjartek would be nice if it could grab a whole directory

@bjartek
Copy link
Collaborator

bjartek commented Oct 27, 2023

Not sure if it belongs in rhis issue bur a flix.json file that had lots of options could work. Let say you want to upload to a service, generate 3 different clients and maybe put those generated files somewhere. Describing all that with flags will be very complex.

@bthaile
Copy link
Contributor Author

bthaile commented Oct 30, 2023

One thing i was thinking about here is that the usecase we talked about in the developer office hours yesterday.

  • could a client be generated for more then 1 flix?
  • should a client have a name or other information. Like if we generate a go file it needs a package. If we generate jvm it needs a artifactId and a name

very simple for a developer to write a script (bash, nodejs, ...) to generate package files per flix. no need to add that complexity to flow-cli.
when we get to golang generation we can figure out module name to add, don't need for js, imo. and dev can call the filename what ever. I really want to keep this simple, more features isn't better. imo

@bthaile bthaile merged commit 670fac7 into master Oct 30, 2023
7 checks passed
@bthaile bthaile deleted the add-flix-gen branch October 30, 2023 17:09
@chasefleming chasefleming added the Feature A new user feature or a new package API label Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature A new user feature or a new package API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support FLIX code generation
5 participants