Skip to content

Commit

Permalink
Style: Async method nomenclature, missing ConfigureAwait and Cancella…
Browse files Browse the repository at this point in the history
…tionTokens
  • Loading branch information
carlosscastro committed May 8, 2020
1 parent f3b5339 commit 3daaf46
Show file tree
Hide file tree
Showing 41 changed files with 170 additions and 150 deletions.
1 change: 1 addition & 0 deletions libraries/AdaptiveExpressions/AdaptiveExpressions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AsyncUsageAnalyzers" Version="1.0.0-alpha003" PrivateAssets="all" />
<PackageReference Include="Antlr4.CodeGenerator" Version="4.6.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public DeleteProperties([CallerFilePath] string callerPath = "", [CallerLineNumb
}
}

return await dc.EndDialogAsync();
return await dc.EndDialogAsync(cancellationToken).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public DeleteProperty(string property, [CallerFilePath] string callerPath = "",
}

dc.State.RemoveValue(Property.GetValue(dc.State));
return await dc.EndDialogAsync();
return await dc.EndDialogAsync().ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public enum ArrayChangeType
dc.State.SetValue(this.ResultProperty.GetValue(dc.State), result);
}

return await dc.EndDialogAsync(result);
return await dc.EndDialogAsync(result).ConfigureAwait(false);
}

protected override string OnComputeId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public enum HttpMethod
// Bind each string token to the data in state
if (instanceBody != null)
{
await ReplaceJTokenRecursively(dc, instanceBody);
await ReplaceJTokenRecursivelyAsync(dc, instanceBody, cancellationToken).ConfigureAwait(false);
}

// Set headers
Expand All @@ -243,14 +243,14 @@ public enum HttpMethod
case HttpMethod.POST:
if (instanceBody == null)
{
response = await client.PostAsync(instanceUrl, null);
response = await client.PostAsync(instanceUrl, content: null, cancellationToken: cancellationToken).ConfigureAwait(false);
}
else
{
var postContent = new StringContent(instanceBody.ToString(), Encoding.UTF8, contentType);
traceInfo.request.content = instanceBody.ToString();
traceInfo.request.headers = JObject.FromObject(postContent?.Headers.ToDictionary(t => t.Key, t => (object)t.Value?.FirstOrDefault()));
response = await client.PostAsync(instanceUrl, postContent);
response = await client.PostAsync(instanceUrl, postContent, cancellationToken).ConfigureAwait(false);
}

break;
Expand All @@ -259,40 +259,40 @@ public enum HttpMethod
if (instanceBody == null)
{
var request = new HttpRequestMessage(new System.Net.Http.HttpMethod("PATCH"), instanceUrl);
response = await client.SendAsync(request);
response = await client.SendAsync(request, cancellationToken).ConfigureAwait(false);
}
else
{
var request = new HttpRequestMessage(new System.Net.Http.HttpMethod("PATCH"), instanceUrl);
request.Content = new StringContent(instanceBody.ToString(), Encoding.UTF8, contentType);
traceInfo.request.content = instanceBody.ToString();
traceInfo.request.headers = JObject.FromObject(request.Content.Headers.ToDictionary(t => t.Key, t => (object)t.Value?.FirstOrDefault()));
response = await client.SendAsync(request);
response = await client.SendAsync(request, cancellationToken).ConfigureAwait(false);
}

break;

case HttpMethod.PUT:
if (instanceBody == null)
{
response = await client.PutAsync(instanceUrl, null);
response = await client.PutAsync(instanceUrl, content: null, cancellationToken: cancellationToken).ConfigureAwait(false);
}
else
{
var putContent = new StringContent(instanceBody.ToString(), Encoding.UTF8, contentType);
traceInfo.request.content = instanceBody.ToString();
traceInfo.request.headers = JObject.FromObject(putContent.Headers.ToDictionary(t => t.Key, t => (object)t.Value?.FirstOrDefault()));
response = await client.PutAsync(instanceUrl, putContent);
response = await client.PutAsync(instanceUrl, putContent, cancellationToken).ConfigureAwait(false);
}

