Skip to content

Complex activator #255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions App/App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\WebApiClientCore.Extensions.SourceGenerator\WebApiClientCore.Extensions.SourceGenerator.csproj" />
<ProjectReference Include="..\WebApiClientCore\WebApiClientCore.csproj" />
<ProjectReference Include="..\WebApiClientCore.Extensions.OAuths\WebApiClientCore.Extensions.OAuths.csproj" />
<ProjectReference Include="..\WebApiClientCore\WebApiClientCore.csproj" />
<ProjectReference Include="..\WebApiClientCore.Extensions.NewtonsoftJson\WebApiClientCore.Extensions.NewtonsoftJson.csproj" />
<ProjectReference Include="..\WebApiClientCore.Analyzers\WebApiClientCore.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\WebApiClientCore.Analyzers.SourceGenerator\WebApiClientCore.Analyzers.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
Expand Down
6 changes: 3 additions & 3 deletions App/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public void ConfigureServices(IServiceCollection services)
// ���ӿ�����
services.AddControllers().AddXmlSerializerFormatters();

// Ӧ�ñ���ʱ���ɽӿڵĴ������ʹ���
// ȫ��Ĭ������
services
.AddWebApiClient()
.UseJsonFirstApiActionDescriptor()
.UseSourceGeneratorHttpApiActivator();
.ConfigureHttpApi(o => { })
.UseJsonFirstApiActionDescriptor();

// ע��userApi
services.AddHttpApi(typeof(IUserApi)).ConfigureHttpApi(o =>
Expand Down
2 changes: 0 additions & 2 deletions AppAot/AppAot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\WebApiClientCore.Extensions.OAuths\WebApiClientCore.Extensions.OAuths.csproj" />
<ProjectReference Include="..\WebApiClientCore.Extensions.SourceGenerator\WebApiClientCore.Extensions.SourceGenerator.csproj" />
<ProjectReference Include="..\WebApiClientCore\WebApiClientCore.csproj" />
<ProjectReference Include="..\WebApiClientCore.Analyzers\WebApiClientCore.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\WebApiClientCore.Analyzers.SourceGenerator\WebApiClientCore.Analyzers.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
Expand Down
1 change: 0 additions & 1 deletion AppAot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ static void Main(string[] args)
{
services
.AddWebApiClient()
.UseSourceGeneratorHttpApiActivator() // SG 激活器
.ConfigureHttpApi(options => // json SG生成器配置
{
var jsonContext = AppJsonSerializerContext.Default;
Expand Down
2 changes: 1 addition & 1 deletion WebApiClientCore.Abstractions/HttpApiOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class HttpApiOptions
/// <summary>
/// 获取自定义数据存储的字典
/// </summary>
public Dictionary<object, object> Properties { get; } = new Dictionary<object, object>();
public Dictionary<object, object> Properties { get; } = [];

/// <summary>
/// 获取接口的全局过滤器集合
Expand Down
2 changes: 1 addition & 1 deletion WebApiClientCore.Abstractions/HttpApiRequestMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public async Task AddFormFieldAsync(string name, string? value)
public void AddFormDataText(string name, string? value)
{
var keyValue = new KeyValue(name, value);
this.AddFormDataText(new[] { keyValue });
this.AddFormDataText([keyValue]);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace WebApiClientCore.Serialization.JsonConverters
/// 支持DateTime和DateTimeOffset
/// </summary>
public class JsonDateTimeConverter : JsonConverterFactory
{
{
/// <summary>
/// 获取日期时间格式
/// </summary>
Expand Down Expand Up @@ -85,7 +85,13 @@ public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
{
return value;
}
return DateTime.Parse(reader.GetString());
var stringValue = reader.GetString();
if (string.IsNullOrEmpty(stringValue))
{
throw new NotSupportedException("无法将空值转为DateTime");
}

return DateTime.Parse(stringValue);
}

public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
Expand Down Expand Up @@ -115,7 +121,8 @@ public NullableDateTimeConverter(string format)
return value;
}

return DateTime.Parse(reader.GetString());
var stringValue = reader.GetString();
return string.IsNullOrEmpty(stringValue) ? null : DateTime.Parse(stringValue);
}

public override void Write(Utf8JsonWriter writer, DateTime? value, JsonSerializerOptions options)
Expand Down Expand Up @@ -150,7 +157,13 @@ public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConver
{
return value;
}
return DateTimeOffset.Parse(reader.GetString());

var stringValue = reader.GetString();
if (string.IsNullOrEmpty(stringValue))
{
throw new NotSupportedException("无法将空值转为DateTimeOffset");
}
return DateTimeOffset.Parse(stringValue);
}

public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)
Expand Down Expand Up @@ -179,7 +192,9 @@ public NullableDateTimeOffsetConverter(string format)
{
return value;
}
return DateTimeOffset.Parse(reader.GetString());

var stringValue = reader.GetString();
return string.IsNullOrEmpty(stringValue) ? null : DateTimeOffset.Parse(stringValue);
}

public override void Write(Utf8JsonWriter writer, DateTimeOffset? value, JsonSerializerOptions options)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WebApiClientCore.Analyzers.SourceGenerator
Expand Down Expand Up @@ -36,7 +35,7 @@ public override string ToString()
builder.AppendLine("#if NET5_0_OR_GREATER");
builder.AppendLine("using System.Diagnostics.CodeAnalysis;");
builder.AppendLine("using System.Runtime.CompilerServices;");
builder.AppendLine($"namespace WebApiClientCore.Implementations");
builder.AppendLine($"namespace WebApiClientCore");
builder.AppendLine("{");
builder.AppendLine(" /// <summary>动态依赖初始化器</summary>");
builder.AppendLine($" static partial class {this.ClassName}");
Expand Down
7 changes: 7 additions & 0 deletions WebApiClientCore.Analyzers/Descriptors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ static class Descriptors
public static DiagnosticDescriptor ModifierDescriptor { get; }
= Create("WA1007", Resx.WA1007_title, Resx.WA1007_message);


/// <summary>
/// 泛型接口诊断描述器
/// </summary>
public static DiagnosticDescriptor GenericInterfaceDescriptor { get; }
= Create("WA1008", Resx.WA1008_title, Resx.WA1008_message);

/// <summary>
/// 创建诊断描述器
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion WebApiClientCore.Analyzers/HttpApiDiagnosticAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
Descriptors.NotMethodDefindedDescriptor,
Descriptors.GenericMethodDescriptor,
Descriptors.UriAttributeDescriptor,
Descriptors.ModifierDescriptor);
Descriptors.ModifierDescriptor,
Descriptors.GenericInterfaceDescriptor);
}
}

