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

Auto deserialize data #445

Open
Tracked by #418
jawoznia opened this issue Oct 15, 2024 · 0 comments · May be fixed by #448
Open
Tracked by #418

Auto deserialize data #445

jawoznia opened this issue Oct 15, 2024 · 0 comments · May be fixed by #448

Comments

@jawoznia
Copy link
Collaborator

jawoznia commented Oct 15, 2024

As in case of payload we can provide a way to auto deserialize the payload in the dispatch so user won't have to.

Similary to payload we would mark the data with an attribute.

#[sv::msg(reply)]
fn on_something_happened(
    &self,
    ctx: ReplyCtx,
    #[sv::data(raw, opt)] data: Option<Binary>,
    #[sv::payload(raw)] payload: Binary,
) -> Result<Response, ContractError> {
}

The difference in this approach between handling data and payload would be the ability to deserialize the payload into separate fields like:

#[sv::msg(reply)]
fn on_something_happened(
    &self,
    ctx: ReplyCtx,
    #[sv::data(raw, opt)] data: Option<Binary>,
    coins: Coins, // First parameter deserialized from the `payload`
    receiver: String, // Second parameter deserialized from the `payload`
) -> Result<Response, ContractError> {
}

This is because #[sv::payload] can be omitted. In case of the #[sv::data] it would require the user to either mark every attribute with the exact parameters in the sv::data attribute.

#[sv::data()]

Marks field to which data should be forwarded.

No #[sv::data]

The data not forwarded.

If no #[sv::data] attribute found in the method signature, Sylvia won't forward the data to the method so the data field should not be present at all.

#[sv::data(raw)]

Fails if the data is None. Extracts and forwards data: Binary if it's Some.

#[sv::data(raw, opt)]

Forwards data: Option<Binary>.

#[sv::data(opt)]

Expects data in the method signature to be data: Option<T> where T: Deserialize
Tries to deserialize data to type defined in the method signature and forwards it wrapped in the Option.

If data is:

  • None - forwards None
  • Some(valid) - forwards Some(valid)
  • Some(invalid) - early returns error specifying what went wrong with serde error attached.

#[sv::data]

Expects data in the method signature to be data: T where T: Deserialize
Tries to deserialize data to type defined in the method signature and forwards it.

If data is:

  • None - early returns error specifying the data is missing.
  • Some(valid) - forwards Some(valid)
  • Some(invalid) - early returns error specifying what went wrong with serde error attached.

Optional attribute variants for future development

#[sv::data(fallible)]

Expects data in the method signature to be data: Result<T, sylvia::DeserializeError> where T: Deserialize.

The sylvia::DeserializeError would have to implement From:

  • StdError
  • serde::Error
  • custom contract error - this might be tricky. Instead author of the contract might have to implement From on those types on their custom error.

Tries to deserialize data to type defined in the method signature and forwards it.

If data is:

  • None - early returns error specifying the data is missing.
  • Some(valid) - forwards Ok(valid)
  • Some(invalid) - forwards Err(serde::Error)

#[sv::data(instantiate)]

Special case in which parse_instantiate_response_data would be called underneath and passed to the method.

This should be handled the similar to #[sv::data].

#[sv::data(instantiate, opt)]

As in case of lone instantiate, but handled similar to #[sv::data(opt)].

#[sv::payload]

To make the API concise we would change it's usage in case we don't want to deserialize the payload to be: #[sv::payload(raw)].

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 a pull request may close this issue.

1 participant