Skip to content

Commit

Permalink
Simplify color conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
gehongyan committed Feb 24, 2023
1 parent bff75ed commit 52aec27
Show file tree
Hide file tree
Showing 17 changed files with 79 additions and 77 deletions.
3 changes: 2 additions & 1 deletion src/Kook.Net.Rest/API/Common/Cards/Card.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ internal class Card : CardBase
public CardTheme Theme { get; set; }

[JsonPropertyName("color")]
[JsonConverter(typeof(NullableColorConverter))]
[JsonConverter(typeof(HexColorConverter))]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public Color? Color { get; set; }

[JsonPropertyName("size")]
Expand Down
2 changes: 1 addition & 1 deletion src/Kook.Net.Rest/API/Common/Embeds/LinkEmbed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class LinkEmbed : EmbedBase
public string SiteName { get; set; }

[JsonPropertyName("theme_color")]
[JsonConverter(typeof(ColorConverter))]
[JsonConverter(typeof(HexColorConverter))]
public Color Color { get; set; }

[JsonPropertyName("image")]
Expand Down
19 changes: 0 additions & 19 deletions src/Kook.Net.Rest/API/Common/MentionRole.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Kook.Net.Rest/API/Common/Pokes/PokeQualityResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Kook.API;
internal class PokeQualityResource
{
[JsonPropertyName("color")]
[JsonConverter(typeof(ColorConverter))]
[JsonConverter(typeof(HexColorConverter))]
public Color Color { get; set; }
[JsonPropertyName("small")]
public string Small { get; set; }
Expand Down
33 changes: 25 additions & 8 deletions src/Kook.Net.Rest/API/Common/Role.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
using Kook.Net.Converters;
using System.Text.Json.Serialization;

namespace Kook.API;

internal class Role
{
[JsonPropertyName("role_id")] public uint Id { get; set; }
[JsonPropertyName("name")] public string Name { get; set; }
[JsonPropertyName("color")] public uint Color { get; set; }
[JsonPropertyName("position")] public int Position { get; set; }
[JsonPropertyName("hoist")] public int Hoist { get; set; }
[JsonPropertyName("mentionable")] public int Mentionable { get; set; }
[JsonPropertyName("permissions")] public ulong Permissions { get; set; }
[JsonPropertyName("type")] public RoleType? Type { get; set; }
[JsonPropertyName("role_id")]
public uint Id { get; set; }

[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("color")]
[JsonConverter(typeof(RawValueColorConverter))]
public Color Color { get; set; }

[JsonPropertyName("position")]
public int Position { get; set; }

[JsonPropertyName("hoist")]
public int Hoist { get; set; }

[JsonPropertyName("mentionable")]
public int Mentionable { get; set; }

[JsonPropertyName("permissions")]
public ulong Permissions { get; set; }

[JsonPropertyName("type")]
public RoleType? Type { get; set; }
}
11 changes: 0 additions & 11 deletions src/Kook.Net.Rest/API/Common/Tag.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Kook.Net.Rest/API/Common/UserTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Kook.API;
internal class UserTag
{
[JsonPropertyName("color")]
[JsonConverter(typeof(ColorConverter))]
[JsonConverter(typeof(HexColorConverter))]
public Color Color { get; set; }

[JsonPropertyName("text")]
Expand Down
30 changes: 22 additions & 8 deletions src/Kook.Net.Rest/API/Rest/GuildMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ namespace Kook.API.Rest;

internal class GuildMember : User
{
[JsonPropertyName("nickname")] public string Nickname { get; set; }
[JsonPropertyName("mobile_verified")] public bool MobileVerified { get; set; }
[JsonPropertyName("nickname")]
public string Nickname { get; set; }

[JsonPropertyName("mobile_verified")]
public bool MobileVerified { get; set; }

[JsonPropertyName("joined_at")]
[JsonConverter(typeof(DateTimeOffsetUnixTimeMillisecondsConverter))]
Expand All @@ -16,9 +19,15 @@ internal class GuildMember : User
[JsonConverter(typeof(DateTimeOffsetUnixTimeMillisecondsConverter))]
public DateTimeOffset ActiveAt { get; set; }

[JsonPropertyName("hoist_info")] public HoistInfo HoistInfo { get; set; }
[JsonPropertyName("color")] public uint Color { get; set; }
[JsonPropertyName("roles")] public uint[] Roles { get; set; }
[JsonPropertyName("hoist_info")]
public HoistInfo HoistInfo { get; set; }

[JsonPropertyName("color")]
[JsonConverter(typeof(RawValueColorConverter))]
public Color Color { get; set; }

[JsonPropertyName("roles")]
public uint[] Roles { get; set; }

[JsonPropertyName("is_master")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
Expand All @@ -34,7 +43,12 @@ internal class GuildMember : User

internal class HoistInfo
{
[JsonPropertyName("role_id")] public uint RoleId { get; set; }
[JsonPropertyName("name")] public string Name { get; set; }
[JsonPropertyName("color")] public uint Color { get; set; }
[JsonPropertyName("role_id")]
public uint RoleId { get; set; }

[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("color")]
public uint Color { get; set; }
}
4 changes: 3 additions & 1 deletion src/Kook.Net.Rest/API/Rest/ModifyGuildRoleParams.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Kook.Net.Converters;
using System.Text.Json.Serialization;

namespace Kook.API.Rest;
Expand All @@ -15,8 +16,9 @@ internal class ModifyGuildRoleParams
public string Name { get; set; }

[JsonPropertyName("color")]
[JsonConverter(typeof(RawValueColorConverter))]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public uint? Color { get; set; }
public Color? Color { get; set; }

[JsonPropertyName("hoist")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
Expand Down
2 changes: 1 addition & 1 deletion src/Kook.Net.Rest/Entities/Roles/RestRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ internal void Update(Model model)
IsHoisted = model.Hoist == 1;
IsMentionable = model.Mentionable == 1;
Position = model.Position;
Color = new Color(model.Color);
Color = model.Color;
Permissions = new GuildPermissions(model.Permissions);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Kook.Net.Rest/Entities/Roles/RoleHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static async Task<Model> ModifyAsync(IRole role, BaseKookClient client,
GuildId = role.Guild.Id,
RoleId = role.Id,
Name = args.Name,
Color = args.Color?.RawValue,
Color = args.Color,
Hoist = args.Hoist switch
{
true => 1,
Expand Down
2 changes: 1 addition & 1 deletion src/Kook.Net.Rest/Entities/Users/RestGuildUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ internal void Update(MemberModel model)
IsMobileVerified = model.MobileVerified;
JoinedAt = model.JoinedAt;
ActiveAt = model.ActiveAt;
Color = new Color(model.Color);
Color = model.Color;
IsOwner = model.IsOwner;
UpdateRoles(model.Roles);
}
Expand Down
20 changes: 0 additions & 20 deletions src/Kook.Net.Rest/Net/Converters/Cards/ColorConverter.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Kook.Net.Converters;

internal class ColorConverter : JsonConverter<Color>
internal class HexColorConverter : JsonConverter<Color>
{
public override Color Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Expand Down
18 changes: 18 additions & 0 deletions src/Kook.Net.Rest/Net/Converters/Cards/RawValueColorConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Kook.Net.Converters;

internal class RawValueColorConverter : JsonConverter<Color>
{
public override Color Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
uint rawValue = reader.GetUInt32();
return new Color(rawValue);
}

public override void Write(Utf8JsonWriter writer, Color value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value.RawValue);
}
}
2 changes: 1 addition & 1 deletion src/Kook.Net.WebSocket/Entities/Roles/SocketRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ internal void Update(ClientState state, Model model)
{
Name = model.Name;
Type = model.Type;
Color = new Color(model.Color);
Color = model.Color;
Position = model.Position;
IsHoisted = model.Hoist switch
{
Expand Down
2 changes: 1 addition & 1 deletion src/Kook.Net.WebSocket/Entities/Users/SocketGuildUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ internal void Update(ClientState state, MemberModel model)
IsMobileVerified = model.MobileVerified;
JoinedAt = model.JoinedAt;
ActiveAt = model.ActiveAt;
Color = new Color(model.Color);
Color = model.Color;
IsOwner = model.IsOwner;
UpdateRoles(model.Roles);
}
Expand Down

0 comments on commit 52aec27

Please sign in to comment.