Expand Down Expand Up @@ -71,6 +72,7 @@ private IEnumerable<HttpApiDiagnosticProvider> GetDiagnosticProviders(HttpApiCon
yield return new GenericMethodDiagnosticProvider(context);
yield return new UriAttributeDiagnosticProvider(context);
yield return new ModifierDiagnosticProvider(context);
yield return new GenericInterfaceDiagnosticProvider(context);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.CodeAnalysis;
using System.Collections.Generic;

namespace WebApiClientCore.Analyzers.Providers
{
/// <summary>
/// 表示泛型接口诊断器
/// </summary>
sealed class GenericInterfaceDiagnosticProvider : HttpApiDiagnosticProvider
{
/// <summary>
/// 获取诊断描述
/// </summary>
public override DiagnosticDescriptor Descriptor => Descriptors.GenericInterfaceDescriptor;

/// <summary>
/// 泛型接口诊断器
/// </summary>
/// <param name="context">上下文</param>
public GenericInterfaceDiagnosticProvider(HttpApiContext context)
: base(context)
{
}

/// <summary>
/// 返回所有的报告诊断
/// </summary>
/// <returns></returns>
public override IEnumerable<Diagnostic> CreateDiagnostics()
{
if (this.Context.Interface.IsGenericType)
{
var location = Context.Syntax.Identifier.GetLocation();
yield return this.CreateDiagnostic(location);
}
}
}
}
27 changes: 22 additions & 5 deletions WebApiClientCore.Analyzers/Resx.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions WebApiClientCore.Analyzers/Resx.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,16 @@
<data name="WA1006_title" xml:space="preserve">
<value>Unsupported decorated parameter index</value>
</data>
<data name="WA1007_message" xml:space="preserve">
<value>The interface must be public</value>
</data>
<data name="WA1007_title" xml:space="preserve">
<value>Unsupported modifiers</value>
</data>
<data name="WA1008_message" xml:space="preserve">
<value>Declaring generic interface is not supported</value>
</data>
<data name="WA1008_title" xml:space="preserve">
<value>Unsupported generic interface</value>
</data>
</root>
6 changes: 6 additions & 0 deletions WebApiClientCore.Analyzers/Resx.resx
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,10 @@
<data name="WA1007_title" xml:space="preserve">
<value>不支持的修饰符</value>
</data>
<data name="WA1008_message" xml:space="preserve">
<value>不支持声明泛型接口</value>
</data>
<data name="WA1008_title" xml:space="preserve">
<value>不支持的泛型接口</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public abstract class TokenProvider : ITokenProvider
/// <summary>
/// 异步锁
/// </summary>
private readonly AsyncRoot asyncRoot = new AsyncRoot();
private readonly AsyncRoot asyncRoot = new();

/// <summary>
/// 获取或设置名称
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using WebApiClientCore;
using WebApiClientCore.Implementations;
using System;

namespace Microsoft.Extensions.DependencyInjection
{
Expand All @@ -12,18 +10,12 @@ public static class WebApiClientBuilderExtensions
/// <summary>
/// 编译时使用SourceGenerator生成接口的代理类型代码
/// 运行时查找接口的代理类型并创建实例
/// </summary>
/// <remarks>
/// <para>• 要求声明http接口的项目的编程语言为c#</para>
/// <para>• 要求声明http接口的项目引用WebApiClientCore.Extensions.SourceGenerator包</para>
/// <para>• 要求Visual Studio或msbuild工具为最新版本</para>
/// </remarks>
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
[Obsolete("SourceGenerator功能已合并到基础包并默认启用")]
public static IWebApiClientBuilder UseSourceGeneratorHttpApiActivator(this IWebApiClientBuilder builder)
{
builder.Services.RemoveAll(typeof(IHttpApiActivator<>));
builder.Services.AddSingleton(typeof(IHttpApiActivator<>), typeof(SourceGeneratorHttpApiActivator<>));
return builder;
}
}
Expand Down

This file was deleted.

Loading