Skip to content

Commit 8abcf94

Browse files
authored
Merge pull request #2572 from JKamsker/bugs/2570-Support-FieldOnly-ValueTypes
Add support for ValueTypes that consist only of Fields #2571
2 parents 2cc00c7 + 6f70b4b commit 8abcf94

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using FluentAssertions;
2+
using Xunit;
3+
4+
namespace LiteDB.Tests.Issues;
5+
6+
public class Issue2570_Tests
7+
{
8+
public class Person
9+
{
10+
public int Id { get; set; }
11+
12+
public (string FirstName, string LastName) Name { get; set; }
13+
}
14+
15+
[Fact]
16+
public void Issue2570_Tuples()
17+
{
18+
using (var db = new LiteDatabase(":memory:"))
19+
{
20+
var col = db.GetCollection<Person>("Person");
21+
22+
col.Insert(new Person { Name = ("John", "Doe") });
23+
col.Insert(new Person { Name = ("Joana", "Doe") });
24+
25+
var result = col.FindOne(x => x.Name.FirstName == "John");
26+
27+
result.Should().NotBeNull();
28+
result.Name.FirstName.Should().Be("John");
29+
result.Name.LastName.Should().Be("Doe");
30+
}
31+
}
32+
33+
public struct PersonData
34+
{
35+
public string FirstName;
36+
public string LastName;
37+
}
38+
39+
public class PersonWithStruct
40+
{
41+
public int Id { get; set; }
42+
43+
public PersonData Name { get; set; }
44+
}
45+
46+
[Fact]
47+
public void Issue2570_Structs()
48+
{
49+
using (var db = new LiteDatabase(":memory:"))
50+
{
51+
var col = db.GetCollection<PersonWithStruct>("Person");
52+
53+
col.Insert(new PersonWithStruct { Name = new PersonData { FirstName = "John", LastName = "Doe" } });
54+
col.Insert(new PersonWithStruct { Name = new PersonData { FirstName = "Joana", LastName = "Doe" } });
55+
56+
var result = col.FindOne(x => x.Name.FirstName == "John");
57+
58+
result.Should().NotBeNull();
59+
result.Name.FirstName.Should().Be("John");
60+
result.Name.LastName.Should().Be("Doe");
61+
}
62+
}
63+
}

LiteDB/Client/Mapper/BsonMapper.GetEntityMapper.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,10 @@ protected virtual IEnumerable<MemberInfo> GetTypeMembers(Type type)
169169
.Where(x => x.CanRead && x.GetIndexParameters().Length == 0)
170170
.Select(x => x as MemberInfo));
171171

172-
if (this.IncludeFields)
172+
var shouldIncludeFields = members.Count == 0
173+
&& type.GetTypeInfo().IsValueType;
174+
175+
if (shouldIncludeFields || this.IncludeFields)
173176
{
174177
members.AddRange(type.GetFields(flags).Where(x => !x.Name.EndsWith("k__BackingField") && x.IsStatic == false)
175178
.Select(x => x as MemberInfo));

0 commit comments

Comments
 (0)