Skip to content

Commit

Permalink
feat: Added expression parsing possibility to ValueContainer.cs
Browse files Browse the repository at this point in the history
When creating a VC from JSON, it is possible to provide an expression engine, to parse the values when creating the new VC.
  • Loading branch information
thygesteffensen committed Mar 3, 2021
1 parent bfe0141 commit c14f72c
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions PowerAutomateMockUp/ExpressionParser/ValueContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,16 @@ public ValueContainer()
_value = null;
}

public ValueContainer(JToken json)
public ValueContainer(JToken json, IExpressionEngine expressionEngine = null)
{
var v = JsonToValueContainer(json);
if (json == null)
{
_type = ValueType.Null;
_value = null;
return;
}

var v = JsonToValueContainer(json, expressionEngine);
_type = v._type;
_value = v._value;
}
Expand Down Expand Up @@ -222,24 +229,26 @@ public T GetValue<T>()
throw new PowerAutomateMockUpException("Can't get none object value container as dict.");
}

private ValueContainer JsonToValueContainer(JToken json)
private ValueContainer JsonToValueContainer(JToken json, IExpressionEngine expressionEngine)
{
switch (json)
{
case JObject jObject:
case JObject _:
{
var dictionary = json.ToDictionary(pair => ((JProperty) pair).Name, token =>
{
if (token.Children().Count() != 1) return JsonToValueContainer(token.Children().First());
if (token.Children().Count() != 1)
return JsonToValueContainer(token.Children().First(), expressionEngine);
var t = token.First;
return t.Type switch
{
JTokenType.String => new ValueContainer(t.Value<string>(), true),
JTokenType.String => expressionEngine?.ParseToValueContainer(t.Value<string>()) ??
new ValueContainer(t.Value<string>()),
JTokenType.Boolean => new ValueContainer(t.Value<bool>()),
JTokenType.Integer => new ValueContainer(t.Value<int>()),
JTokenType.Float => new ValueContainer(t.Value<float>()),
_ => JsonToValueContainer(token.Children().First())
_ => JsonToValueContainer(token.Children().First(), expressionEngine)
};
});

Expand All @@ -260,7 +269,8 @@ private ValueContainer JsonToValueContainer(JToken json)
JTokenType.Integer => new ValueContainer(jValue.Value<int>()),
JTokenType.Float => new ValueContainer(jValue.Value<float>()),
JTokenType.Null => new ValueContainer(),
JTokenType.String => new ValueContainer(jValue.Value<string>()),
JTokenType.String => expressionEngine?.ParseToValueContainer(jValue.Value<string>()) ??
new ValueContainer(jValue.Value<string>()),
JTokenType.None => new ValueContainer(),
JTokenType.Guid => new ValueContainer(jValue.Value<Guid>().ToString()),
_ => throw new PowerAutomateMockUpException(
Expand Down

0 comments on commit c14f72c

Please sign in to comment.