-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathDslInput.cs
More file actions
136 lines (109 loc) · 4.24 KB
/
DslInput.cs
File metadata and controls
136 lines (109 loc) · 4.24 KB
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
using System.Text.Json.Serialization;
using YamlDotNet.Serialization;
namespace TemporalioSamples.Dsl;
public record DslInput
{
public record ActivityInvocation
{
required public string Name { get; init; }
public IReadOnlyList<string> Arguments { get; init; } = Array.Empty<string>();
public string? Result { get; init; }
}
[JsonPolymorphic(TypeDiscriminatorPropertyName = "statementType")]
[JsonDerivedType(typeof(ActivityStatement), "activity")]
[JsonDerivedType(typeof(SequenceStatement), "sequence")]
[JsonDerivedType(typeof(ParallelStatement), "parallel")]
public abstract record Statement;
public record ActivityStatement : Statement
{
required public ActivityInvocation Activity { get; init; }
}
public record Sequence
{
required public IReadOnlyList<Statement> Elements { get; init; }
}
public record SequenceStatement : Statement
{
required public Sequence Sequence { get; init; }
}
public record ParallelBranches
{
required public IReadOnlyList<Statement> Branches { get; init; }
}
public record ParallelStatement : Statement
{
required public ParallelBranches Parallel { get; init; }
}
required public Statement Root { get; init; }
public Dictionary<string, object> Variables { get; init; } = new();
public static DslInput Parse(string yamlContent)
{
var deserializer = new DeserializerBuilder().Build();
var yamlObject = deserializer.Deserialize<Dictionary<string, object>>(yamlContent)
?? throw new InvalidOperationException("Failed to parse YAML");
return ConvertToDslInput(yamlObject);
}
private static DslInput ConvertToDslInput(Dictionary<string, object> yaml)
{
var variables = new Dictionary<string, object>();
if (yaml.TryGetValue("variables", out var varsObj) && varsObj is Dictionary<object, object> varsDict)
{
foreach (var kvp in varsDict)
{
variables[kvp.Key.ToString() ?? string.Empty] = kvp.Value;
}
}
var rootObj = yaml["root"];
var root = ConvertToStatement(rootObj);
return new DslInput { Root = root, Variables = variables };
}
private static Statement ConvertToStatement(object obj)
{
if (obj is not Dictionary<object, object> dict)
{
throw new ArgumentException("Statement must be a dictionary");
}
if (dict.TryGetValue("activity", out var activityObj))
{
return new ActivityStatement { Activity = ConvertToActivityInvocation(activityObj) };
}
if (dict.TryGetValue("sequence", out var sequenceObj))
{
var seqDict = (Dictionary<object, object>)sequenceObj;
var elements = ((List<object>)seqDict["elements"])
.Select(ConvertToStatement)
.ToList();
return new SequenceStatement { Sequence = new Sequence { Elements = elements } };
}
if (dict.TryGetValue("parallel", out var parallelObj))
{
var parDict = (Dictionary<object, object>)parallelObj;
var branches = ((List<object>)parDict["branches"])
.Select(ConvertToStatement)
.ToList();
return new ParallelStatement { Parallel = new ParallelBranches { Branches = branches } };
}
throw new ArgumentException("Unknown statement type");
}
private static ActivityInvocation ConvertToActivityInvocation(object obj)
{
var dict = (Dictionary<object, object>)obj;
var name = dict["name"].ToString() ?? throw new ArgumentException("Activity name is required");
var arguments = new List<string>();
if (dict.TryGetValue("arguments", out var argsObj) && argsObj is List<object> argsList)
{
arguments = argsList.Select(a => a.ToString() ?? string.Empty).ToList();
}
string? result = null;
if (dict.TryGetValue("result", out var resultObj))
{
result = resultObj.ToString();
}
return new ActivityInvocation
{
Name = name,
Arguments = arguments,
Result = result,
};
}
}