Skip to content

Commit 918c86a

Browse files
authored
Merge pull request #813 from polyadic/try
Add more Try* API wrappers
2 parents 24289b5 + b1e8ceb commit 918c86a

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

FrameworkFeatureConstants.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
<DefineConstants>$(DefineConstants);GENERIC_MATH;GENERIC_PARSABLE;AOT</DefineConstants>
1717
</PropertyGroup>
1818
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
19-
<DefineConstants>$(DefineConstants);RANDOM_SHUFFLE;UTF8_SPAN_PARSABLE</DefineConstants>
19+
<DefineConstants>$(DefineConstants);RANDOM_SHUFFLE;UTF8_SPAN_PARSABLE;JSON_SERIALIZER_OPTIONS_TRY_GET_TYPE_INFO;IP_NETWORK</DefineConstants>
2020
</PropertyGroup>
2121
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))">
22-
<DefineConstants>$(DefineConstants);REFLECTION_ASSEMBLY_NAME_INFO;REFLECTION_TYPE_NAME</DefineConstants>
22+
<DefineConstants>$(DefineConstants);REFLECTION_ASSEMBLY_NAME_INFO;REFLECTION_TYPE_NAME;ORDERED_DICTIONARY</DefineConstants>
2323
</PropertyGroup>
2424
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#if JSON_SERIALIZER_OPTIONS_TRY_GET_TYPE_INFO
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization.Metadata;
4+
5+
namespace Funcky.Extensions;
6+
7+
public static class JsonSerializerOptionsExtensions
8+
{
9+
public static Option<JsonTypeInfo> GetTypeInfoOrNone(this JsonSerializerOptions options, Type type)
10+
=> options.TryGetTypeInfo(type, out var typeInfo)
11+
? typeInfo
12+
: Option<JsonTypeInfo>.None;
13+
}
14+
#endif
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#if ORDERED_DICTIONARY
2+
using static Funcky.Internal.ValueMapper;
3+
4+
namespace Funcky.Extensions;
5+
6+
public static class OrderedDictionaryExtensions
7+
{
8+
/// <summary>Determines the index of a specific key in the <see cref="System.Collections.Generic.OrderedDictionary{TKey,TValue}" />.</summary>
9+
/// <param name="dictionary">The dictionary to search.</param>
10+
/// <param name="key">The key to locate.</param>
11+
/// <exception cref="T:System.ArgumentNullException">
12+
/// <paramref name="key" /> is <see langword="null" />.</exception>
13+
/// <returns>The index of <paramref name="key" /> if found; otherwise, <see cref="Option{TItem}.None"/>.</returns>
14+
public static Option<int> IndexOfOrNone<TKey, TValue>(this OrderedDictionary<TKey, TValue> dictionary, TKey key)
15+
where TKey : notnull
16+
=> MapNotFoundToNone(dictionary.IndexOf(key));
17+
}
18+
#endif

Funcky/Extensions/ParseExtensions/ParseExtensions.IpPrimitives.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ namespace Funcky.Extensions;
77
#if IP_END_POINT_TRY_PARSE_SUPPORTED
88
[OrNoneFromTryPattern(typeof(IPEndPoint), nameof(IPEndPoint.TryParse))]
99
#endif
10+
#if IP_NETWORK
11+
[OrNoneFromTryPattern(typeof(IPNetwork), nameof(IPNetwork.TryParse))]
12+
#endif
1013
public static partial class ParseExtensions;

Funcky/PublicAPI.Unshipped.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
#nullable enable
2+
Funcky.Extensions.JsonSerializerOptionsExtensions
3+
Funcky.Extensions.OrderedDictionaryExtensions
4+
static Funcky.Extensions.JsonSerializerOptionsExtensions.GetTypeInfoOrNone(this System.Text.Json.JsonSerializerOptions! options, System.Type! type) -> Funcky.Monads.Option<System.Text.Json.Serialization.Metadata.JsonTypeInfo!>
5+
static Funcky.Extensions.OrderedDictionaryExtensions.IndexOfOrNone<TKey, TValue>(this System.Collections.Generic.OrderedDictionary<TKey, TValue>! dictionary, TKey key) -> Funcky.Monads.Option<int>
6+
static Funcky.Extensions.ParseExtensions.ParseIPNetworkOrNone(this string? candidate) -> Funcky.Monads.Option<System.Net.IPNetwork>
7+
static Funcky.Extensions.ParseExtensions.ParseIPNetworkOrNone(this System.ReadOnlySpan<char> candidate) -> Funcky.Monads.Option<System.Net.IPNetwork>

0 commit comments

Comments
 (0)