Skip to content

Commit

Permalink
Cleaned up semantics around switch/case expression and constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Laird-McConnell committed Feb 8, 2020
1 parent fbf8538 commit d41968f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 746 deletions.
35 changes: 2 additions & 33 deletions libraries/Microsoft.Bot.Builder.Dialogs.Adaptive/Actions/Case.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.

using System.Collections.Generic;
using System.Linq;
using Microsoft.Bot.Expressions;
using Newtonsoft.Json;

Expand All @@ -17,42 +16,12 @@ public Case(string value = null, IEnumerable<Dialog> actions = null)
}

/// <summary>
/// Gets or sets value expression to be compared against condition.
/// Gets or sets constant to be compared against condition.
/// </summary>
/// <value>
/// Value expression to be compared against condition.
/// Constant be compared against condition.
/// </value>
[JsonProperty("value")]
public string Value { get; set; }

/// <summary>
/// Creates an expression that returns the value in its primitive type. Still
/// assumes that switch case values are compile time constants and not expressions
/// that can be evaluated against state.
/// </summary>
/// <returns>An expression that reflects the constant case value.</returns>
public Expression CreateValueExpression()
{
Expression expression = null;

if (long.TryParse(Value, out long i))
{
expression = Expression.ConstantExpression(i);
}
else if (float.TryParse(Value, out float f))
{
expression = Expression.ConstantExpression(f);
}
else if (bool.TryParse(Value, out bool b))
{
expression = Expression.ConstantExpression(b);
}
else
{
expression = Expression.ConstantExpression(Value);
}

return expression;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public SwitchCondition([CallerFilePath] string callerPath = "", [CallerLineNumbe
/// Value Expression against memory. This value expression will be combined with value expression in case statements to make a bool expression.
/// </value>
[JsonProperty("condition")]
public ValueExpression Condition { get; set; }
public Expression Condition { get; set; }

/// <summary>
/// Gets or sets an optional expression which if is true will disable this action.
Expand All @@ -50,7 +50,7 @@ public SwitchCondition([CallerFilePath] string callerPath = "", [CallerLineNumbe
/// A boolean expression.
/// </value>
[JsonProperty("disabled")]
public BoolExpression Disabled { get; set; }
public BoolExpression Disabled { get; set; }

/// <summary>
/// Gets or sets default case.
Expand Down Expand Up @@ -121,8 +121,27 @@ public override async Task<DialogTurnResult> BeginDialogAsync(DialogContext dc,

foreach (var cse in this.Cases)
{
// Values for cases are always coerced to string
var caseCondition = Expression.EqualsExpression(this.Condition.ToExpression(), cse.CreateValueExpression());
Expression caseExpression = null;

// Values for cases are always coerced to constant
if (long.TryParse(cse.Value, out long i))
{
caseExpression = Expression.ConstantExpression(i);
}
else if (float.TryParse(cse.Value, out float f))
{
caseExpression = Expression.ConstantExpression(f);
}
else if (bool.TryParse(cse.Value, out bool b))
{
caseExpression = Expression.ConstantExpression(b);
}
else
{
caseExpression = Expression.ConstantExpression(cse.Value);
}

var caseCondition = Expression.EqualsExpression(this.Condition, caseExpression);

// Map of expression to actions
this.caseExpressions[cse.Value] = caseCondition;
Expand Down
6 changes: 6 additions & 0 deletions libraries/Microsoft.Bot.Expressions/Expression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ public Expression(ExpressionEvaluator evaluator, params Expression[] children)
/// </value>
public ReturnType ReturnType => Evaluator.ReturnType;

/// <summary>
/// allow a string to be implicitly assigned to an expression property.
/// </summary>
/// <param name="expression">string expression.</param>
public static implicit operator Expression(string expression) => Expression.Parse(expression);

/// <summary>
/// Parse an string into an expression object.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public override void SetValue(object value)
}

// keep the string as quoted expression, which will be literal unless string interpolation is used.
this.ExpressionText = $"'{stringOrExpression}'";
this.ExpressionText = $"=`{stringOrExpression}`";
return;
}

Expand Down

0 comments on commit d41968f

Please sign in to comment.