temporalio / sdk-core

Core Temporal SDK that can be used as a base for language specific Temporal SDKs
MIT License
262 stars 70 forks source link

sdk: allow types to abstract over encoding #718

Closed djc closed 1 week ago

djc commented 4 months ago

What was changed

So far, the sdk and sdk-core-protos crates have specialized JSON encoding of payloads with the AsJsonPayloadExt and FromJsonPayloadExt traits. Some implementations in the Rust SDK have effectively encoded that in the API so that it is harder or non-intuitive to use any other encoding. This PR interposes Encoder and Decoder traits so each type that must be converted into/from a Payload has to explicitly opt in to this behavior, and gets to chose which encoding is used. A Json encoding is provided that makes it easy to regain the previous behavior of encoding as JSON.

Why?

The Temporal infrastructure/gRPC protocol allows encoding in a number of different ways and this is exposed to the other SDKs. However, the Rust SDK so far has facilitated JSON to the point where it is hard to use anything else. Given Rust's type system, we can do better and make it statically required for types to opt in to a particular encoding, and for the SDK to rely on these implementations to encode to and decode from the Payload type.

This will also help users of the Rust SDK to implement a type system boundary that makes sure any types used in Temporal workflows/activities must be encrypted, because opting into an encoding is required.

Some previous discussion in this Slack thread: https://temporalio.slack.com/archives/C02MTL0GEKH/p1708568506832879.

Checklist

  1. How was this tested: it compiles and passes the tests
  2. Any docs updates needed? I don't think so?
djc commented 4 months ago

(The previous run of the integration tests seemed to pass, and they passed locally, too.)

djc commented 1 week ago

I'm leaving the company where I was working with Temporal, so I probably won't be pushing this forward anymore. Feel free to reuse any of the code here for future changes.