Skip to content

Commit

Permalink
Relation SourceCode Generator WIP2 (fixes regressions form previous c…
Browse files Browse the repository at this point in the history
…ommit)
  • Loading branch information
Bobris committed Dec 31, 2024
1 parent bd95e97 commit 227bc2f
Show file tree
Hide file tree
Showing 4 changed files with 378 additions and 189 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//HintName: TestNamespace.IPersonTable.g.cs
// <auto-generated/>
#pragma warning disable 612,618
#nullable enable
using System;
using System.Runtime.CompilerServices;
// Name: IPersonTable
// Field: ParentId int
// PrimaryIndex: 1
// Field: PersonId int
// PrimaryIndex: 2
// SecondaryIndex PersonId: 1
// Field: Name string reference
// PrimaryIndex: 3 InKeyValue
// Field: LowerCaseName string reference computed
// SecondaryIndex LowerCaseName: 2 IncludePrimaryKeyOrder 1

namespace TestNamespace;
[CompilerGenerated]
static file class IPersonTableRegistration
{
[ModuleInitializer]
internal static unsafe void Register4BTDB()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//HintName: TestNamespace.Person.g.cs
// <auto-generated/>
#pragma warning disable 612,618
using System;
using System.Runtime.CompilerServices;

namespace TestNamespace;

[CompilerGenerated]
static file class PersonRegistration
{
[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
extern static global::TestNamespace.Person Creator();
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<ParentId>k__BackingField")]
extern static ref int Field1(global::TestNamespace.Person @this);
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<PersonId>k__BackingField")]
extern static ref int Field2(global::TestNamespace.Person @this);
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<Name>k__BackingField")]
extern static ref string Field3(global::TestNamespace.Person @this);
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "get_LowerCaseName")]
extern static string Getter4(global::TestNamespace.Person @this);
static void GenGetter4(object @this, ref byte value)
{
Unsafe.As<byte, string>(ref value) = Getter4(Unsafe.As<global::TestNamespace.Person>(@this));
}
[ModuleInitializer]
internal static unsafe void Register4BTDB()
{
global::BTDB.IOC.IContainer.RegisterFactory(typeof(global::TestNamespace.Person), (container, ctx) =>
{
return (container2, ctx2) =>
{
var res = new global::TestNamespace.Person();
return res;
};
});
var metadata = new global::BTDB.Serialization.ClassMetadata();
metadata.Name = "Person";
metadata.Type = typeof(global::TestNamespace.Person);
metadata.Namespace = "TestNamespace";
metadata.Implements = [];
metadata.Creator = &Creator;
var dummy = Unsafe.As<global::TestNamespace.Person>(metadata);
metadata.Fields = new global::BTDB.Serialization.FieldMetadata[]
{
new global::BTDB.Serialization.FieldMetadata
{
Name = "ParentId",
Type = typeof(int),
ByteOffset = global::BTDB.Serialization.RawData.CalcOffset(dummy, ref Field1(dummy)),
},
new global::BTDB.Serialization.FieldMetadata
{
Name = "PersonId",
Type = typeof(int),
ByteOffset = global::BTDB.Serialization.RawData.CalcOffset(dummy, ref Field2(dummy)),
},
new global::BTDB.Serialization.FieldMetadata
{
Name = "Name",
Type = typeof(string),
ByteOffset = global::BTDB.Serialization.RawData.CalcOffset(dummy, ref Field3(dummy)),
},
new global::BTDB.Serialization.FieldMetadata
{
Name = "LowerCaseName",
Type = typeof(string),
PropRefGetter = &GenGetter4,
},
};
global::BTDB.Serialization.ReflectionMetadata.Register(metadata);
}
}
23 changes: 23 additions & 0 deletions BTDB.SourceGenerator.Test/RelationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,27 @@ public interface IPersonTable : ICovariantRelation<Person>
}
""");
}

[Fact]
public Task VerifyRelationWithSecondaryKey()
{
// language=cs
return VerifySourceGenerator("""
using BTDB.ODBLayer;
namespace TestNamespace;
public class Person
{
[PrimaryKey(1)] public int ParentId { get; set; }
[PrimaryKey(2)] [SecondaryKey("PersonId", Order = 1) public int PersonId { get; set; }
[PrimaryKey(3, true)] public string Name { get; set; } = null!;
[SecondaryKey("LowerCaseName", IncludePrimaryKeyOrder = 1, Order = 2)] public string LowerCaseName => Name.ToLower();
}
public interface IPersonTable : IRelation<Person>
{
}
""");
}
}
Loading

0 comments on commit 227bc2f

Please sign in to comment.