Skip to content

Commit

Permalink
Merge pull request #935 from MrLuje/fsharp-collection
Browse files Browse the repository at this point in the history
feat: properly handle FSharp List deserialization
  • Loading branch information
EdwardCooke authored Jul 14, 2024
2 parents 95e6b41 + 125a77f commit 66cf6d9
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 5 deletions.
81 changes: 81 additions & 0 deletions YamlDotNet.Fsharp.Test/DeserializerTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,84 @@ cars:
person.Cars[1].Spec |> should be null
person.Cars[1].Spec |> should equal None
person.Cars[1].Nickname |> should equal None


[<CLIMutable>]
type TestSeq = {
name: string
numbers: int seq
}

[<Fact>]
let Deserialize_YamlSeq() =
let jackTheDriver = {
name = "Jack"
numbers = seq { 12; 2; 2 }
}

let yaml = """name: Jack
numbers:
- 12
- 2
- 2
"""
let sut = DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build()

let person = sut.Deserialize<TestSeq>(yaml)
person.name |> should equal jackTheDriver.name
person.numbers |> should equalSeq jackTheDriver.numbers

[<CLIMutable>]
type TestList = {
name: string
numbers: int list
}

[<Fact>]
let Deserialize_YamlList() =
let jackTheDriver = {
name = "Jack"
numbers = [ 12; 2; 2 ]
}

let yaml = """name: Jack
numbers:
- 12
- 2
- 2
"""
let sut = DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build()

let person = sut.Deserialize<TestList>(yaml)
person |> should equal jackTheDriver


[<CLIMutable>]
type TestArray = {
name: string
numbers: int array
}

[<Fact>]
let Deserialize_YamlArray() =
let jackTheDriver = {
name = "Jack"
numbers = [| 12; 2; 2 |]
}

let yaml = """name: Jack
numbers:
- 12
- 2
- 2
"""
let sut = DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build()

let person = sut.Deserialize<TestArray>(yaml)
person |> should equal jackTheDriver
81 changes: 79 additions & 2 deletions YamlDotNet.Fsharp.Test/SerializerTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,84 @@ cars:
let person = sut.Serialize(jackTheDriver)
person |> should equal yaml

type TestOmit = {
[<CLIMutable>]
type TestSeq = {
name: string
numbers: int seq
}

[<Fact>]
let Serialize_YamlSeq() =
let jackTheDriver = {
name = "Jack"
numbers = [ 12; 2; 2 ]
}

let yaml = """name: Jack
numbers:
- 12
- 2
- 2
"""
let sut = SerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull)
.Build()

let person = sut.Serialize(jackTheDriver)
person |> should equal yaml


[<CLIMutable>]
type TestList = {
name: string
numbers: int list
}

[<Fact>]
let Serialize_YamlList() =
let jackTheDriver = {
name = "Jack"
numbers = [ 12; 2; 2 ]
}

let yaml = """name: Jack
numbers:
- 12
- 2
- 2
"""
let sut = SerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull)
.Build()

let person = sut.Serialize(jackTheDriver)
person |> should equal yaml

[<CLIMutable>]
type TestArray = {
name: string
plop: int option
numbers: int array
}

[<Fact>]
let Serialize_YamlArray() =
let jackTheDriver = {
name = "Jack"
numbers = [| 12; 2; 2 |]
}

let yaml = """name: Jack
numbers:
- 12
- 2
- 2
"""
let sut = SerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull)
.Build()

let person = sut.Serialize(jackTheDriver)
person |> should equal yaml
26 changes: 24 additions & 2 deletions YamlDotNet/Helpers/FsharpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ namespace YamlDotNet.Helpers
{
public static class FsharpHelper
{
private static bool IsFsharp(Type t)
private static bool IsFsharpCore(Type t)
{
return t.Namespace == "Microsoft.FSharp.Core";
}

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

public static Type? GetOptionUnderlyingType(Type t)
Expand All @@ -34,5 +34,27 @@ public static bool IsOptionType(Type t)

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, new[] { arr });

return fsharpList;
}
}
}
3 changes: 2 additions & 1 deletion YamlDotNet/Serialization/DeserializerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ public DeserializerBuilder()
duplicateKeyChecking,
typeConverter,
enumNamingConvention)
}
},
{ typeof(FsharpListNodeDeserializer), _ => new FsharpListNodeDeserializer(enumNamingConvention) },
};

nodeTypeResolverFactories = new LazyComponentRegistrationList<Nothing, INodeTypeResolver>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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;
using System.Collections.Generic;
using YamlDotNet.Core;
using YamlDotNet.Helpers;
using YamlDotNet.Serialization.Utilities;

namespace YamlDotNet.Serialization.NodeDeserializers
{
public sealed class FsharpListNodeDeserializer : INodeDeserializer
{
private readonly INamingConvention enumNamingConvention;

public FsharpListNodeDeserializer(INamingConvention enumNamingConvention)
{
this.enumNamingConvention = enumNamingConvention;
}

public bool Deserialize(IParser parser, Type expectedType, Func<IParser, Type, object?> nestedObjectDeserializer, out object? value)
{
if (!FsharpHelper.IsFsharpListType(expectedType))
{
value = false;
return false;
}

var itemsType = expectedType.GetGenericArguments()[0];
var collectionType = expectedType.GetGenericTypeDefinition().MakeGenericType(itemsType);

var items = new ArrayList();
CollectionNodeDeserializer.DeserializeHelper(itemsType, parser, nestedObjectDeserializer, items, true, enumNamingConvention);

var array = Array.CreateInstance(itemsType, items.Count);
items.CopyTo(array, 0);

var collection = FsharpHelper.CreateFsharpListFromArray(collectionType, itemsType, array);
value = collection;
return true;
}
}
}

0 comments on commit 66cf6d9

Please sign in to comment.