break;

case HttpMethod.DELETE:
response = await client.DeleteAsync(instanceUrl);
response = await client.DeleteAsync(instanceUrl, cancellationToken).ConfigureAwait(false);
break;

case HttpMethod.GET:
response = await client.GetAsync(instanceUrl);
response = await client.GetAsync(instanceUrl, cancellationToken).ConfigureAwait(false);
break;
}

Expand All @@ -302,7 +302,7 @@ public enum HttpMethod
ReasonPhrase = response.ReasonPhrase,
};

object content = (object)await response.Content.ReadAsStringAsync();
object content = (object)await response.Content.ReadAsStringAsync().ConfigureAwait(false);

switch (this.ResponseType.GetValue(dc.State))
{
Expand Down Expand Up @@ -348,22 +348,22 @@ public enum HttpMethod
}

// return the actionResult as the result of this operation
return await dc.EndDialogAsync(result: requestResult, cancellationToken: cancellationToken);
return await dc.EndDialogAsync(result: requestResult, cancellationToken: cancellationToken).ConfigureAwait(false);
}

protected override string OnComputeId()
{
return $"{this.GetType().Name}[{Method} {Url?.ToString()}]";
}

private async Task ReplaceJTokenRecursively(DialogContext dc, JToken token)
private async Task ReplaceJTokenRecursivelyAsync(DialogContext dc, JToken token, CancellationToken cancellationToken = default(CancellationToken))
{
switch (token.Type)
{
case JTokenType.Object:
foreach (var child in token.Children<JProperty>())
{
await ReplaceJTokenRecursively(dc, child);
await ReplaceJTokenRecursivelyAsync(dc, child, cancellationToken).ConfigureAwait(false);
}

break;
Expand All @@ -372,13 +372,13 @@ private async Task ReplaceJTokenRecursively(DialogContext dc, JToken token)
// NOTE: ToList() is required because JToken.Replace will break the enumeration.
foreach (var child in token.Children().ToList())
{
await ReplaceJTokenRecursively(dc, child);
await ReplaceJTokenRecursivelyAsync(dc, child, cancellationToken).ConfigureAwait(false);
}

break;

case JTokenType.Property:
await ReplaceJTokenRecursively(dc, ((JProperty)token).Value);
await ReplaceJTokenRecursivelyAsync(dc, ((JProperty)token).Value, cancellationToken).ConfigureAwait(false);
break;

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public override async Task<DialogTurnResult> BeginDialogAsync(DialogContext dc,

EnsureDependenciesInstalled();

await this.CheckForVersionChangeAsync(dc).ConfigureAwait(false);
await this.CheckForVersionChangeAsync(dc, cancellationToken).ConfigureAwait(false);

if (!dc.State.ContainsKey(DialogPath.EventCounter))
{
Expand Down Expand Up @@ -217,10 +217,10 @@ public override async Task<DialogTurnResult> ContinueDialogAsync(DialogContext d
{
EnsureDependenciesInstalled();

await this.CheckForVersionChangeAsync(dc).ConfigureAwait(false);
await this.CheckForVersionChangeAsync(dc, cancellationToken).ConfigureAwait(false);

// Continue step execution
return await ContinueActionsAsync(dc, null, cancellationToken).ConfigureAwait(false);
return await ContinueActionsAsync(dc, options: null, cancellationToken).ConfigureAwait(false);
}

public override async Task<DialogTurnResult> ResumeDialogAsync(DialogContext dc, DialogReason reason, object result = null, CancellationToken cancellationToken = default)
Expand All @@ -230,14 +230,14 @@ public override async Task<DialogTurnResult> ResumeDialogAsync(DialogContext dc,
throw new ArgumentException($"{nameof(result)} cannot be a cancellation token");
}

await this.CheckForVersionChangeAsync(dc).ConfigureAwait(false);
await this.CheckForVersionChangeAsync(dc, cancellationToken).ConfigureAwait(false);

// Containers are typically leaf nodes on the stack but the dev is free to push other dialogs
// on top of the stack which will result in the container receiving an unexpected call to
// resumeDialog() when the pushed on dialog ends.
// To avoid the container prematurely ending we need to implement this method and simply
// ask our inner dialog stack to re-prompt.
await RepromptDialogAsync(dc.Context, dc.ActiveDialog).ConfigureAwait(false);
await RepromptDialogAsync(dc.Context, dc.ActiveDialog, cancellationToken).ConfigureAwait(false);

return EndOfTurn;
}
Expand Down Expand Up @@ -464,7 +464,7 @@ protected override async Task<bool> OnPostBubbleEventAsync(DialogContext dc, Dia
if (activity.Type == ActivityTypes.Message)
{
// Recognize utterance
var recognizedResult = await OnRecognize(actionContext, activity, cancellationToken).ConfigureAwait(false);
var recognizedResult = await OnRecognizeAsync(actionContext, activity, cancellationToken).ConfigureAwait(false);

// TODO figure out way to not use turn state to pass this value back to caller.
actionContext.State.SetValue(TurnPath.Recognized, recognizedResult);
Expand Down Expand Up @@ -681,7 +681,7 @@ protected async Task<DialogTurnResult> OnEndOfActionsAsync(ActionContext actionC
return new DialogTurnResult(DialogTurnStatus.Cancelled);
}

protected async Task<RecognizerResult> OnRecognize(ActionContext actionContext, Activity activity, CancellationToken cancellationToken = default)
protected async Task<RecognizerResult> OnRecognizeAsync(ActionContext actionContext, Activity activity, CancellationToken cancellationToken = default)
{
if (Recognizer != null)
{
Expand Down Expand Up @@ -823,7 +823,7 @@ private async Task<bool> ProcessQueuesAsync(ActionContext actionContext, Cancell
if (nextAssignment != null && nextAssignment.Event != AdaptiveEvents.AssignEntity)
{
assignments.Dequeue(actionContext);
handled = await this.ProcessQueuesAsync(actionContext, cancellationToken);
handled = await this.ProcessQueuesAsync(actionContext, cancellationToken).ConfigureAwait(false);
}
}

Expand All @@ -837,7 +837,7 @@ private string GetUniqueInstanceId(DialogContext dc)

private async Task<bool> QueueFirstMatchAsync(ActionContext actionContext, DialogEvent dialogEvent, bool preBubble, CancellationToken cancellationToken)
{
var selection = await Selector.Select(actionContext, cancellationToken).ConfigureAwait(false);
var selection = await Selector.SelectAsync(actionContext, cancellationToken).ConfigureAwait(false);
if (selection.Any())
{
var condition = selection.First();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public MultiLanguageGeneratorBase()
/// <param name="template">The template.</param>
/// <param name="data">data to bind to.</param>
/// <returns>The generator.</returns>
public override async Task<object> Generate(DialogContext dialogContext, string template, object data)
public override async Task<object> GenerateAsync(DialogContext dialogContext, string template, object data)
{
var targetLocale = dialogContext.Context.Activity.Locale?.ToLower() ?? string.Empty;

Expand Down Expand Up @@ -89,7 +89,7 @@ public override async Task<object> Generate(DialogContext dialogContext, string
{
try
{
return await generator.Generate(dialogContext, template, data);
return await generator.GenerateAsync(dialogContext, template, data).ConfigureAwait(false);
}
catch (Exception err)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ public TemplateEngineLanguageGenerator(string filePath, Dictionary<string, IList
/// <param name="template">template to evaluate.</param>
/// <param name="data">data to bind to.</param>
/// <returns>generated text.</returns>
public override async Task<object> Generate(DialogContext dialogContext, string template, object data)
public override Task<object> GenerateAsync(DialogContext dialogContext, string template, object data)
{
try
{
return await Task.FromResult(lg.EvaluateText(template, data));
return Task.FromResult(lg.EvaluateText(template, data));
}
catch (Exception err)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public AttachmentInput([CallerFilePath] string callerPath = "", [CallerLineNumbe
[JsonProperty("outputFormat")]
public EnumExpression<AttachmentOutputFormat> OutputFormat { get; set; } = AttachmentOutputFormat.First;

protected override Task<InputState> OnRecognizeInput(DialogContext dc)
protected override Task<InputState> OnRecognizeInputAsync(DialogContext dc)
{
var input = dc.State.GetValue<List<Attachment>>(VALUE_PROPERTY);
var first = input.Count > 0 ? input[0] : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ protected override object OnInitializeOptions(DialogContext dc, object options)
return base.OnInitializeOptions(dc, op);
}

protected override Task<InputState> OnRecognizeInput(DialogContext dc)
protected override Task<InputState> OnRecognizeInputAsync(DialogContext dc)
{
var input = dc.State.GetValue<object>(VALUE_PROPERTY);
var options = dc.State.GetValue<ChoiceInputOptions>(ThisPath.Options);
Expand Down Expand Up @@ -174,10 +174,10 @@ protected override Task<InputState> OnRecognizeInput(DialogContext dc)
return Task.FromResult(InputState.Valid);
}

protected override async Task<IActivity> OnRenderPrompt(DialogContext dc, InputState state)
protected override async Task<IActivity> OnRenderPromptAsync(DialogContext dc, InputState state)
{
var locale = GetCulture(dc);
var prompt = await base.OnRenderPrompt(dc, state);
var prompt = await base.OnRenderPromptAsync(dc, state).ConfigureAwait(false);
var channelId = dc.Context.Activity.ChannelId;
var choicePrompt = new ChoicePrompt(this.Id);
var choiceOptions = this.ChoiceOptions?.GetValue(dc.State) ?? ChoiceInput.DefaultChoiceOptions[locale];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public ConfirmInput([CallerFilePath] string callerPath = "", [CallerLineNumber]
[JsonProperty("outputFormat")]
public ValueExpression OutputFormat { get; set; }

protected override Task<InputState> OnRecognizeInput(DialogContext dc)
protected override Task<InputState> OnRecognizeInputAsync(DialogContext dc)
{
var input = dc.State.GetValue<object>(VALUE_PROPERTY);
if (dc.Context.Activity.Type == ActivityTypes.Message)
Expand Down Expand Up @@ -139,7 +139,7 @@ protected override Task<InputState> OnRecognizeInput(DialogContext dc)
return Task.FromResult(InputState.Valid);
}

protected override async Task<IActivity> OnRenderPrompt(DialogContext dc, InputState state)
protected override async Task<IActivity> OnRenderPromptAsync(DialogContext dc, InputState state)
{
// Format prompt to send
var channelId = dc.Context.Activity.ChannelId;
Expand All @@ -148,7 +148,7 @@ protected override async Task<IActivity> OnRenderPrompt(DialogContext dc, InputS
var choiceOptions = ChoiceOptions?.GetValue(dc.State) ?? defaults.Item3;
var confirmChoices = ConfirmChoices?.GetValue(dc.State) ?? new List<Choice>() { defaults.Item1, defaults.Item2 };

var prompt = await base.OnRenderPrompt(dc, state);
var prompt = await base.OnRenderPromptAsync(dc, state).ConfigureAwait(false);
var (style, error) = this.Style.TryGetValue(dc.State);
return this.AppendChoices(prompt.AsMessageActivity(), channelId, confirmChoices, style, choiceOptions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public DateTimeInput([CallerFilePath] string callerPath = "", [CallerLineNumber]
[JsonProperty("outputFormat")]
public Expression OutputFormat { get; set; }

protected override Task<InputState> OnRecognizeInput(DialogContext dc)
protected override Task<InputState> OnRecognizeInputAsync(DialogContext dc)
{
var input = dc.State.GetValue<object>(VALUE_PROPERTY);
var culture = GetCulture(dc);
Expand Down
Loading

0 comments on commit 3daaf46

Please sign in to comment.