Skip to content

Commit

Permalink
#minor CherryPick control property to 4.14 (#5956)
Browse files Browse the repository at this point in the history
* add control property (#5943)

* add control property

* disable fragile test

* flip enableFromFile to false by default and fix unit tests (#5953)

* flip enableFromFile to false by default and fix unit tests

* fxi unit test

Co-authored-by: johnataylor <johtaylo@microsoft.com>
  • Loading branch information
EricDahlvang and johnataylor authored Nov 7, 2021
1 parent d33c8d5 commit 038309f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
11 changes: 7 additions & 4 deletions libraries/Microsoft.Bot.Builder.LanguageGeneration/Evaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -605,11 +605,14 @@ private EvaluatorLookup CustomizedEvaluatorLookup(EvaluatorLookup baseLookup)
return new ExpressionEvaluator(template, FunctionUtils.Apply(this.TemplateFunction()), ReturnType.Object, this.ValidateTemplateFunction);
}

const string fromFile = "fromFile";

if (name.Equals(fromFile, StringComparison.Ordinal))
if (Templates.EnableFromFile)
{
return new ExpressionEvaluator(fromFile, FunctionUtils.Apply(FromFile()), ReturnType.String, ValidateFromFile);
const string fromFile = "fromFile";

if (name.Equals(fromFile, StringComparison.Ordinal))
{
return new ExpressionEvaluator(fromFile, FunctionUtils.Apply(FromFile()), ReturnType.String, ValidateFromFile);
}
}

const string activityAttachment = "ActivityAttachment";
Expand Down
11 changes: 7 additions & 4 deletions libraries/Microsoft.Bot.Builder.LanguageGeneration/Expander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,14 @@ private EvaluatorLookup CustomizedEvaluatorLookup(EvaluatorLookup baseLookup, bo
return new ExpressionEvaluator(template, FunctionUtils.Apply(this.TemplateFunction()), ReturnType.Object, this.ValidateTemplateFunction);
}

const string fromFile = "fromFile";

if (name.Equals(fromFile, StringComparison.Ordinal))
if (Templates.EnableFromFile)
{
return new ExpressionEvaluator(fromFile, FunctionUtils.Apply(this.FromFile()), ReturnType.String, ValidateFromFile);
const string fromFile = "fromFile";

if (name.Equals(fromFile, StringComparison.Ordinal))
{
return new ExpressionEvaluator(fromFile, FunctionUtils.Apply(this.FromFile()), ReturnType.String, ValidateFromFile);
}
}

const string activityAttachment = "ActivityAttachment";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ public Templates(
InjectToExpressionFunction();
}

/// <summary>
/// Gets or sets a value indicating whether fromFile is allowed in LG templates.
/// </summary>
/// <value>
/// Boolean where true indicates fromFile is allowed.
/// </value>
public static bool EnableFromFile { get; set; } = false;

/// <summary>
/// Gets get all templates from current lg file and reference lg files.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class ActivityFactoryTests

public ActivityFactoryTests()
{
Templates.EnableFromFile = true;

ComponentRegistration.Add(new DialogsComponentRegistration());
ComponentRegistration.Add(new DeclarativeComponentRegistration());
ComponentRegistration.Add(new AdaptiveComponentRegistration());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1818,6 +1818,8 @@ public void TestInjectLGWithoutNamespace()
[Fact]
public void TestFileOperation()
{
Templates.EnableFromFile = true;

var templates = Templates.ParseFile(GetExampleFilePath("FileOperation.lg"));
var evaluated = templates.Evaluate("FromFileWithoutEvaluation");
Assert.Equal("hi ${name}", evaluated);
Expand All @@ -1832,6 +1834,18 @@ public void TestFileOperation()
Assert.Equal("hi ${name}", evaluated);
}

[Fact]
public void TestFileOperationDisabled()
{
Templates.EnableFromFile = false;

Assert.Throws<InvalidOperationException>(() =>
{
var templates = Templates.ParseFile(GetExampleFilePath("FileOperation.lg"));
var evaluated = templates.Evaluate("FromFileWithoutEvaluation");
});
}

public class LoopClass
{
public string Name { get; set; }
Expand Down

0 comments on commit 038309f

Please sign in to comment.