Skip to content

Commit

Permalink
✨ ViewModelWrapper MP2
Browse files Browse the repository at this point in the history
  • Loading branch information
AigioL committed Jan 22, 2024
1 parent ad61bfe commit 97c7251
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/BD.Common8.Bcl/Diagnostics/Process2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public static unsafe bool IsProcessElevated(Process process)
{
if (CsWin32.PInvoke.OpenProcessToken(handle,
CsWin32.Security.TOKEN_ACCESS_MASK.TOKEN_READ,
out token))
out token))
{
TOKEN_ELEVATION elevation = default;
if (CsWin32.PInvoke.GetTokenInformation(
Expand Down
1 change: 1 addition & 0 deletions src/BD.Common8.SourceGenerator.Bcl.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public enum TestEnum
{
}

[MP2Obj]
[ViewModelWrapperGenerated(typeof(TodoModel),
Properties = [
nameof(TodoModel.C),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ public readonly record struct AttributeModel

/// <inheritdoc cref="ViewModelWrapperGeneratedAttribute"/>
public required ViewModelWrapperGeneratedAttribute Attribute { get; init; }

/// <summary>
/// 类型是否支持 MemoryPack
/// </summary>
public required bool IsMemoryPack { get; init; }
}

protected override AttributeModel GetAttribute(ImmutableArray<AttributeData> attributes)
{
var attribute = attributes.FirstOrDefault(x => x.ClassNameEquals(AttrName));
attribute.ThrowIsNull();

var isMemoryPack = attributes.Any(x => x.ClassNameEquals("MemoryPack.MemoryPackableAttribute"));

if (attribute.ConstructorArguments.FirstOrDefault().GetObjectValue()
is not INamedTypeSymbol modelType)
throw new ArgumentOutOfRangeException(nameof(modelType));
Expand Down Expand Up @@ -74,6 +81,7 @@ protected override AttributeModel GetAttribute(ImmutableArray<AttributeData> att
{
Attribute = attr,
DictProperties = dictProperties,
IsMemoryPack = isMemoryPack,
};
}

Expand Down Expand Up @@ -165,7 +173,7 @@ protected override void WriteFile(Stream stream, SourceModel m)
stream.WriteNewLine();
WriteNamespace(stream, m.Namespace);
stream.WriteNewLine();
var vmBaseType = m.Attribute.ViewModelBaseType?.Name ?? "ReactiveObject";
var vmBaseType = m.Attribute.ViewModelBaseType?.Name ?? (m.AttrModel.IsMemoryPack ? "ReactiveSerializationObject" : "ReactiveObject");
var modelType = m.Attribute.ModelType?.Name;
modelType.ThrowIsNull();

Expand Down Expand Up @@ -233,9 +241,33 @@ partial class {0} : {1}

#region Body

if (debuggerDisplayProperty != null)
if (m.AttrModel.IsMemoryPack)
{
stream.WriteFormat(
"""
/// <summary>
/// Initializes a new instance of the <see cref="{0}"/> class.
"""u8, m.TypeName);
stream.WriteFormat(
"""

/// </summary>
[MP2Constructor, SystemTextJsonConstructor]
public {0}() : this(new())

"""u8, m.TypeName);
stream.Write(
"""
{
}


"""u8);
}

if (debuggerDisplayProperty != null)
{
stream.Write(
"""
/// <inheritdoc cref="DebuggerDisplayAttribute"/>
[XmlIgnore, IgnoreDataMember, SystemTextJsonIgnore, NewtonsoftJsonIgnore, MPIgnore, MP2Ignore]
Expand All @@ -257,11 +289,23 @@ partial class {0} : {1}

if (m.Attribute.Constructor)
{
stream.WriteFormat(
if (m.AttrModel.IsMemoryPack)
{
stream.WriteFormat(
"""
/// <inheritdoc cref="{0}"/>
[MP2Key(0), JsonPropertyOrder(0)]
public {0} Model { get; } = model;
"""u8, modelType);
}
else
{
stream.WriteFormat(
"""
/// <inheritdoc cref="{0}"/>
public {0} Model { get; } = model;
"""u8, modelType);
}
}
else
{
Expand Down Expand Up @@ -398,6 +442,7 @@ static ImmutableArray<IPropertySymbol> GetVMMProperties(ImmutableArray<ISymbol>
stream.WriteFormat(
"""
/// <inheritdoc cref="{0}.{1}"/>
[XmlIgnore, IgnoreDataMember, SystemTextJsonIgnore, NewtonsoftJsonIgnore, MPIgnore, MP2Ignore]
public {2} {1}
"""u8, modelType, p.Key, p.Value);
stream.Write(
Expand Down Expand Up @@ -461,6 +506,7 @@ static ImmutableArray<IPropertySymbol> GetVMMProperties(ImmutableArray<ISymbol>
stream.WriteFormat(
"""
/// <inheritdoc cref="{0}.{1}"/>
[XmlIgnore, IgnoreDataMember, SystemTextJsonIgnore, NewtonsoftJsonIgnore, MPIgnore, MP2Ignore]
public {2} {1}
"""u8, modelType, p.Key, p.Value);
stream.Write("{"u8);
Expand Down
5 changes: 1 addition & 4 deletions src/Ipc.Client.Sample.Experimental/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,11 @@ protected sealed override void ConfigureSocketsHttpHandler(SocketsHttpHandler ha
}

static readonly Lazy<SystemTextJsonSerializerOptions> _JsonSerializerOptions =
new(SampleJsonSerializerContext.Default.Options.AddDefaultJsonTypeInfoResolver);
new(() => SampleJsonSerializerContext.Default.Options.AddDefaultJsonTypeInfoResolver());

/// <inheritdoc/>
protected override SystemTextJsonSerializerOptions JsonSerializerOptions => _JsonSerializerOptions.Value;

/// <inheritdoc/>
protected sealed override bool EnableLogOnError => false;

protected override void OnBuildHubConnection(HubConnection connection)
{
connection.On<string>(nameof(ITodoService), s =>
Expand Down
2 changes: 1 addition & 1 deletion src/Ipc.Server.Sample.Experimental/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ sealed class IpcServerService2(X509Certificate2 serverCertificate) : IpcServerSe

protected override bool ListenUnixSocket => true;

static readonly Lazy<SystemTextJsonSerializerOptions> _JsonSerializerOptions = new(SampleJsonSerializerContext.Default.Options.AddDefaultJsonTypeInfoResolver);
static readonly Lazy<SystemTextJsonSerializerOptions> _JsonSerializerOptions = new(() => SampleJsonSerializerContext.Default.Options.AddDefaultJsonTypeInfoResolver());

protected override SystemTextJsonSerializerOptions JsonSerializerOptions
=> _JsonSerializerOptions.Value;
Expand Down

0 comments on commit 97c7251

Please sign in to comment.