Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more Try* API wrappers #813

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Loading