Use nullable

This commit is contained in:
Alexey Golub 2019-11-13 19:19:36 +02:00
parent 1bf9d9e2e2
commit e5a2852165
42 changed files with 195 additions and 196 deletions

View file

@ -6,17 +6,17 @@
{
public string Id { get; }
public string ParentId { get; }
public string? ParentId { get; }
public string GuildId { get; }
public string? GuildId { get; }
public string Name { get; }
public string Topic { get; }
public string? Topic { get; }
public ChannelType Type { get; }
public Channel(string id, string parentId, string guildId, string name, string topic, ChannelType type)
public Channel(string id, string? parentId, string? guildId, string name, string? topic, ChannelType type)
{
Id = id;
ParentId = parentId;

View file

@ -2,10 +2,11 @@
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Tyrrrz.Extensions" Version="1.6.3" />
<PackageReference Include="Tyrrrz.Extensions" Version="1.6.5" />
</ItemGroup>
</Project>

View file

@ -8,28 +8,29 @@ namespace DiscordChatExporter.Core.Models
public class Embed
{
public string Title { get; }
public string? Title { get; }
public string Url { get; }
public string? Url { get; }
public DateTimeOffset? Timestamp { get; }
// TODO: this should be nullable and default color should be set in CSS
public Color Color { get; }
public EmbedAuthor Author { get; }
public EmbedAuthor? Author { get; }
public string Description { get; }
public string? Description { get; }
public IReadOnlyList<EmbedField> Fields { get; }
public EmbedImage Thumbnail { get; }
public EmbedImage? Thumbnail { get; }
public EmbedImage Image { get; }
public EmbedImage? Image { get; }
public EmbedFooter Footer { get; }
public EmbedFooter? Footer { get; }
public Embed(string title, string url, DateTimeOffset? timestamp, Color color, EmbedAuthor author, string description,
IReadOnlyList<EmbedField> fields, EmbedImage thumbnail, EmbedImage image, EmbedFooter footer)
public Embed(string? title, string? url, DateTimeOffset? timestamp, Color color, EmbedAuthor? author, string? description,
IReadOnlyList<EmbedField> fields, EmbedImage? thumbnail, EmbedImage? image, EmbedFooter? footer)
{
Title = title;
Url = url;
@ -43,6 +44,6 @@ namespace DiscordChatExporter.Core.Models
Footer = footer;
}
public override string ToString() => Title;
public override string ToString() => Title ?? "<untitled embed>";
}
}

View file

@ -4,19 +4,19 @@ namespace DiscordChatExporter.Core.Models
public class EmbedAuthor
{
public string Name { get; }
public string? Name { get; }
public string Url { get; }
public string? Url { get; }
public string IconUrl { get; }
public string? IconUrl { get; }
public EmbedAuthor(string name, string url, string iconUrl)
public EmbedAuthor(string? name, string? url, string? iconUrl)
{
Name = name;
Url = url;
IconUrl = iconUrl;
}
public override string ToString() => Name;
public override string ToString() => Name ?? "<unnamed author>";
}
}

View file

