Skip to content

Commit

Permalink
Merge pull request #813 from polyadic/try
Browse files Browse the repository at this point in the history
Add more Try* API wrappers
  • Loading branch information
bash authored Jan 21, 2025
2 parents 24289b5 + b1e8ceb commit 918c86a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions FrameworkFeatureConstants.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
<DefineConstants>$(DefineConstants);GENERIC_MATH;GENERIC_PARSABLE;AOT</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
<DefineConstants>$(DefineConstants);RANDOM_SHUFFLE;UTF8_SPAN_PARSABLE</DefineConstants>
<DefineConstants>$(DefineConstants);RANDOM_SHUFFLE;UTF8_SPAN_PARSABLE;JSON_SERIALIZER_OPTIONS_TRY_GET_TYPE_INFO;IP_NETWORK</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))">
<DefineConstants>$(DefineConstants);REFLECTION_ASSEMBLY_NAME_INFO;REFLECTION_TYPE_NAME</DefineConstants>
<DefineConstants>$(DefineConstants);REFLECTION_ASSEMBLY_NAME_INFO;REFLECTION_TYPE_NAME;ORDERED_DICTIONARY</DefineConstants>
</PropertyGroup>
</Project>
14 changes: 14 additions & 0 deletions Funcky/Extensions/JsonSerializerOptionsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#if JSON_SERIALIZER_OPTIONS_TRY_GET_TYPE_INFO
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;

namespace Funcky.Extensions;

public static class JsonSerializerOptionsExtensions
{
public static Option<JsonTypeInfo> GetTypeInfoOrNone(this JsonSerializerOptions options, Type type)
=> options.TryGetTypeInfo(type, out var typeInfo)
? typeInfo
: Option<JsonTypeInfo>.None;
}
#endif
18 changes: 18 additions & 0 deletions Funcky/Extensions/OrderedDictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#if ORDERED_DICTIONARY
using static Funcky.Internal.ValueMapper;

namespace Funcky.Extensions;

public static class OrderedDictionaryExtensions
{
/// <summary>Determines the index of a specific key in the <see cref="System.Collections.Generic.OrderedDictionary{TKey,TValue}" />.</summary>
/// <param name="dictionary">The dictionary to search.</param>
/// <param name="key">The key to locate.</param>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="key" /> is <see langword="null" />.</exception>
/// <returns>The index of <paramref name="key" /> if found; otherwise, <see cref="Option{TItem}.None"/>.</returns>
public static Option<int> IndexOfOrNone<TKey, TValue>(this OrderedDictionary<TKey, TValue> dictionary, TKey key)
where TKey : notnull
=> MapNotFoundToNone(dictionary.IndexOf(key));
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ namespace Funcky.Extensions;
#if IP_END_POINT_TRY_PARSE_SUPPORTED
[OrNoneFromTryPattern(typeof(IPEndPoint), nameof(IPEndPoint.TryParse))]
#endif
#if IP_NETWORK
[OrNoneFromTryPattern(typeof(IPNetwork), nameof(IPNetwork.TryParse))]
#endif
public static partial class ParseExtensions;
6 changes: 6 additions & 0 deletions Funcky/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
#nullable enable
Funcky.Extensions.JsonSerializerOptionsExtensions
Funcky.Extensions.OrderedDictionaryExtensions
static Funcky.Extensions.JsonSerializerOptionsExtensions.GetTypeInfoOrNone(this System.Text.Json.JsonSerializerOptions! options, System.Type! type) -> Funcky.Monads.Option<System.Text.Json.Serialization.Metadata.JsonTypeInfo!>
static Funcky.Extensions.OrderedDictionaryExtensions.IndexOfOrNone<TKey, TValue>(this System.Collections.Generic.OrderedDictionary<TKey, TValue>! dictionary, TKey key) -> Funcky.Monads.Option<int>
static Funcky.Extensions.ParseExtensions.ParseIPNetworkOrNone(this string? candidate) -> Funcky.Monads.Option<System.Net.IPNetwork>
static Funcky.Extensions.ParseExtensions.ParseIPNetworkOrNone(this System.ReadOnlySpan<char> candidate) -> Funcky.Monads.Option<System.Net.IPNetwork>

0 comments on commit 918c86a

Please sign in to comment.