Skip to content

Commit

Permalink
fix(Sdk): Removed the DataModelDefinition
Browse files Browse the repository at this point in the history
fix(Sdk): Updated both InputDataModelDefinition and OutputDataModelDefinition to reflect serverlessworkflow/specification#892
fix(Sdk): Added the ExportDataModelDefinition, and updated TaskDefinition accordingly, to reflect serverlessworkflow/specification#892

Signed-off-by: Charles d'Avernas <[email protected]>
  • Loading branch information
cdavernas committed Jun 19, 2024
1 parent 5000ea3 commit 83563c4
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public static IServiceCollection AddServerlessWorkflowIO(this IServiceCollection
options.Deserializer.WithNodeDeserializer(
inner => new TaskDefinitionYamlDeserializer(inner),
syntax => syntax.InsteadOf<JsonSchemaDeserializer>());
options.Serializer.WithTypeConverter(new MapEntryYamlConverter(() => options.Serializer.Build()));
var mapEntryConverter = new MapEntryYamlConverter(() => options.Serializer.Build(), () => options.Deserializer.Build());
options.Deserializer.WithTypeConverter(mapEntryConverter);
options.Serializer.WithTypeConverter(mapEntryConverter);
});
services.AddSingleton<IWorkflowDefinitionReader, WorkflowDefinitionReader>();
services.AddSingleton<IWorkflowDefinitionReader, WorkflowDefinitionReader>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@
namespace ServerlessWorkflow.Sdk.Models;

/// <summary>
/// Represents the definition of a data model
/// Represents the definition of a data context
/// </summary>
[DataContract]
public abstract record DataModelDefinition
public record ContextDataModelDefinition
{

/// <summary>
/// Gets/sets the schema, if any, that defines and describes the defined data model
/// Gets/sets the schema, if any, that defines and describes the context data
/// </summary>
[DataMember(Name = "schema", Order = 1), JsonPropertyName("schema"), JsonPropertyOrder(1), YamlMember(Alias = "schema", Order = 1)]
public virtual SchemaDefinition? Schema { get; set; }

/// <summary>
/// Gets/sets a runtime expression, if any, used to build the defined model using both input and scope data
/// Gets/sets a runtime expression, if any, used to export specific data to the context data
/// </summary>
[DataMember(Name = "from", Order = 2), JsonPropertyName("from"), JsonPropertyOrder(2), JsonInclude, YamlMember(Alias = "from", Order = 2)]
public virtual object? From { get; set; }
[DataMember(Name = "as", Order = 3), JsonPropertyName("as"), JsonPropertyOrder(3), YamlMember(Alias = "as", Order = 3)]
public virtual object? As { get; set; }

}
}
13 changes: 11 additions & 2 deletions src/ServerlessWorkflow.Sdk/Models/InputDataModelDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,22 @@
namespace ServerlessWorkflow.Sdk.Models;

/// <summary>
/// Represents the definition of an output data model
/// Represents the definition of an input data model
/// </summary>
[DataContract]
public record InputDataModelDefinition
: DataModelDefinition
{

/// <summary>
/// Gets/sets the schema, if any, that defines and describes the input data of a workflow or task
/// </summary>
[DataMember(Name = "schema", Order = 1), JsonPropertyName("schema"), JsonPropertyOrder(1), YamlMember(Alias = "schema", Order = 1)]
public virtual SchemaDefinition? Schema { get; set; }

/// <summary>
/// Gets/sets a runtime expression, if any, used to build the workflow or task input data based on both input and scope data
/// </summary>
[DataMember(Name = "from", Order = 2), JsonPropertyName("from"), JsonPropertyOrder(2), JsonInclude, YamlMember(Alias = "from", Order = 2)]
public virtual object? From { get; set; }

}
13 changes: 9 additions & 4 deletions src/ServerlessWorkflow.Sdk/Models/OutputDataModelDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ namespace ServerlessWorkflow.Sdk.Models;
/// </summary>
[DataContract]
public record OutputDataModelDefinition
: DataModelDefinition
{

/// <summary>
/// Gets/sets the schema, if any, that defines and describes the output data of a workflow or task
/// </summary>
[DataMember(Name = "schema", Order = 1), JsonPropertyName("schema"), JsonPropertyOrder(1), YamlMember(Alias = "schema", Order = 1)]
public virtual SchemaDefinition? Schema { get; set; }

/// <summary>
/// Gets/sets a runtime expression, if any, used to output specific data to the scope data
/// </summary>
[DataMember(Name = "to", Order = 3), JsonPropertyName("to"), JsonPropertyOrder(3), YamlMember(Alias = "to", Order = 3)]
public virtual object? To { get; set; }
[DataMember(Name = "as", Order = 3), JsonPropertyName("as"), JsonPropertyOrder(3), YamlMember(Alias = "as", Order = 3)]
public virtual object? As { get; set; }

}
}
11 changes: 8 additions & 3 deletions src/ServerlessWorkflow.Sdk/Models/TaskDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// limitations under the License.

using ServerlessWorkflow.Sdk.Serialization.Json;
using System.ComponentModel;

