| | | 1 | | using System.Text.Json; |
| | | 2 | | using Elsa.Expressions.Contracts; |
| | | 3 | | using Elsa.Workflows.Models; |
| | | 4 | | using Humanizer; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Workflows.Serialization.Helpers; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Helper class for writing synthetic properties of an activity to a JSON writer. |
| | | 10 | | /// </summary> |
| | 14 | 11 | | public class SyntheticPropertiesWriter(IExpressionDescriptorRegistry expressionDescriptorRegistry) |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Writes synthetic properties of an activity to a JSON writer. |
| | | 15 | | /// </summary> |
| | | 16 | | public void WriteSyntheticProperties(Utf8JsonWriter writer, IActivity value, ActivityDescriptor activityDescriptor, |
| | | 17 | | { |
| | 1185 | 18 | | WriteSyntheticInputProperties(writer, value, activityDescriptor, options); |
| | 1185 | 19 | | WriteSyntheticOutputProperties(writer, value, activityDescriptor, options); |
| | 1185 | 20 | | } |
| | | 21 | | |
| | | 22 | | private void WriteSyntheticInputProperties(Utf8JsonWriter writer, IActivity value, ActivityDescriptor activityDescri |
| | | 23 | | { |
| | 3604 | 24 | | var syntheticInputs = activityDescriptor.Inputs.Where(x => x.IsSynthetic).ToList(); |
| | | 25 | | |
| | 2384 | 26 | | foreach (var inputDescriptor in syntheticInputs) |
| | | 27 | | { |
| | 7 | 28 | | var inputName = inputDescriptor.Name; |
| | 7 | 29 | | var propertyName = inputName.Camelize(); |
| | | 30 | | |
| | 7 | 31 | | if (!value.SyntheticProperties.TryGetValue(inputName, out var inputValue)) |
| | | 32 | | continue; |
| | | 33 | | |
| | 7 | 34 | | writer.WritePropertyName(propertyName); |
| | | 35 | | |
| | 7 | 36 | | var input = (Input?)inputValue; |
| | | 37 | | |
| | 7 | 38 | | if (input == null) |
| | | 39 | | { |
| | 0 | 40 | | writer.WriteNullValue(); |
| | 0 | 41 | | continue; |
| | | 42 | | } |
| | | 43 | | |
| | 7 | 44 | | var expression = input.Expression; |
| | 7 | 45 | | var expressionType = expression?.Type; |
| | 7 | 46 | | var inputType = input.Type; |
| | 7 | 47 | | var memoryReferenceId = input.MemoryBlockReference().Id; |
| | 7 | 48 | | var expressionDescriptor = expressionType != null ? expressionDescriptorRegistry.Find(expressionType) : null |
| | | 49 | | |
| | 7 | 50 | | if (expressionDescriptor == null) |
| | 0 | 51 | | throw new Exception($"Syntax descriptor with expression type {expressionType} not found in registry"); |
| | | 52 | | |
| | 7 | 53 | | var inputModel = new |
| | 7 | 54 | | { |
| | 7 | 55 | | TypeName = inputType, |
| | 7 | 56 | | Expression = expression, |
| | 7 | 57 | | MemoryReference = new |
| | 7 | 58 | | { |
| | 7 | 59 | | Id = memoryReferenceId |
| | 7 | 60 | | } |
| | 7 | 61 | | }; |
| | | 62 | | |
| | 7 | 63 | | JsonSerializer.Serialize(writer, inputModel, inputModel.GetType(), options); |
| | | 64 | | } |
| | 1185 | 65 | | } |
| | | 66 | | |
| | | 67 | | private void WriteSyntheticOutputProperties(Utf8JsonWriter writer, IActivity value, ActivityDescriptor activityDescr |
| | | 68 | | { |
| | 1765 | 69 | | var syntheticOutputs = activityDescriptor.Outputs.Where(x => x.IsSynthetic).ToList(); |
| | | 70 | | |
| | | 71 | | // Write synthetic outputs. |
| | 2412 | 72 | | foreach (var outputDescriptor in syntheticOutputs) |
| | | 73 | | { |
| | 21 | 74 | | var outputName = outputDescriptor.Name; |
| | 21 | 75 | | var propertyName = outputName.Camelize(); |
| | | 76 | | |
| | 21 | 77 | | if (!value.SyntheticProperties.TryGetValue(outputName, out var outputValue)) |
| | | 78 | | continue; |
| | | 79 | | |
| | 7 | 80 | | writer.WritePropertyName(propertyName); |
| | 7 | 81 | | var output = (Output?)outputValue; |
| | | 82 | | |
| | 7 | 83 | | if (output == null) |
| | | 84 | | { |
| | 0 | 85 | | writer.WriteNullValue(); |
| | 0 | 86 | | continue; |
| | | 87 | | } |
| | | 88 | | |
| | 7 | 89 | | var outputType = outputDescriptor.Type; |
| | 7 | 90 | | var memoryReferenceId = output.MemoryBlockReference().Id; |
| | | 91 | | |
| | 7 | 92 | | var outputModel = new |
| | 7 | 93 | | { |
| | 7 | 94 | | TypeName = outputType, |
| | 7 | 95 | | MemoryReference = new |
| | 7 | 96 | | { |
| | 7 | 97 | | Id = memoryReferenceId |
| | 7 | 98 | | } |
| | 7 | 99 | | }; |
| | | 100 | | |
| | 7 | 101 | | JsonSerializer.Serialize(writer, outputModel, outputModel.GetType(), options); |
| | | 102 | | } |
| | 1185 | 103 | | } |
| | | 104 | | } |