@ -6,9 +6,9 @@ namespace DiscordChatExporter.Core.Models
{
public string Text { get; }
public string IconUrl { get; }
public string? IconUrl { get; }
public EmbedFooter(string text, string iconUrl)
public EmbedFooter(string text, string? iconUrl)
{
Text = text;
IconUrl = iconUrl;

View file

@ -4,13 +4,13 @@ namespace DiscordChatExporter.Core.Models
public class EmbedImage
{
public string Url { get; }
public string? Url { get; }
public int? Width { get; }
public int? Height { get; }
public EmbedImage(string url, int? width, int? height)
public EmbedImage(string? url, int? width, int? height)
{
Url = url;
Height = height;

View file

@ -8,7 +8,7 @@ namespace DiscordChatExporter.Core.Models
public partial class Emoji
{
public string Id { get; }
public string? Id { get; }
public string Name { get; }
@ -16,7 +16,7 @@ namespace DiscordChatExporter.Core.Models
public string ImageUrl { get; }
public Emoji(string id, string name, bool isAnimated)
public Emoji(string? id, string name, bool isAnimated)
{
Id = id;
Name = name;
@ -37,10 +37,10 @@ namespace DiscordChatExporter.Core.Models
private static string GetTwemojiName(string emoji) =>
GetCodePoints(emoji).Select(i => i.ToString("x")).JoinToString("-");
public static string GetImageUrl(string id, string name, bool isAnimated)
public static string GetImageUrl(string? id, string name, bool isAnimated)
{
// Custom emoji
if (!id.IsNullOrWhiteSpace())
if (!string.IsNullOrWhiteSpace(id))
{
// Animated
if (isAnimated)

View file

@ -1,8 +1,6 @@
using Tyrrrz.Extensions;
namespace DiscordChatExporter.Core.Models
namespace DiscordChatExporter.Core.Models
{
// https://discordapp.com/developers/docs/resources/guild#guild-object
// https://discordapp.string.IsNullOrWhiteSpace(com/developers/docs/resources/guild#guild-object
public partial class Guild
{
@ -10,11 +8,11 @@ namespace DiscordChatExporter.Core.Models
public string Name { get; }
public string IconHash { get; }
public string? IconHash { get; }
public string IconUrl { get; }
public Guild(string id, string name, string iconHash)
public Guild(string id, string name, string? iconHash)
{
Id = id;
Name = name;
@ -28,9 +26,9 @@ namespace DiscordChatExporter.Core.Models
public partial class Guild
{
public static string GetIconUrl(string id, string iconHash)
public static string GetIconUrl(string id, string? iconHash)
{
return !iconHash.IsNullOrWhiteSpace()
return !string.IsNullOrWhiteSpace(iconHash)
? $"https://cdn.discordapp.com/icons/{id}/{iconHash}.png"
: "https://cdn.discordapp.com/embed/avatars/0.png";
}

View file

@ -19,7 +19,7 @@ namespace DiscordChatExporter.Core.Models
public DateTimeOffset? EditedTimestamp { get; }
public string Content { get; }
public string? Content { get; }
public IReadOnlyList<Attachment> Attachments { get; }
@ -32,7 +32,7 @@ namespace DiscordChatExporter.Core.Models
public bool IsPinned { get; }
public Message(string id, string channelId, MessageType type, User author, DateTimeOffset timestamp,
DateTimeOffset? editedTimestamp, string content, IReadOnlyList<Attachment> attachments,
DateTimeOffset? editedTimestamp, string? content, IReadOnlyList<Attachment> attachments,
IReadOnlyList<Embed> embeds, IReadOnlyList<Reaction> reactions, IReadOnlyList<User> mentionedUsers,
bool isPinned)
{
@ -50,6 +50,6 @@ namespace DiscordChatExporter.Core.Models
IsPinned = isPinned;
}
public override string ToString() => Content;
public override string ToString() => Content ?? "<message without content>";
}
}

View file

@ -1,5 +1,4 @@
using System;
using Tyrrrz.Extensions;
namespace DiscordChatExporter.Core.Models
{
@ -15,13 +14,13 @@ namespace DiscordChatExporter.Core.Models
public string FullName { get; }
public string AvatarHash { get; }
public string? AvatarHash { get; }
public string AvatarUrl { get; }
public bool IsBot { get; }
public User(string id, int discriminator, string name, string avatarHash, bool isBot)
public User(string id, int discriminator, string name, string? avatarHash, bool isBot)
{
Id = id;
Discriminator = discriminator;
@ -40,10 +39,10 @@ namespace DiscordChatExporter.Core.Models
{
public static string GetFullName(string name, int discriminator) => $"{name}#{discriminator:0000}";
public static string GetAvatarUrl(string id, int discriminator, string avatarHash)
public static string GetAvatarUrl(string id, int discriminator, string? avatarHash)
{
// Custom avatar
if (!avatarHash.IsNullOrWhiteSpace())
if (!string.IsNullOrWhiteSpace(avatarHash))
{
// Animated
if (avatarHash.StartsWith("a_", StringComparison.Ordinal))