forked from danielgerlag/workflow-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelExtensions.cs
165 lines (135 loc) · 7.56 KB
/
ModelExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using Amazon.DynamoDBv2.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using WorkflowCore.Models;
namespace WorkflowCore.Providers.AWS
{
internal static class ModelExtensions
{
private static JsonSerializerSettings SerializerSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
public static Dictionary<string, AttributeValue> ToDynamoMap(this WorkflowInstance source)
{
var result = new Dictionary<string, AttributeValue>();
result["id"] = new AttributeValue(source.Id);
result["workflow_definition_id"] = new AttributeValue(source.WorkflowDefinitionId);
result["version"] = new AttributeValue(source.Version.ToString());
result["next_execution"] = new AttributeValue { N = (source.NextExecution ?? 0).ToString() };
result["create_time"] = new AttributeValue { N = source.CreateTime.Ticks.ToString() };
result["data"] = new AttributeValue(JsonConvert.SerializeObject(source.Data, SerializerSettings));
result["workflow_status"] = new AttributeValue { N = Convert.ToInt32(source.Status).ToString() };
if (!string.IsNullOrEmpty(source.Description))
result["description"] = new AttributeValue(source.Description);
if (!string.IsNullOrEmpty(source.Reference))
result["reference"] = new AttributeValue(source.Reference);
if (source.CompleteTime.HasValue)
result["complete_time"] = new AttributeValue { N = source.CompleteTime.Value.Ticks.ToString() };
var pointers = new List<AttributeValue>();
foreach (var pointer in source.ExecutionPointers)
{
pointers.Add(new AttributeValue(JsonConvert.SerializeObject(pointer, SerializerSettings)));
}
result["pointers"] = new AttributeValue { L = pointers };
if (source.Status == WorkflowStatus.Runnable)
result["runnable"] = new AttributeValue { N = 1.ToString() };
return result;
}
public static WorkflowInstance ToWorkflowInstance(this Dictionary<string, AttributeValue> source)
{
var result = new WorkflowInstance
{
Id = source["id"].S,
WorkflowDefinitionId = source["workflow_definition_id"].S,
Version = Convert.ToInt32(source["version"].S),
Status = (WorkflowStatus)Convert.ToInt32(source["workflow_status"].N),
NextExecution = Convert.ToInt64(source["next_execution"].N),
CreateTime = new DateTime(Convert.ToInt64(source["create_time"].N)),
Data = JsonConvert.DeserializeObject(source["data"].S, SerializerSettings)
};
if (source.ContainsKey("description"))
result.Description = source["description"].S;
if (source.ContainsKey("reference"))
result.Reference = source["reference"].S;
if (source.ContainsKey("complete_time"))
result.CompleteTime = new DateTime(Int64.Parse(source["complete_time"].N));
foreach (var pointer in source["pointers"].L)
{
var ep = JsonConvert.DeserializeObject<ExecutionPointer>(pointer.S, SerializerSettings);
result.ExecutionPointers.Add(ep);
}
return result;
}
public static Dictionary<string, AttributeValue> ToDynamoMap(this EventSubscription source)
{
var result = new Dictionary<string, AttributeValue>
{
["id"] = new AttributeValue(source.Id),
["event_name"] = new AttributeValue(source.EventName),
["event_key"] = new AttributeValue(source.EventKey),
["workflow_id"] = new AttributeValue(source.WorkflowId),
["execution_pointer_id"] = new AttributeValue(source.ExecutionPointerId),
["step_id"] = new AttributeValue(source.StepId.ToString()),
["subscribe_as_of"] = new AttributeValue { N = source.SubscribeAsOf.Ticks.ToString() },
["subscription_data"] = new AttributeValue(JsonConvert.SerializeObject(source.SubscriptionData, SerializerSettings)),
["event_slug"] = new AttributeValue($"{source.EventName}:{source.EventKey}")
};
if (!string.IsNullOrEmpty(source.ExternalToken))
result["external_token"] = new AttributeValue(source.ExternalToken);
if (!string.IsNullOrEmpty(source.ExternalWorkerId))
result["external_worker_id"] = new AttributeValue(source.ExternalWorkerId);
if (source.ExternalTokenExpiry.HasValue)
result["external_token_expiry"] = new AttributeValue { N = source.ExternalTokenExpiry.Value.Ticks.ToString()};
return result;
}
public static EventSubscription ToEventSubscription(this Dictionary<string, AttributeValue> source)
{
var result = new EventSubscription
{
Id = source["id"].S,
EventName = source["event_name"].S,
EventKey = source["event_key"].S,
WorkflowId = source["workflow_id"].S,
ExecutionPointerId = source["execution_pointer_id"].S,
StepId = Convert.ToInt32(source["step_id"].S),
SubscribeAsOf = new DateTime(Convert.ToInt64(source["subscribe_as_of"].N)),
SubscriptionData = JsonConvert.DeserializeObject(source["subscription_data"].S, SerializerSettings),
};
if (source.ContainsKey("external_token"))
result.ExternalToken = source["external_token"].S;
if (source.ContainsKey("external_worker_id"))
result.ExternalWorkerId = source["external_worker_id"].S;
if (source.ContainsKey("external_token_expiry"))
result.ExternalTokenExpiry = new DateTime(Int64.Parse(source["external_token_expiry"].N));
return result;
}
public static Dictionary<string, AttributeValue> ToDynamoMap(this Event source)
{
var result = new Dictionary<string, AttributeValue>
{
["id"] = new AttributeValue(source.Id),
["event_name"] = new AttributeValue(source.EventName),
["event_key"] = new AttributeValue(source.EventKey),
["event_data"] = new AttributeValue(JsonConvert.SerializeObject(source.EventData, SerializerSettings)),
["event_time"] = new AttributeValue { N = source.EventTime.Ticks.ToString() },
["event_slug"] = new AttributeValue($"{source.EventName}:{source.EventKey}")
};
if (!source.IsProcessed)
result["not_processed"] = new AttributeValue { N = 1.ToString() };
return result;
}
public static Event ToEvent(this Dictionary<string, AttributeValue> source)
{
var result = new Event
{
Id = source["id"].S,
EventName = source["event_name"].S,
EventKey = source["event_key"].S,
EventData = JsonConvert.DeserializeObject(source["event_data"].S, SerializerSettings),
EventTime = new DateTime(Convert.ToInt64(source["event_time"].N)),
IsProcessed = (!source.ContainsKey("not_processed"))
};
return result;
}
}
}