microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Api/Activities/Invokes/ConfigActivity.cs
80lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Text.Json; |
| 5 | using System.Text.Json.Serialization; |
| 6 | |
| 7 | using Microsoft.Teams.Common; |
| 8 | |
| 9 | namespace Microsoft.Teams.Api.Activities.Invokes; |
| 10 | |
| 11 | public partial class Name : StringEnum |
| 12 | { |
| 13 | public bool IsConfig => Value.StartsWith("config/"); |
| 14 | } |
| 15 | |
| 16 | /// <summary> |
| 17 | /// Any Config Activity |
| 18 | /// </summary> |
| 19 | [JsonConverter(typeof(JsonConverter))] |
| 20 | public abstract class ConfigActivity(Name.Configs name) : InvokeActivity(new(name.Value)) |
| 21 | { |
| 22 | public Configs.FetchActivity ToFetch() => (Configs.FetchActivity)this; |
| 23 | public Configs.SubmitActivity ToSubmit() => (Configs.SubmitActivity)this; |
| 24 | |
| 25 | public override object ToType(Type type, IFormatProvider? provider) |
| 26 | { |
| 27 | if (type == typeof(Configs.FetchActivity)) return ToFetch(); |
| 28 | if (type == typeof(Configs.SubmitActivity)) return ToSubmit(); |
| 29 | return this; |
| 30 | } |
| 31 | |
| 32 | public new class JsonConverter : JsonConverter<ConfigActivity> |
| 33 | { |
| 34 | public override bool CanConvert(Type typeToConvert) |
| 35 | { |
| 36 | return base.CanConvert(typeToConvert); |
| 37 | } |
| 38 | |
| 39 | public override ConfigActivity? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 40 | { |
| 41 | var element = JsonSerializer.Deserialize<JsonElement>(ref reader, options); |
| 42 | |
| 43 | if (!element.TryGetProperty("name", out JsonElement property)) |
| 44 | { |
| 45 | throw new JsonException("invoke activity must have a 'name' property"); |
| 46 | } |
| 47 | |
| 48 | var name = property.Deserialize<string>(options); |
| 49 | |
| 50 | if (name is null) |
| 51 | { |
| 52 | throw new JsonException("failed to deserialize invoke activity 'name' property"); |
| 53 | } |
| 54 | |
| 55 | return name switch |
| 56 | { |
| 57 | "config/fetch" => JsonSerializer.Deserialize<Configs.FetchActivity>(element.ToString(), options), |
| 58 | "config/submit" => JsonSerializer.Deserialize<Configs.SubmitActivity>(element.ToString(), options), |
| 59 | _ => throw new JsonException($"failed to deserialize config activity '{name}' doesn't match any known types.") |
| 60 | }; |
| 61 | } |
| 62 | |
| 63 | public override void Write(Utf8JsonWriter writer, ConfigActivity value, JsonSerializerOptions options) |
| 64 | { |
| 65 | if (value is Configs.FetchActivity fetch) |
| 66 | { |
| 67 | JsonSerializer.Serialize(writer, fetch, options); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | if (value is Configs.SubmitActivity submit) |
| 72 | { |
| 73 | JsonSerializer.Serialize(writer, submit, options); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | JsonSerializer.Serialize(writer, value, options); |
| 78 | } |
| 79 | } |
| 80 | } |