-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e87e42e
commit 24cc656
Showing
113 changed files
with
607 additions
and
298 deletions.
There are no files selected for viewing
14 changes: 0 additions & 14 deletions
14
src/Octokit.Webhooks/Converter/JsonStringEnumMemberConverterWithFallback.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace Octokit.Webhooks.Converter; | ||
|
||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Octokit.Webhooks.Extensions; | ||
|
||
public sealed class StringEnumConverter<TEnum> : JsonConverter<StringEnum<TEnum>> | ||
where TEnum : struct | ||
{ | ||
public override StringEnum<TEnum> Read( | ||
ref Utf8JsonReader reader, | ||
Type typeToConvert, | ||
JsonSerializerOptions options) => new(reader.GetString()!); | ||
|
||
public override void Write(Utf8JsonWriter writer, StringEnum<TEnum> value, JsonSerializerOptions options) => | ||
JsonSerializer.Serialize(writer, value.StringValue, options); | ||
} |
64 changes: 64 additions & 0 deletions
64
src/Octokit.Webhooks/Converter/StringEnumEnumerableConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
namespace Octokit.Webhooks.Converter; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Octokit.Webhooks.Extensions; | ||
|
||
public sealed class StringEnumEnumerableConverter<TEnum> : JsonConverter<IEnumerable<StringEnum<TEnum>>> | ||
where TEnum : struct | ||
{ | ||
public override IEnumerable<StringEnum<TEnum>> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => | ||
ReadInternal(ref reader, options); | ||
|
||
public override void Write(Utf8JsonWriter writer, IEnumerable<StringEnum<TEnum>> value, JsonSerializerOptions options) => | ||
WriteInternal(writer, value, options); | ||
|
||
internal static IEnumerable<StringEnum<TEnum>> ReadInternal(ref Utf8JsonReader reader, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType == JsonTokenType.Null) | ||
{ | ||
throw new JsonException("Unexpected null value."); | ||
} | ||
|
||
var jsonSerializerOptions = new JsonSerializerOptions(options); | ||
jsonSerializerOptions.Converters.Clear(); | ||
jsonSerializerOptions.Converters.Add(Activator.CreateInstance<StringEnumConverter<TEnum>>()); | ||
|
||
var returnValue = new List<StringEnum<TEnum>>(); | ||
|
||
while (reader.TokenType != JsonTokenType.EndArray) | ||
{ | ||
if (reader.TokenType != JsonTokenType.StartArray) | ||
{ | ||
returnValue.Add((StringEnum<TEnum>)JsonSerializer.Deserialize(ref reader, typeof(StringEnum<TEnum>), jsonSerializerOptions)!); | ||
} | ||
|
||
reader.Read(); | ||
} | ||
|
||
return returnValue!; | ||
} | ||
|
||
internal static void WriteInternal(Utf8JsonWriter writer, IEnumerable<StringEnum<TEnum>> value, JsonSerializerOptions options) | ||
{ | ||
if (value == null) | ||
{ | ||
throw new JsonException("Unexpected null value."); | ||
} | ||
|
||
var jsonSerializerOptions = new JsonSerializerOptions(options); | ||
jsonSerializerOptions.Converters.Clear(); | ||
jsonSerializerOptions.Converters.Add(Activator.CreateInstance<StringEnumConverter<TEnum>>()); | ||
|
||
writer.WriteStartArray(); | ||
|
||
foreach (var data in value) | ||
{ | ||
JsonSerializer.Serialize(writer, data, jsonSerializerOptions); | ||
} | ||
|
||
writer.WriteEndArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
namespace Octokit.Webhooks.Extensions; | ||
|
||
using System; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Runtime.Serialization; | ||
using JetBrains.Annotations; | ||
|
||
[PublicAPI] | ||
public sealed record StringEnum<TEnum> | ||
where TEnum : struct | ||
{ | ||
private TEnum? parsedValue; | ||
|
||
public StringEnum(string stringValue) | ||
{ | ||
this.StringValue = stringValue; | ||
this.parsedValue = null; | ||
} | ||
|
||
public StringEnum(TEnum parsedValue) | ||
{ | ||
if (!Enum.IsDefined(typeof(TEnum), parsedValue)) | ||
{ | ||
throw GetArgumentException(parsedValue.ToString()); | ||
} | ||
|
||
this.StringValue = ToEnumString(parsedValue); | ||
this.parsedValue = parsedValue; | ||
} | ||
|
||
public string StringValue { get; } | ||
|
||
public TEnum Value => this.parsedValue ??= this.ParseValue(); | ||
|
||
public static implicit operator StringEnum<TEnum>(string value) => new(value); | ||
|
||
public static implicit operator StringEnum<TEnum>(TEnum parsedValue) => new(parsedValue); | ||
|
||
public bool TryParse(out TEnum value) | ||
{ | ||
if (this.parsedValue is not null) | ||
{ | ||
value = this.parsedValue.Value; | ||
return true; | ||
} | ||
|
||
try | ||
{ | ||
value = ToEnum(this.StringValue); | ||
this.parsedValue = value; | ||
return true; | ||
} | ||
catch (ArgumentException) | ||
{ | ||
value = default; | ||
return false; | ||
} | ||
} | ||
|
||
private static ArgumentException GetArgumentException(string? value) => new(string.Format( | ||
CultureInfo.InvariantCulture, | ||
"Value '{0}' is not a valid '{1}' enum value.", | ||
value, | ||
typeof(TEnum).Name)); | ||
|
||
private TEnum ParseValue() | ||
{ | ||
if (this.TryParse(out var value)) | ||
{ | ||
return value; | ||
} | ||
|
||
throw GetArgumentException(this.StringValue); | ||
} | ||
|
||
private static string ToEnumString(TEnum type) | ||
{ | ||
var enumType = typeof(TEnum); | ||
var name = Enum.GetName(enumType, type); | ||
if (name is not null) | ||
{ | ||
var enumMemberAttribute = ((EnumMemberAttribute[])enumType.GetField(name)!.GetCustomAttributes(typeof(EnumMemberAttribute), true)).Single(); | ||
return enumMemberAttribute.Value!; | ||
} | ||
|
||
throw new ArgumentException(type.ToString()); | ||
} | ||
|
||
private static TEnum ToEnum(string str) | ||
{ | ||
var enumType = typeof(TEnum); | ||
foreach (var name in Enum.GetNames(enumType)) | ||
{ | ||
var enumMemberAttribute = ((EnumMemberAttribute[])enumType.GetField(name)!.GetCustomAttributes(typeof(EnumMemberAttribute), true)).Single(); | ||
if (enumMemberAttribute.Value == str) | ||
{ | ||
return (TEnum)Enum.Parse(enumType, name); | ||
} | ||
} | ||
|
||
throw new ArgumentException(str); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.