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 type AnyStruct #1253

Conversation

ObjectPlayer
Copy link

@ObjectPlayer ObjectPlayer commented Jun 15, 2022

Hey Team,

This branch contains AnyStruct type support and having test-cases to test all non-resource types and values that are under the AnyStruct

This branch will resolve #1237

@changeset-bot
Copy link

changeset-bot bot commented Jun 15, 2022

🦋 Changeset detected

Latest commit: 4e07a86

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@onflow/types Minor
@onflow/fcl Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@justinbarry
Copy link
Contributor

Thank you @ObjectPlayer for your contribution! Would you be able to add a
changeset file for this? Just run npm run changeset at the root and follow the prompts using these conventions:

  • MAJOR version when you make incompatible API changes,
  • MINOR version when you add functionality in a backwards compatible manner, and
  • PATCH version when you make backwards compatible bug fixes.

@ObjectPlayer
Copy link
Author

Sure @justinbarry let me add changeset and update the PR

@ObjectPlayer ObjectPlayer requested a review from a team as a code owner June 16, 2022 11:55
@JeffreyDoyle
Copy link
Member

Hey @ObjectPlayer ! Thank you for taking the time to prepare this PR, we sincerely appreciate your time and effort to help improve FCL! 🚀

AnyStruct is not permissible to be passed as an argument to a transaction or script.

Think of AnyStruct as an “interface”: concrete values might be a subtype, but no concrete value ever has that type. Only concrete values can be passed to a transaction or script as an argument.

AnyStruct may be a parameter to a transaction or script:

Eg:

pub fun main(a: AnyStruct, b: AnyStruct) { ...

But the arguments passed for a or b must be a concrete type: eg: String, Int, UFix64 etc...

It is not possible to pass AnyStruct type as argument for a transaction or script.

In FCL, our types library aims to strictly support all types that are permissible as arguments to a transaction or script.

@ObjectPlayer
Copy link
Author

Hey @JeffreyDoyle ,

Thanks for your feedback. Yes I am agree with you that a or b must be a concrete type: eg: String, Int, UFix64 and that's how AnyStruct work, it only support the valid type, I think we can add validation.

I have a use case, where I have a dictionary which have key as String, but it values has dynamic type. It can be String, Int64, UFix64, Boolean etc as that dictionary has multiple values. Also there can be multiple values may be 20 to 30, therefore in my opinion we can create dynamic dictionary and pass that dictionary to any transaction and script as argument. Alternative is available that we can add separate values but I think it will be good if we can pass AnyStruct as argument.

You can say it is very hard to achieve this, but it is possible.

@JeffreyDoyle
Copy link
Member

Hey @ObjectPlayer,

It is not possible to submit arguments with AnyStruct as the type of the argument, as it is invalid in JSON-CDC https://docs.onflow.org/cadence/json-cadence-spec (which is how arguments are sent to Flow in a transaction or script). It's also not possible to have a key or value be type AnyStruct in a Dictionary or Composite type.

For your use case, you'll need to check the type of the argument that is to be included in the transaction or script that you're submitting, and then use the correct FCL type binding for it. Eg: Check the value is a string, and use the String type, or, if it is a fixed point number use UFix64, for example.

@turbolent
Copy link
Member

@ObjectPlayer For this functionality to be added to FCL, it first has to be supported by Cadence, which it isn't at the moment, see https://docs.onflow.org/cadence/json-cadence-spec/. All values need to specify their concrete type, Cadence does not, and will not be able to infer, the concrete type based on the value (a string) alone.

@ObjectPlayer
Copy link
Author

Thanks @turbolent , yeah it makes sense to me, If Cadence is not supporting this at the moment, than yes this functionality can't be added to FCL.

Thanks @JeffreyDoyle for the alternative solutions, we are already using this solution but IMO there should be a way to send dynamic type values by using AnyStruct.

@JeffreyDoyle
Copy link
Member

Hey @ObjectPlayer , I totally empathize with your need and can see why such a feature would be helpful! We'll take this feedback and be sure to think of any possible solutions going forward!

I'm going to close this PR as this functionality shouldn't be added to FCL as it is unsupported by cadence.

Thank you for taking the time to create this PR! We sincerely appreciate the time and effort it took, and look forward to any future contributions you may have!

Happy building! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for type AnyStruct
4 participants