Skip to content

Commit

Permalink
Disable fsharp on AOT/Trimming
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwardCooke committed Dec 1, 2024
1 parent 9a977ef commit 3505cbf
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<PublishAot>true</PublishAot>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<Nullable>enable</Nullable>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug-AOT|AnyCPU' ">
Expand Down
85 changes: 85 additions & 0 deletions YamlDotNet/Helpers/DefaultFsharpHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YamlDotNet.Serialization;

namespace YamlDotNet.Helpers
{
public class DefaultFsharpHelper : IFsharpHelper
{
private static bool IsFsharpCore(Type t)
{
return t.Namespace == "Microsoft.FSharp.Core";
}

public bool IsOptionType(Type t)
{
return IsFsharpCore(t) && t.Name == "FSharpOption`1";
}

public Type? GetOptionUnderlyingType(Type t)
{
return t.IsGenericType && IsOptionType(t) ? t.GenericTypeArguments[0] : null;
}

public object? GetValue(IObjectDescriptor objectDescriptor)
{
if (!IsOptionType(objectDescriptor.Type))
{
throw new InvalidOperationException("Should not be called on non-Option<> type");
}

if (objectDescriptor.Value is null)
{
return null;
}

return objectDescriptor.Type.GetProperty("Value").GetValue(objectDescriptor.Value);
}

public bool IsFsharpListType(Type t)
{
return t.Namespace == "Microsoft.FSharp.Collections" && t.Name == "FSharpList`1";
}

public object? CreateFsharpListFromArray(Type t, Type itemsType, Array arr)
{
if (!IsFsharpListType(t))
{
return null;
}

var fsharpList =
t.Assembly
.GetType("Microsoft.FSharp.Collections.ListModule")
.GetMethod("OfArray")
.MakeGenericMethod(itemsType)
.Invoke(null, [arr]);

return fsharpList;
}
}
}
52 changes: 6 additions & 46 deletions YamlDotNet/Helpers/FsharpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,56 +26,16 @@ namespace YamlDotNet.Helpers
{
public static class FsharpHelper
{
private static bool IsFsharpCore(Type t)
{
return t.Namespace == "Microsoft.FSharp.Core";
}
public static IFsharpHelper? Instance { get; set; }

public static bool IsOptionType(Type t)
{
return IsFsharpCore(t) && t.Name == "FSharpOption`1";
}
public static bool IsOptionType(Type t) => Instance?.IsOptionType(t) ?? false;

public static Type? GetOptionUnderlyingType(Type t)
{
return t.IsGenericType && IsOptionType(t) ? t.GenericTypeArguments[0] : null;
}
public static Type? GetOptionUnderlyingType(Type t) => Instance?.GetOptionUnderlyingType(t);

public static object? GetValue(IObjectDescriptor objectDescriptor)
{
if (!IsOptionType(objectDescriptor.Type))
{
throw new InvalidOperationException("Should not be called on non-Option<> type");
}
public static object? GetValue(IObjectDescriptor objectDescriptor) => Instance?.GetValue(objectDescriptor);

if (objectDescriptor.Value is null)
{
return null;
}
public static bool IsFsharpListType(Type t) => Instance?.IsFsharpListType(t) ?? false;

return objectDescriptor.Type.GetProperty("Value").GetValue(objectDescriptor.Value);
}

public static bool IsFsharpListType(Type t)
{
return t.Namespace == "Microsoft.FSharp.Collections" && t.Name == "FSharpList`1";
}

public static object? CreateFsharpListFromArray(Type t, Type itemsType, Array arr)
{
if (!IsFsharpListType(t))
{
return null;
}

var fsharpList =
t.Assembly
.GetType("Microsoft.FSharp.Collections.ListModule")
.GetMethod("OfArray")
.MakeGenericMethod(itemsType)
.Invoke(null, [arr]);

return fsharpList;
}
public static object? CreateFsharpListFromArray(Type t, Type itemsType, Array arr) => Instance?.CreateFsharpListFromArray(t, itemsType, arr);
}
}
35 changes: 35 additions & 0 deletions YamlDotNet/Helpers/IFsharpHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;
using YamlDotNet.Serialization;

namespace YamlDotNet.Helpers
{
public interface IFsharpHelper
{
bool IsOptionType(Type t);
Type? GetOptionUnderlyingType(Type t);
object? GetValue(IObjectDescriptor objectDescriptor);
bool IsFsharpListType(Type t);
object? CreateFsharpListFromArray(Type t, Type itemsType, Array arr);
}
}
42 changes: 42 additions & 0 deletions YamlDotNet/Helpers/NullFsharpHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;
using YamlDotNet.Serialization;

namespace YamlDotNet.Helpers
{
/// <summary>
/// Empty implementation of the fsharphelper to allow trimming of csharp applications.
/// </summary>
public class NullFsharpHelper : IFsharpHelper
{
public object? CreateFsharpListFromArray(Type t, Type itemsType, Array arr) => null;

public Type? GetOptionUnderlyingType(Type t) => null;

public object? GetValue(IObjectDescriptor objectDescriptor) => null;

public bool IsFsharpListType(Type t) => false;

public bool IsOptionType(Type t) => false;
}
}
5 changes: 5 additions & 0 deletions YamlDotNet/Serialization/DeserializerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,11 @@ public DeserializerBuilder WithDuplicateKeyChecking()
/// </summary>
public IDeserializer Build()
{
if (FsharpHelper.Instance == null)
{
FsharpHelper.Instance = new DefaultFsharpHelper();
}

return Deserializer.FromValueDeserializer(BuildValueDeserializer());
}

Expand Down
5 changes: 5 additions & 0 deletions YamlDotNet/Serialization/SerializerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,11 @@ public SerializerBuilder WithIndentedSequences()
/// </summary>
public ISerializer Build()
{
if (FsharpHelper.Instance == null)
{
FsharpHelper.Instance = new DefaultFsharpHelper();
}

return Serializer.FromValueSerializer(BuildValueSerializer(), emitterSettings);
}

Expand Down
1 change: 1 addition & 0 deletions YamlDotNet/Serialization/StaticDeserializerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
using YamlDotNet.Serialization.TypeResolvers;
using YamlDotNet.Serialization.Utilities;
using YamlDotNet.Serialization.ValueDeserializers;
using YamlDotNet.Helpers;

namespace YamlDotNet.Serialization
{
Expand Down

0 comments on commit 3505cbf

Please sign in to comment.