-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathActionResult.cs
More file actions
59 lines (48 loc) · 1.85 KB
/
ActionResult.cs
File metadata and controls
59 lines (48 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Text.Json;
using System.Text.Json.Serialization;
namespace autoShell;
/// <summary>
/// Represents the result of executing an action.
/// Serialized to JSON and written to stdout as the response to the caller.
/// </summary>
internal class ActionResult
{
[JsonPropertyName("id")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string Id { get; set; }
[JsonPropertyName("success")]
public bool Success { get; init; }
[JsonPropertyName("message")]
public string Message { get; init; }
[JsonPropertyName("data")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public JsonElement? Data { get; init; }
/// <summary>
/// When true, the caller should exit the interactive loop after sending this result.
/// Not serialized — this is internal control flow only.
/// </summary>
[JsonIgnore]
public bool IsQuit { get; init; }
/// <summary>
/// Creates a successful result with a message.
/// </summary>
public static ActionResult Ok(string message) =>
new() { Success = true, Message = message };
/// <summary>
/// Creates a successful result with a message and associated data.
/// </summary>
public static ActionResult Ok(string message, JsonElement data) =>
new() { Success = true, Message = message, Data = data };
/// <summary>
/// Creates a failure result with an error message.
/// </summary>
public static ActionResult Fail(string message) =>
new() { Success = false, Message = message };
/// <summary>
/// Creates a successful quit result that signals the interactive loop to exit.
/// </summary>
public static ActionResult Quit() =>
new() { Success = true, Message = "Quitting", IsQuit = true };
}