namespace ServerlessWorkflow.Sdk.Models;

Expand Down Expand Up @@ -42,16 +41,22 @@ public abstract record TaskDefinition
[DataMember(Name = "output", Order = 11), JsonPropertyName("output"), JsonPropertyOrder(11), YamlMember(Alias = "output", Order = 11)]
public virtual OutputDataModelDefinition? Output { get; set; }

/// <summary>
/// Gets/sets the optional configuration for exporting data within the task's context
/// </summary>
[DataMember(Name = "export", Order = 12), JsonPropertyName("export"), JsonPropertyOrder(12), YamlMember(Alias = "export", Order = 12)]
public virtual OutputDataModelDefinition? Export { get; set; }

/// <summary>
/// Gets/sets a boolean indicating whether or not to return the result, if any, of the defined task
/// </summary>
[DataMember(Name = "timeout", Order = 12), JsonPropertyName("timeout"), JsonPropertyOrder(12), YamlMember(Alias = "timeout", Order = 12)]
[DataMember(Name = "timeout", Order = 13), JsonPropertyName("timeout"), JsonPropertyOrder(13), YamlMember(Alias = "timeout", Order = 13)]
public virtual TimeoutDefinition? Timeout { get; set; }

/// <summary>
/// Gets/sets the flow directive to be performed upon completion of the task
/// </summary>
[DataMember(Name = "then", Order = 13), JsonPropertyName("then"), JsonPropertyOrder(13), YamlMember(Alias = "then", Order = 13)]
[DataMember(Name = "then", Order = 14), JsonPropertyName("then"), JsonPropertyOrder(14), YamlMember(Alias = "then", Order = 14)]
public virtual string? Then { get; set; }

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ namespace ServerlessWorkflow.Sdk.Serialization.Yaml;
/// Represents the service used to serialize/deserialize <see cref="MapEntry{TKey, TValue}"/> values to/from YAML
/// </summary>
/// <param name="serializerFactory">A function used to create a new <see cref="ISerializer"/></param>
public class MapEntryYamlConverter(Func<ISerializer> serializerFactory)
/// <param name="deserializerFactory">A function used to create a new <see cref="IDeserializer"/></param>
public class MapEntryYamlConverter(Func<ISerializer> serializerFactory, Func<IDeserializer> deserializerFactory)
: IYamlTypeConverter
{

Expand All @@ -43,10 +44,10 @@ protected virtual IYamlTypeConverter CreateGenericConverter(Type type)
{
var typeArguments = type.GetGenericArguments();
var converterType = typeof(MapEntryConverter<,>).MakeGenericType(typeArguments);
return (IYamlTypeConverter)Activator.CreateInstance(converterType, serializerFactory())!;
return (IYamlTypeConverter)Activator.CreateInstance(converterType, serializerFactory(), deserializerFactory())!;
}

class MapEntryConverter<TKey, TValue> (ISerializer serializer)
class MapEntryConverter<TKey, TValue> (ISerializer serializer, IDeserializer deserializer)
: IYamlTypeConverter
where TKey : notnull
{
Expand All @@ -55,7 +56,14 @@ class MapEntryConverter<TKey, TValue> (ISerializer serializer)
public bool Accepts(Type type) => type == typeof(MapEntry<TKey, TValue>);

/// <inheritdoc/>
public virtual object ReadYaml(IParser parser, Type type) => throw new NotImplementedException();
public virtual object ReadYaml(IParser parser, Type type)
{
parser.Consume<MappingStart>();
var key = deserializer.Deserialize<TKey>(parser);
var value = deserializer.Deserialize<TValue>(parser);
parser.Consume<MappingEnd>();
return new MapEntry<TKey, TValue>(key, value);
}

/// <inheritdoc/>
public virtual void WriteYaml(IEmitter emitter, object? value, Type type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

namespace ServerlessWorkflow.Sdk.UnitTests.Cases.IO;

public class WorkflowDefinitionWriterTests
public class WorkflowDefinitionIOTests
{

[Fact]
public async Task Write_Workflow_Definition_To_Yaml_Should_Work()
public async Task WriteThenRead_Workflow_Definition_ToFrom_Yaml_Should_Work()
{
//arrange
var toSerialize = WorkflowDefinitionFactory.Create();
Expand All @@ -32,6 +32,7 @@ public async Task Write_Workflow_Definition_To_Yaml_Should_Work()
await stream.FlushAsync();
stream.Position = 0;
var yaml = new StreamReader(stream).ReadToEnd();
stream.Position = 0;
var deserialized = await reader.ReadAsync(stream);

//assert
Expand All @@ -40,7 +41,7 @@ public async Task Write_Workflow_Definition_To_Yaml_Should_Work()
}

[Fact]
public async Task Write_Workflow_Definition_To_Json_Should_Work()
public async Task WriteThenRead_Workflow_Definition_ToFrom_Json_Should_Work()
{
//arrange
var toSerialize = WorkflowDefinitionFactory.Create();
Expand Down

This file was deleted.

0 comments on commit 83563c4

Please sign in to comment.