Skip to content

Commit 919d988

Browse files
committed
Bon Deserialization works.
1 parent fee928c commit 919d988

13 files changed

+730
-19
lines changed

BTDB.SourceGenerator.Sample/Examples.cs

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.CompilerServices;
24
using BTDB;
35
using BTDB.IOC;
6+
using BTDB.Serialization;
47
using Microsoft.AspNetCore.Http;
58

69
var builder = new ContainerBuilder();
@@ -14,6 +17,28 @@
1417
h(container, "Hello");
1518
}
1619

20+
unsafe
21+
{
22+
ReflectionMetadata.RegisterCollection(new()
23+
{
24+
Type = typeof(Dictionary<int, string>),
25+
ElementKeyType = typeof(int),
26+
ElementValueType = typeof(string),
27+
Creator = &Create1,
28+
AdderKeyValue = &Add1
29+
});
30+
}
31+
32+
static object Create1(uint capacity)
33+
{
34+
return new Dictionary<int, string>((int)capacity);
35+
}
36+
37+
static void Add1(object c, ref byte key, ref byte value)
38+
{
39+
Unsafe.As<Dictionary<int, string>>(c).Add(Unsafe.As<byte, int>(ref key), Unsafe.As<byte, string>(ref value));
40+
}
41+
1742
[Generate]
1843
public class DynamicEventWrapper
1944
{
@@ -74,7 +99,7 @@ public interface ILogger
7499
[Generate]
75100
public class ErrorHandler
76101
{
77-
[Dependency]
102+
[BTDB.IOC.Dependency]
78103
public ILogger? Logger
79104
{
80105
get => null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//HintName: CollectionRegistrations.g.cs
2+
// <auto-generated/>
3+
#nullable enable
4+
using System;
5+
using System.Runtime.CompilerServices;
6+
[CompilerGenerated]
7+
static file class CollectionRegistrations
8+
{
9+
[ModuleInitializer]
10+
internal static void Register4BTDB()
11+
{
12+
BTDB.Serialization.ReflectionMetadata.RegisterCollection(new()
13+
{
14+
Type = typeof(global::System.Collections.Generic.List<global::TestNamespace.Person>),
15+
ElementKeyType = typeof(global::TestNamespace.Person),
16+
Creator = &Create1,
17+
Adder = &Add1
18+
});
19+
20+
static object Create1(uint capacity)
21+
{
22+
return new global::System.Collections.Generic.List<global::TestNamespace.Person>((int)capacity);
23+
}
24+
25+
static void Add1(object c, ref byte value)
26+
{
27+
Unsafe.As<global::System.Collections.Generic.List<global::TestNamespace.Person>>(c).Add(Unsafe.As<byte, global::TestNamespace.Person>(ref value));
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 = "<Name>k__BackingField")]
15+
extern static ref string Field1(global::TestNamespace.Person @this);
16+
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<Friends>k__BackingField")]
17+
extern static ref global::System.Collections.Generic.List<global::TestNamespace.Person> Field2(global::TestNamespace.Person @this);
18+
[ModuleInitializer]
19+
internal static unsafe void Register4BTDB()
20+
{
21+
global::BTDB.IOC.IContainer.RegisterFactory(typeof(global::TestNamespace.Person), (container, ctx) =>
22+
{
23+
return (container2, ctx2) =>
24+
{
25+
var res = new global::TestNamespace.Person();
26+
return res;
27+
};
28+
});
29+
var metadata = new global::BTDB.Serialization.ClassMetadata();
30+
metadata.Name = "Person";
31+
metadata.Type = typeof(global::TestNamespace.Person);
32+
metadata.Namespace = "TestNamespace";
33+
metadata.Implements = [];
34+
metadata.Creator = &Creator;
35+
var dummy = Unsafe.As<global::TestNamespace.Person>(metadata);
36+
metadata.Fields = new[]
37+
{
38+
new global::BTDB.Serialization.FieldMetadata
39+
{
40+
Name = "Name",
41+
Type = typeof(string),
42+
ByteOffset = global::BTDB.Serialization.RawData.CalcOffset(dummy, ref Field1(dummy)),
43+
},
44+
new global::BTDB.Serialization.FieldMetadata
45+
{
46+
Name = "Friends",
47+
Type = typeof(global::System.Collections.Generic.List<global::TestNamespace.Person>),
48+
ByteOffset = global::BTDB.Serialization.RawData.CalcOffset(dummy, ref Field2(dummy)),
49+
},
50+
};
51+
global::BTDB.Serialization.ReflectionMetadata.Register(metadata);
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//HintName: CollectionRegistrations.g.cs
2+
// <auto-generated/>
3+
#nullable enable
4+
using System;
5+
using System.Runtime.CompilerServices;
6+
[CompilerGenerated]
7+
static file class CollectionRegistrations
8+
{
9+
[ModuleInitializer]
10+
internal static void Register4BTDB()
11+
{
12+
BTDB.Serialization.ReflectionMetadata.RegisterCollection(new()
13+
{
14+
Type = typeof(global::System.Collections.Generic.Dictionary<int, string>),
15+
ElementKeyType = typeof(int),
16+
ElementValueType = typeof(string),
17+
Creator = &Create1,
18+
AdderKeyValue = &Add1
19+
});
20+
21+
static object Create1(uint capacity)
22+
{
23+
return new global::System.Collections.Generic.Dictionary<int, string>((int)capacity);
24+
}
25+
26+
static void Add1(object c, ref byte key, ref byte value)
27+
{
28+
Unsafe.As<global::System.Collections.Generic.Dictionary<int, string>>(c).Add(Unsafe.As<byte, int>(ref key), Unsafe.As<byte, string>(ref value));
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 = "<Name>k__BackingField")]
15+
extern static ref string Field1(global::TestNamespace.Person @this);
16+
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<Id2Name>k__BackingField")]
17+
extern static ref global::System.Collections.Generic.Dictionary<int, string> Field2(global::TestNamespace.Person @this);
18+
[ModuleInitializer]
19+
internal static unsafe void Register4BTDB()
20+
{
21+
global::BTDB.IOC.IContainer.RegisterFactory(typeof(global::TestNamespace.Person), (container, ctx) =>
22+
{
23+
return (container2, ctx2) =>
24+
{
25+
var res = new global::TestNamespace.Person();
26+
return res;
27+
};
28+
});
29+
var metadata = new global::BTDB.Serialization.ClassMetadata();
30+
metadata.Name = "Person";
31+
metadata.Type = typeof(global::TestNamespace.Person);
32+
metadata.Namespace = "TestNamespace";
33+
metadata.Implements = [];
34+
metadata.Creator = &Creator;
35+
var dummy = Unsafe.As<global::TestNamespace.Person>(metadata);
36+
metadata.Fields = new[]
37+
{
38+
new global::BTDB.Serialization.FieldMetadata
39+
{
40+
Name = "Name",
41+
Type = typeof(string),
42+
ByteOffset = global::BTDB.Serialization.RawData.CalcOffset(dummy, ref Field1(dummy)),
43+
},
44+
new global::BTDB.Serialization.FieldMetadata
45+
{
46+
Name = "Id2Name",
47+
Type = typeof(global::System.Collections.Generic.Dictionary<int, string>),
48+
ByteOffset = global::BTDB.Serialization.RawData.CalcOffset(dummy, ref Field2(dummy)),
49+
},
50+
};
51+
global::BTDB.Serialization.ReflectionMetadata.Register(metadata);
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//HintName: CollectionRegistrations.g.cs
2+
// <auto-generated/>
3+
#nullable enable
4+
using System;
5+
using System.Runtime.CompilerServices;
6+
[CompilerGenerated]
7+
static file class CollectionRegistrations
8+
{
9+
[ModuleInitializer]
10+
internal static void Register4BTDB()
11+
{
12+
BTDB.Serialization.ReflectionMetadata.RegisterCollection(new()
13+
{
14+
Type = typeof(global::System.Collections.Generic.List<string>),
15+
ElementKeyType = typeof(string),
16+
Creator = &Create1,
17+
Adder = &Add1
18+
});
19+
20+
static object Create1(uint capacity)
21+
{
22+
return new global::System.Collections.Generic.List<string>((int)capacity);
23+
}
24+
25+
static void Add1(object c, ref byte value)
26+
{
27+
Unsafe.As<global::System.Collections.Generic.List<string>>(c).Add(Unsafe.As<byte, string>(ref value));
28+
}
29+
30+
BTDB.Serialization.ReflectionMetadata.RegisterCollection(new()
31+
{
32+
Type = typeof(global::System.Collections.Generic.Dictionary<int, global::System.Collections.Generic.List<string>>),
33+
ElementKeyType = typeof(int),
34+
ElementValueType = typeof(global::System.Collections.Generic.List<string>),
35+
Creator = &Create2,
36+
AdderKeyValue = &Add2
37+
});
38+
39+
static object Create2(uint capacity)
40+
{
41+
return new global::System.Collections.Generic.Dictionary<int, global::System.Collections.Generic.List<string>>((int)capacity);
42+
}
43+
44+
static void Add2(object c, ref byte key, ref byte value)
45+
{
46+
Unsafe.As<global::System.Collections.Generic.Dictionary<int, global::System.Collections.Generic.List<string>>>(c).Add(Unsafe.As<byte, int>(ref key), Unsafe.As<byte, global::System.Collections.Generic.List<string>>(ref value));
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 = "<Name>k__BackingField")]
15+
extern static ref string Field1(global::TestNamespace.Person @this);
16+
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<Id2Names>k__BackingField")]
17+
extern static ref global::System.Collections.Generic.Dictionary<int, global::System.Collections.Generic.List<string>> Field2(global::TestNamespace.Person @this);
18+
[ModuleInitializer]
19+
internal static unsafe void Register4BTDB()
20+
{
21+
global::BTDB.IOC.IContainer.RegisterFactory(typeof(global::TestNamespace.Person), (container, ctx) =>
22+
{
23+
return (container2, ctx2) =>
24+
{
25+
var res = new global::TestNamespace.Person();
26+
return res;
27+
};
28+
});
29+
var metadata = new global::BTDB.Serialization.ClassMetadata();
30+
metadata.Name = "Person";
31+
metadata.Type = typeof(global::TestNamespace.Person);
32+
metadata.Namespace = "TestNamespace";
33+
metadata.Implements = [];
34+
metadata.Creator = &Creator;
35+
var dummy = Unsafe.As<global::TestNamespace.Person>(metadata);
36+
metadata.Fields = new[]
37+
{
38+
new global::BTDB.Serialization.FieldMetadata
39+
{
40+
Name = "Name",
41+
Type = typeof(string),
42+
ByteOffset = global::BTDB.Serialization.RawData.CalcOffset(dummy, ref Field1(dummy)),
43+
},
44+
new global::BTDB.Serialization.FieldMetadata
45+
{
46+
Name = "Id2Names",
47+
Type = typeof(global::System.Collections.Generic.Dictionary<int, global::System.Collections.Generic.List<string>>),
48+
ByteOffset = global::BTDB.Serialization.RawData.CalcOffset(dummy, ref Field2(dummy)),
49+
},
50+
};
51+
global::BTDB.Serialization.ReflectionMetadata.Register(metadata);
52+
}
53+
}

BTDB.SourceGenerator.Test/MetadataTests.cs

+52-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using System.Linq;
1+
using System.Collections.Generic;
22
using System.Threading.Tasks;
3-
using Microsoft.CodeAnalysis;
4-
using Microsoft.CodeAnalysis.CSharp;
53
using VerifyXunit;
64
using Xunit;
75

@@ -26,6 +24,57 @@ public class Person
2624
""");
2725
}
2826

27+
[Fact]
28+
public Task VerifyCollectionMetadata()
29+
{
30+
// language=cs
31+
return VerifySourceGenerator("""
32+
using System.Collections.Generic;
33+
namespace TestNamespace;
34+
35+
[BTDB.Generate]
36+
public class Person
37+
{
38+
public string Name { get; set; } = "";
39+
public List<Person> Friends { get; set; } = new();
40+
}
41+
""");
42+
}
43+
44+
[Fact]
45+
public Task VerifyDictCollectionMetadata()
46+
{
47+
// language=cs
48+
return VerifySourceGenerator("""
49+
using System.Collections.Generic;
50+
namespace TestNamespace;
51+
52+
[BTDB.Generate]
53+
public class Person
54+
{
55+
public string Name { get; set; } = "";
56+
public Dictionary<int,string> Id2Name { get; set; } = new();
57+
}
58+
""");
59+
}
60+
61+
[Fact]
62+
public Task VerifyNestedCollectionMetadata()
63+
{
64+
// language=cs
65+
return VerifySourceGenerator("""
66+
using System.Collections.Generic;
67+
namespace TestNamespace;
68+
69+
[BTDB.Generate]
70+
public class Person
71+
{
72+
public string Name { get; set; } = "";
73+
public Dictionary<int,List<string>> Id2Names { get; set; } = new();
74+
}
75+
""");
76+
}
77+
2978
[Fact]
3079
public Task VerifyImplementsInterface()
3180
{

0 commit comments

Comments
 (0)