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 support for ValueTypes that consist only of Fields #2571 #2572

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
63 changes: 63 additions & 0 deletions LiteDB.Tests/Issues/Issue2570_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using FluentAssertions;
using Xunit;

namespace LiteDB.Tests.Issues;

public class Issue2570_Tests
{
public class Person
{
public int Id { get; set; }

public (string FirstName, string LastName) Name { get; set; }
}

[Fact]
public void Issue2570_Tuples()
{
using (var db = new LiteDatabase(":memory:"))
{
var col = db.GetCollection<Person>("Person");

col.Insert(new Person { Name = ("John", "Doe") });
col.Insert(new Person { Name = ("Joana", "Doe") });

var result = col.FindOne(x => x.Name.FirstName == "John");

result.Should().NotBeNull();
result.Name.FirstName.Should().Be("John");
result.Name.LastName.Should().Be("Doe");
}
}

public struct PersonData
{
public string FirstName;
public string LastName;
}

public class PersonWithStruct
{
public int Id { get; set; }

public PersonData Name { get; set; }
}

[Fact]
public void Issue2570_Structs()
{
using (var db = new LiteDatabase(":memory:"))
{
var col = db.GetCollection<PersonWithStruct>("Person");

col.Insert(new PersonWithStruct { Name = new PersonData { FirstName = "John", LastName = "Doe" } });
col.Insert(new PersonWithStruct { Name = new PersonData { FirstName = "Joana", LastName = "Doe" } });

var result = col.FindOne(x => x.Name.FirstName == "John");

result.Should().NotBeNull();
result.Name.FirstName.Should().Be("John");
result.Name.LastName.Should().Be("Doe");
}
}
}
5 changes: 4 additions & 1 deletion LiteDB/Client/Mapper/BsonMapper.GetEntityMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ protected virtual IEnumerable<MemberInfo> GetTypeMembers(Type type)
.Where(x => x.CanRead && x.GetIndexParameters().Length == 0)
.Select(x => x as MemberInfo));

if (this.IncludeFields)
var shouldIncludeFields = members.Count == 0
&& type.GetTypeInfo().IsValueType;

if (shouldIncludeFields || this.IncludeFields)
{
members.AddRange(type.GetFields(flags).Where(x => !x.Name.EndsWith("k__BackingField") && x.IsStatic == false)
.Select(x => x as MemberInfo));
Expand Down