-
Notifications
You must be signed in to change notification settings - Fork 57
Add transfer type payload conversion hooks #795
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
Open
tconley1428
wants to merge
3
commits into
main
Choose a base branch
from
data-model-payload-hooks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/Temporalio/Converters/ITemporalTransferTypeConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| using System; | ||
|
|
||
| namespace Temporalio.Converters | ||
| { | ||
| /// <summary> | ||
| /// Converter for a type marked with <see cref="TemporalTransferTypeConverterAttribute"/>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This API is experimental and may change in a future release. | ||
| /// </remarks> | ||
| public interface ITemporalTransferTypeConverter | ||
| { | ||
| /// <summary> | ||
| /// Gets the transfer type handed to or read from the payload converter. | ||
| /// </summary> | ||
| Type TransferType { get; } | ||
|
|
||
| /// <summary> | ||
| /// Convert a value to the transfer type value that should be passed to the payload converter. | ||
| /// </summary> | ||
| /// <param name="value">Value to convert.</param> | ||
| /// <returns>Transfer type value to pass to the payload converter.</returns> | ||
| object? ToTransferType(object? value); | ||
|
|
||
| /// <summary> | ||
| /// Convert a transfer type value from the payload converter to the marked type. | ||
| /// </summary> | ||
| /// <param name="transferType">Transfer type value returned by the payload converter.</param> | ||
| /// <returns>Converted value.</returns> | ||
| object? FromTransferType(object? transferType); | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
src/Temporalio/Converters/TemporalTransferTypeConverterAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| using System; | ||
|
|
||
| namespace Temporalio.Converters | ||
| { | ||
| /// <summary> | ||
| /// Marks a type as converting to and from a transfer type before payload conversion. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This is used by the SDK payload converter to delegate hook-aware values to the configured | ||
| /// payload converter using the transfer type representation provided by | ||
| /// <see cref="ITemporalTransferTypeConverter"/>. | ||
| /// This API is experimental and may change in a future release. | ||
| /// </remarks> | ||
| [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false)] | ||
| public sealed class TemporalTransferTypeConverterAttribute : Attribute | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the | ||
| /// <see cref="TemporalTransferTypeConverterAttribute"/> class. | ||
| /// </summary> | ||
| /// <param name="converterType">Converter type implementing | ||
| /// <see cref="ITemporalTransferTypeConverter"/>.</param> | ||
| public TemporalTransferTypeConverterAttribute(Type converterType) => | ||
| ConverterType = converterType; | ||
|
|
||
| /// <summary> | ||
| /// Gets the converter type. | ||
| /// </summary> | ||
| public Type ConverterType { get; } | ||
| } | ||
| } |
128 changes: 128 additions & 0 deletions
128
src/Temporalio/Converters/TemporalTransferTypePayloadConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.Reflection; | ||
| using Temporalio.Api.Common.V1; | ||
|
|
||
| namespace Temporalio.Converters | ||
| { | ||
| /// <summary> | ||
| /// Payload converter wrapper that applies Temporal transfer type hooks. | ||
| /// </summary> | ||
| internal sealed class TemporalTransferTypePayloadConverter : | ||
| IPayloadConverter, | ||
| IWithSerializationContext<IPayloadConverter> | ||
| { | ||
| private static readonly ConcurrentDictionary<Type, ITemporalTransferTypeConverter?> Converters = new(); | ||
| private readonly IPayloadConverter inner; | ||
|
|
||
| private TemporalTransferTypePayloadConverter(IPayloadConverter inner) => this.inner = inner; | ||
|
|
||
| /// <summary> | ||
| /// Wrap a payload converter unless it is already wrapped. | ||
| /// </summary> | ||
| /// <param name="payloadConverter">Payload converter to wrap.</param> | ||
| /// <returns>Wrapped payload converter.</returns> | ||
| public static IPayloadConverter Wrap(IPayloadConverter payloadConverter) => | ||
| payloadConverter is TemporalTransferTypePayloadConverter ? | ||
| payloadConverter : new TemporalTransferTypePayloadConverter(payloadConverter); | ||
|
|
||
| /// <summary> | ||
| /// Wrap the data converter's payload converter unless it is already wrapped. | ||
| /// </summary> | ||
| /// <param name="dataConverter">Data converter to wrap.</param> | ||
| /// <returns>Data converter with a wrapped payload converter.</returns> | ||
| public static DataConverter Wrap(DataConverter dataConverter) | ||
| { | ||
| var payloadConverter = Wrap(dataConverter.PayloadConverter); | ||
| return ReferenceEquals(payloadConverter, dataConverter.PayloadConverter) ? | ||
| dataConverter : dataConverter with { PayloadConverter = payloadConverter }; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public Payload ToPayload(object? value) | ||
| { | ||
| var converter = value == null ? null : Converters.GetOrAdd(value.GetType(), CreateConverter); | ||
| if (converter != null) | ||
| { | ||
| value = converter.ToTransferType(value); | ||
| } | ||
| return inner.ToPayload(value); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public object? ToValue(Payload payload, Type type) | ||
| { | ||
| var converter = Converters.GetOrAdd(type, CreateConverter); | ||
| if (converter == null) | ||
| { | ||
| return inner.ToValue(payload, type); | ||
| } | ||
|
|
||
| var transferTypeValue = inner.ToValue(payload, converter.TransferType); | ||
| return converter.FromTransferType(transferTypeValue); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public IPayloadConverter WithSerializationContext(ISerializationContext context) | ||
| { | ||
| if (inner is not IWithSerializationContext<IPayloadConverter> withContext) | ||
| { | ||
| return this; | ||
| } | ||
|
|
||
| var contextInner = withContext.WithSerializationContext(context); | ||
| return ReferenceEquals(contextInner, inner) ? this : new TemporalTransferTypePayloadConverter(contextInner); | ||
| } | ||
|
|
||
| private static ITemporalTransferTypeConverter? CreateConverter(Type type) | ||
| { | ||
| var attr = type.GetCustomAttribute<TemporalTransferTypeConverterAttribute>( | ||
| inherit: false); | ||
| if (attr == null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| if (!typeof(ITemporalTransferTypeConverter).IsAssignableFrom(attr.ConverterType)) | ||
| { | ||
| throw new InvalidOperationException( | ||
| $"Type {type} has a Temporal transfer type converter type " + | ||
| $"{attr.ConverterType} that does not implement {nameof(ITemporalTransferTypeConverter)}."); | ||
| } | ||
| if (attr.ConverterType.IsAbstract) | ||
| { | ||
| throw new InvalidOperationException( | ||
| $"Type {type} has an abstract Temporal transfer type converter type " + | ||
| $"{attr.ConverterType}."); | ||
| } | ||
| if (attr.ConverterType.ContainsGenericParameters) | ||
| { | ||
| throw new InvalidOperationException( | ||
| $"Type {type} has an open generic Temporal transfer type converter type " + | ||
| $"{attr.ConverterType}."); | ||
| } | ||
| if (!attr.ConverterType.IsValueType && | ||
| attr.ConverterType.GetConstructor(Type.EmptyTypes) == null) | ||
| { | ||
| throw new InvalidOperationException( | ||
| $"Type {type} has a Temporal transfer type converter type " + | ||
| $"{attr.ConverterType} without a public parameterless constructor."); | ||
| } | ||
|
|
||
| if (Activator.CreateInstance(attr.ConverterType) is not ITemporalTransferTypeConverter converter) | ||
|
tconley1428 marked this conversation as resolved.
|
||
| { | ||
| throw new InvalidOperationException( | ||
| $"Type {type} has a Temporal transfer type converter type " + | ||
| $"{attr.ConverterType} that could not be instantiated."); | ||
| } | ||
| if (converter.TransferType == null) | ||
| { | ||
| throw new InvalidOperationException( | ||
| $"Type {type} has a Temporal transfer type converter type " + | ||
| $"{attr.ConverterType} with a null transfer type."); | ||
| } | ||
|
|
||
| return converter; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.