Skip to content

Commit 227bc2f

Browse files
committed
Relation SourceCode Generator WIP2 (fixes regressions form previous commit)
1 parent bd95e97 commit 227bc2f

4 files changed

+378
-189
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//HintName: TestNamespace.IPersonTable.g.cs
2+
// <auto-generated/>
3+
#pragma warning disable 612,618
4+
#nullable enable
5+
using System;
6+
using System.Runtime.CompilerServices;
7+
// Name: IPersonTable
8+
// Field: ParentId int
9+
// PrimaryIndex: 1
10+
// Field: PersonId int
11+
// PrimaryIndex: 2
12+
// SecondaryIndex PersonId: 1
13+
// Field: Name string reference
14+
// PrimaryIndex: 3 InKeyValue
15+
// Field: LowerCaseName string reference computed
16+
// SecondaryIndex LowerCaseName: 2 IncludePrimaryKeyOrder 1
17+
18+
namespace TestNamespace;
19+
[CompilerGenerated]
20+
static file class IPersonTableRegistration
21+
{
22+
[ModuleInitializer]
23+
internal static unsafe void Register4BTDB()
24+
{
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//HintName: TestNamespace.Person.g.cs
2+
// <auto-generated/>
3+
#pragma warning disable 612,618
4+
using System;
5+
using System.Runtime.CompilerServices;
6+
7+
namespace TestNamespace;
8+
9+
[CompilerGenerated]
10+
static file class PersonRegistration
11+
{
12+
[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
13+
extern static global::TestNamespace.Person Creator();
14+
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<ParentId>k__BackingField")]
15+
extern static ref int Field1(global::TestNamespace.Person @this);
16+
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<PersonId>k__BackingField")]
17+
extern static ref int Field2(global::TestNamespace.Person @this);
18+
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<Name>k__BackingField")]
19+
extern static ref string Field3(global::TestNamespace.Person @this);
20+
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "get_LowerCaseName")]
21+
extern static string Getter4(global::TestNamespace.Person @this);
22+
static void GenGetter4(object @this, ref byte value)
23+
{
24+
Unsafe.As<byte, string>(ref value) = Getter4(Unsafe.As<global::TestNamespace.Person>(@this));
25+
}
26+
[ModuleInitializer]
27+
internal static unsafe void Register4BTDB()
28+
{
29+
global::BTDB.IOC.IContainer.RegisterFactory(typeof(global::TestNamespace.Person), (container, ctx) =>
30+
{
31+
return (container2, ctx2) =>
32+
{
33+
var res = new global::TestNamespace.Person();
34+
return res;
35+
};
36+
});
37+
var metadata = new global::BTDB.Serialization.ClassMetadata();
38+
metadata.Name = "Person";
39+
metadata.Type = typeof(global::TestNamespace.Person);
40+
metadata.Namespace = "TestNamespace";
41+
metadata.Implements = [];
42+
metadata.Creator = &Creator;
43+
var dummy = Unsafe.As<global::TestNamespace.Person>(metadata);
44+
metadata.Fields = new global::BTDB.Serialization.FieldMetadata[]
45+
{
46+
new global::BTDB.Serialization.FieldMetadata
47+
{
48+
Name = "ParentId",
49+
Type = typeof(int),
50+
ByteOffset = global::BTDB.Serialization.RawData.CalcOffset(dummy, ref Field1(dummy)),
51+
},
52+
new global::BTDB.Serialization.FieldMetadata
53+
{
54+
Name = "PersonId",
55+
Type = typeof(int),
56+
ByteOffset = global::BTDB.Serialization.RawData.CalcOffset(dummy, ref Field2(dummy)),
57+
},
58+
new global::BTDB.Serialization.FieldMetadata
59+
{
60+
Name = "Name",
61+
Type = typeof(string),
62+
ByteOffset = global::BTDB.Serialization.RawData.CalcOffset(dummy, ref Field3(dummy)),
63+
},
64+
new global::BTDB.Serialization.FieldMetadata
65+
{
66+
Name = "LowerCaseName",
67+
Type = typeof(string),
68+
PropRefGetter = &GenGetter4,
69+
},
70+
};
71+
global::BTDB.Serialization.ReflectionMetadata.Register(metadata);
72+
}
73+
}

BTDB.SourceGenerator.Test/RelationTests.cs

+23
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,27 @@ public interface IPersonTable : ICovariantRelation<Person>
2626
}
2727
""");
2828
}
29+
30+
[Fact]
31+
public Task VerifyRelationWithSecondaryKey()
32+
{
33+
// language=cs
34+
return VerifySourceGenerator("""
35+
using BTDB.ODBLayer;
36+
37+
namespace TestNamespace;
38+
39+
public class Person
40+
{
41+
[PrimaryKey(1)] public int ParentId { get; set; }
42+
[PrimaryKey(2)] [SecondaryKey("PersonId", Order = 1) public int PersonId { get; set; }
43+
[PrimaryKey(3, true)] public string Name { get; set; } = null!;
44+
[SecondaryKey("LowerCaseName", IncludePrimaryKeyOrder = 1, Order = 2)] public string LowerCaseName => Name.ToLower();
45+
}
46+
47+
public interface IPersonTable : IRelation<Person>
48+
{
49+
}
50+
""");
51+
}
2952
}

0 commit comments

Comments
 (0)