Skip to content

Commit

Permalink
Adds supports for card Try* null diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
gehongyan committed Mar 19, 2024
1 parent ead8fe6 commit cd80a4a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 13 deletions.
22 changes: 19 additions & 3 deletions src/Kook.Net.CardMarkup/CardMarkupSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
using Kook.CardMarkup.Extensions;
using Kook.CardMarkup.Models;

#if NETSTANDARD2_1 || NET5_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif

namespace Kook.CardMarkup;

/// <summary>
Expand Down Expand Up @@ -242,7 +246,11 @@ public static IEnumerable<ICard> Deserialize(Stream xmlStream)
/// <param name="file">UTF-8 encoded XML file</param>
/// <param name="cards"><see cref="ICard"/> enumerable, will be null if return value is false</param>
/// <returns>True if deserialization is successful, otherwise false</returns>
public static bool TryDeserialize(FileInfo file, out IEnumerable<ICard>? cards)
public static bool TryDeserialize(FileInfo file,
#if NETSTANDARD2_1 || NET5_0_OR_GREATER
[NotNullWhen(true)]
#endif
out IEnumerable<ICard>? cards)
{
try
{
Expand All @@ -262,7 +270,11 @@ public static bool TryDeserialize(FileInfo file, out IEnumerable<ICard>? cards)
/// <param name="xmlText">UTF-8 encoded XML text</param>
/// <param name="cards"><see cref="ICard"/> enumerable, will be null if return value is false</param>
/// <returns>True if deserialization is successful, otherwise false</returns>
public static bool TryDeserialize(string xmlText, out IEnumerable<ICard>? cards)
public static bool TryDeserialize(string xmlText,
#if NETSTANDARD2_1 || NET5_0_OR_GREATER
[NotNullWhen(true)]
#endif
out IEnumerable<ICard>? cards)
{
try
{
Expand All @@ -282,7 +294,11 @@ public static bool TryDeserialize(string xmlText, out IEnumerable<ICard>? cards)
/// <param name="xmlStream">UTF-8 encoded XML stream</param>
/// <param name="cards"><see cref="ICard"/> enumerable, will be null if return value is false</param>
/// <returns>True if deserialization is successful, otherwise false</returns>
public static bool TryDeserialize(Stream xmlStream, out IEnumerable<ICard>? cards)
public static bool TryDeserialize(Stream xmlStream,
#if NETSTANDARD2_1 || NET5_0_OR_GREATER
[NotNullWhen(true)]
#endif
out IEnumerable<ICard>? cards)
{
try
{
Expand Down
32 changes: 26 additions & 6 deletions src/Kook.Net.Core/Extensions/CardExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ public static KMarkdownElementBuilder ToBuilder(this KMarkdownElement entity)
public static ImageElementBuilder ToBuilder(this ImageElement entity)
{
if (entity is null) return null;
return new ImageElementBuilder { Source = entity.Source, Alternative = entity.Alternative, Size = entity.Size, Circle = entity.Circle };
return new ImageElementBuilder
{
Source = entity.Source, Alternative = entity.Alternative, Size = entity.Size, Circle = entity.Circle
};
}

/// <summary>
Expand All @@ -57,7 +60,10 @@ public static ImageElementBuilder ToBuilder(this ImageElement entity)
public static ButtonElementBuilder ToBuilder(this ButtonElement entity)
{
if (entity is null) return null;
return new ButtonElementBuilder { Theme = entity.Theme, Click = entity.Click, Value = entity.Value, Text = entity.Text.ToBuilder() };
return new ButtonElementBuilder
{
Theme = entity.Theme, Click = entity.Click, Value = entity.Value, Text = entity.Text.ToBuilder()
};
}

/// <summary>
Expand All @@ -66,7 +72,10 @@ public static ButtonElementBuilder ToBuilder(this ButtonElement entity)
public static ParagraphStructBuilder ToBuilder(this ParagraphStruct entity)
{
if (entity is null) return null;
return new ParagraphStructBuilder { ColumnCount = entity.ColumnCount, Fields = entity.Fields.Select(x => x.ToBuilder()).ToList() };
return new ParagraphStructBuilder
{
ColumnCount = entity.ColumnCount, Fields = entity.Fields.Select(x => x.ToBuilder()).ToList()
};
}

#endregion
Expand Down Expand Up @@ -112,7 +121,12 @@ public static HeaderModuleBuilder ToBuilder(this HeaderModule entity)
public static SectionModuleBuilder ToBuilder(this SectionModule entity)
{
if (entity is null) return null;
return new SectionModuleBuilder { Mode = entity.Mode, Text = entity.Text.ToBuilder(), Accessory = entity.Accessory.ToBuilder() };
return new SectionModuleBuilder
{
Mode = entity.Mode,
Text = entity.Text.ToBuilder(),
Accessory = entity.Accessory.ToBuilder()
};
}

/// <summary>
Expand Down Expand Up @@ -193,7 +207,10 @@ public static VideoModuleBuilder ToBuilder(this VideoModule entity)
public static CountdownModuleBuilder ToBuilder(this CountdownModule entity)
{
if (entity is null) return null;
return new CountdownModuleBuilder { Mode = entity.Mode, EndTime = entity.EndTime, StartTime = entity.StartTime };
return new CountdownModuleBuilder
{
Mode = entity.Mode, EndTime = entity.EndTime, StartTime = entity.StartTime
};
}

/// <summary>
Expand Down Expand Up @@ -232,7 +249,10 @@ public static CardBuilder ToBuilder(this Card builder)

return new CardBuilder
{
Theme = builder.Theme, Size = builder.Size, Color = builder.Color, Modules = builder.Modules.Select(m => m.ToBuilder()).ToList()
Theme = builder.Theme,
Size = builder.Size,
Color = builder.Color,
Modules = builder.Modules.Select(m => m.ToBuilder()).ToList()
};
}

Expand Down
22 changes: 18 additions & 4 deletions src/Kook.Net.Rest/Extensions/CardJsonExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
using Kook.API;
using Kook.Net.Converters;

#if NETSTANDARD2_1 || NET5_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif

namespace Kook.Rest;

/// <summary>
Expand All @@ -24,9 +28,12 @@ public static class CardJsonExtension
/// <param name="json">The json string to parse.</param>
/// <param name="builder">The <see cref="ICardBuilder"/> with populated values. An empty instance if method returns <c>false</c>.</param>
/// <returns><c>true</c> if <paramref name="json"/> was successfully parsed. <c>false</c> if not.</returns>
public static bool TryParseSingle(string json, out ICardBuilder builder)
public static bool TryParseSingle(string json,
#if NETSTANDARD2_1 || NET5_0_OR_GREATER
[NotNullWhen(true)]
#endif
out ICardBuilder builder)
{
builder = new CardBuilder();
try
{
CardBase model = JsonSerializer.Deserialize<CardBase>(json, _options.Value);
Expand All @@ -37,10 +44,12 @@ public static bool TryParseSingle(string json, out ICardBuilder builder)
return true;
}

builder = new CardBuilder();
return false;
}
catch
{
builder = new CardBuilder();
return false;
}
}
Expand All @@ -51,9 +60,12 @@ public static bool TryParseSingle(string json, out ICardBuilder builder)
/// <param name="json">The json string to parse.</param>
/// <param name="builders">A collection of <see cref="ICardBuilder"/> with populated values. An empty instance if method returns <c>false</c>.</param>
/// <returns><c>true</c> if <paramref name="json"/> was successfully parsed. <c>false</c> if not.</returns>
public static bool TryParseMany(string json, out IEnumerable<ICardBuilder> builders)
public static bool TryParseMany(string json,
#if NETSTANDARD2_1 || NET5_0_OR_GREATER
[NotNullWhen(true)]
#endif
out IEnumerable<ICardBuilder> builders)
{
builders = Enumerable.Empty<ICardBuilder>();
try
{
IEnumerable<CardBase> models = JsonSerializer.Deserialize<IEnumerable<CardBase>>(json, _options.Value);
Expand All @@ -64,10 +76,12 @@ public static bool TryParseMany(string json, out IEnumerable<ICardBuilder> build
return true;
}

builders = null;
return false;
}
catch
{
builders = null;
return false;
}
}
Expand Down

0 comments on commit cd80a4a

Please sign in to comment.