Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
fix(fx): fix async command method returning string not work
Browse files Browse the repository at this point in the history
  • Loading branch information
bsdayo committed Apr 9, 2023
1 parent fcaab1d commit b025922
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/Flandre.Core/Messaging/MessageContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ public static implicit operator MessageContent(MessageSegment segment)
/// <summary>
/// 由字符串隐式转换
/// </summary>
public static implicit operator MessageContent(string text)
public static implicit operator MessageContent(string? text)
{
return new MessageContent(new[] { new TextSegment(text) });
return text is null
? new MessageContent(Array.Empty<MessageSegment>())
: new MessageContent(new[] { new TextSegment(text) });
}

/// <summary>
Expand Down
14 changes: 9 additions & 5 deletions src/Flandre.Framework/Common/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,16 @@ public Command AddSubCommand(string path)
var cmdResult = InnerMethod?.Invoke(plugin, args.ToArray());
var content = cmdResult switch
{
Task<MessageContent?> task => await task,
ValueTask<MessageContent?> valueTask => await valueTask,
MessageContent msgContent => msgContent,
MessageContent mc => mc,
Task<MessageContent?> mcTask => await mcTask,
ValueTask<MessageContent?> mcValueTask => await mcValueTask,

string str => (MessageContent)str,
MessageBuilder msgBuilder => (MessageContent)msgBuilder,
MessageBuilder mb => mb.Build(),
Task<MessageBuilder> mbTask => (MessageContent)await mbTask,
ValueTask<MessageBuilder> mbValueTask => (MessageContent)await mbValueTask,

Task<string> strTask => (MessageContent)await strTask,
ValueTask<string> strValueTask => (MessageContent)await strValueTask,

_ => cmdResult?.ToString() is { } val ? (MessageContent)val : null
};
Expand Down
14 changes: 13 additions & 1 deletion tests/Flandre.Framework.Tests/CommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ public static string Test6(double arg, params string[] strArr)
.Append(string.Join(',', strArr))
.ToString();
}

[Command]
public static async ValueTask<string> TestAsync()
{
// simulates async tasks
await Task.Run(() => { });
return "ok!";
}
}

[Fact]
Expand All @@ -79,7 +87,7 @@ public async Task TestCommands()

var service = app.Services.GetRequiredService<CommandService>();

Assert.Equal(6, service.RootCommandNode.CountCommands());
Assert.Equal(7, service.RootCommandNode.CountCommands());

await app.StartWithDefaultsAsync();

Expand All @@ -95,6 +103,10 @@ public async Task TestCommands()
Assert.Equal("123 191.981 False True",
content?.GetText());

// test async
content = await friendClient.SendMessageForReply("testasync");
Assert.Equal("ok!", content?.GetText());

await app.StopAsync();
}

Expand Down

0 comments on commit b025922

Please sign in to comment.