Skip to content

Commit

Permalink
Serialize/deserialize enums to/from strings
Browse files Browse the repository at this point in the history
  • Loading branch information
boma96 committed Oct 24, 2024
1 parent c355bcb commit 2a8328d
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 1 deletion.
34 changes: 34 additions & 0 deletions examples/ConductorSharp.Definitions/Generated/Task.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ConductorSharp.Engine.Model;
using ConductorSharp.Engine.Util;
using MediatR;
using Newtonsoft.Json;

namespace ConductorSharp.Definitions.Generated
{
Expand Down Expand Up @@ -59,4 +60,37 @@ public partial class CustomerGetV1Output
///
[OriginalName("CUSTOMER_get")]
public partial class CustomerGetV1 : SimpleTaskModel<CustomerGetV1Input, CustomerGetV1Output> { }

public partial class EnumTaskInput : IRequest<EnumTaskOutput>
{
/// <originalName>
/// status
/// </originalName>
[JsonProperty("status")]
public object Status { get; set; }
}

public partial class EnumTaskOutput
{
/// <originalName>
/// status
/// </originalName>
[JsonProperty("status")]
public object Status { get; set; }
}

/// <originalName>
/// ENUM_task
/// </originalName>
/// <ownerEmail>
/// [email protected]
/// </ownerEmail>
/// <node>
///
/// </node>
/// <summary>
///
/// </summary>
[OriginalName("ENUM_task")]
public partial class EnumTask : SimpleTaskModel<EnumTaskInput, EnumTaskOutput> { }
}
1 change: 1 addition & 0 deletions examples/ConductorSharp.Definitions/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
services.RegisterWorkflow<SendCustomerNotification>();
services.RegisterWorkflow<HandleNotificationFailure>();
services.RegisterWorkflow<CSharpLambdaWorkflow>();
services.RegisterWorkflow<EnumWorkflow>();
}
);

Expand Down
28 changes: 28 additions & 0 deletions examples/ConductorSharp.Definitions/Workflows/EnumWorkflow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using ConductorSharp.Client.Generated;
using ConductorSharp.Definitions.Generated;
using ConductorSharp.Engine.Builders;
using ConductorSharp.Engine.Builders.Metadata;

namespace ConductorSharp.Definitions.Workflows
{
public class EnumWorkflowInput : WorkflowInput<EnumWorkflowOutput> { }

public class EnumWorkflowOutput : WorkflowOutput { }

[OriginalName("ENUM_workflow")]
public class EnumWorkflow : Workflow<EnumWorkflow, EnumWorkflowInput, EnumWorkflowOutput>
{
public EnumWorkflow(WorkflowDefinitionBuilder<EnumWorkflow, EnumWorkflowInput, EnumWorkflowOutput> builder)
: base(builder) { }

public EnumTask EnumTask1 { get; set; }
public EnumTask EnumTask2 { get; set; }

public override void BuildDefinition()
{
_builder.AddTask(wf => wf.EnumTask1, wf => new() { Status = 1 });

_builder.AddTask(wf => wf.EnumTask2, wf => new() { Status = wf.EnumTask1.Output.Status });
}
}
}
27 changes: 27 additions & 0 deletions examples/ConductorSharp.NoApi/Handlers/EnumTaskHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using ConductorSharp.Client.Generated;
using ConductorSharp.Engine;
using ConductorSharp.Engine.Builders.Metadata;
using MediatR;

namespace ConductorSharp.NoApi.Handlers
{
public class EnumTaskInput : IRequest<EnumTaskOutput>
{
public WorkflowStatus Status { get; set; }
}

public class EnumTaskOutput
{
public WorkflowStatus Status { get; set; }
}

[OriginalName("ENUM_task")]
public class EnumTaskHandler : TaskRequestHandler<EnumTaskInput, EnumTaskOutput>
{
public override Task<EnumTaskOutput> Handle(EnumTaskInput request, CancellationToken cancellationToken)
{
Console.WriteLine(request.Status);
return System.Threading.Tasks.Task.FromResult(new EnumTaskOutput() { Status = request.Status });
}
}
}
1 change: 1 addition & 0 deletions examples/ConductorSharp.NoApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

services.RegisterWorkerTask<GetCustomerHandler>();
services.RegisterWorkerTask<PrepareEmailHandler>();
services.RegisterWorkerTask<EnumTaskHandler>();
}
);

Expand Down
3 changes: 2 additions & 1 deletion src/ConductorSharp.Client/ConductorConstants.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ConductorSharp.Client.Util;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;

namespace ConductorSharp.Client
Expand All @@ -24,7 +25,7 @@ public static JsonSerializerSettings IoJsonSerializerSettings
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead,
TypeNameHandling = TypeNameHandling.Auto,
Converters = new List<JsonConverter>() { new JsonNodeConverter(), new JsonElementConverter() }
Converters = new List<JsonConverter>() { new JsonNodeConverter(), new JsonElementConverter(), new StringEnumConverter() }
};
}
}
Expand Down

0 comments on commit 2a8328d

Please sign in to comment.