Skip to content

Commit

Permalink
Merge pull request #493 from Cysharp/feature/GeneratorImproveIfDirective
Browse files Browse the repository at this point in the history
Introduce GenerateIfDirectiveAttribute
  • Loading branch information
mayuki authored Jan 20, 2022
2 parents 9bf6828 + 079660d commit 4959c64
Show file tree
Hide file tree
Showing 21 changed files with 836 additions and 196 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand Down
15 changes: 15 additions & 0 deletions src/MagicOnion.Abstractions/GenerateIfDirectiveAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace MagicOnion
{
/// <summary>
/// instruction for moc.exe, surround #if symbol with output code.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Interface, AllowMultiple = false, Inherited = false)]
public class GenerateIfDirectiveAttribute : Attribute
{
public GenerateIfDirectiveAttribute(string condition)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace MagicOnion
{
/// <summary>
/// instruction for moc.exe, surround #if symbol with output code.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Interface, AllowMultiple = false, Inherited = false)]
public class GenerateIfDirectiveAttribute : Attribute
{
public GenerateIfDirectiveAttribute(string condition)
{
}
}
}

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

65 changes: 30 additions & 35 deletions src/MagicOnion.GeneratorCore/CodeAnalysis/Definitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class InterfaceDefinition
public string FullName { get; set; }
public string Namespace { get; set; }
public bool IsServiceDefinition { get; set; }
public bool IsIfDebug { get; set; }
public string IfDirectiveCondition { get; set; }
public bool HasIfDirectiveCondition => !string.IsNullOrWhiteSpace(IfDirectiveCondition);
public MethodDefinition[] Methods { get; set; }

// NOTE: A client name is derived from original interface name without 'I' prefix.
Expand All @@ -48,9 +49,11 @@ public class MethodDefinition
public string Name { get; set; }
public MethodType MethodType { get; set; }
public string RequestType { get; set; }
public bool IsIfDebug { get; set; }
public string IfDirectiveCondition { get; set; }
public bool HasIfDirectiveCondition => !string.IsNullOrWhiteSpace(IfDirectiveCondition);
public int HubId { get; set; } // only use in Hub.


string responseType;
public string ResponseType
{
Expand Down Expand Up @@ -317,56 +320,48 @@ public interface IResolverRegisterInfo
{
string FullName { get; }
string FormatterName { get; }

IReadOnlyList<string> IfDirectiveConditions { get; }
bool HasIfDirectiveConditions { get; }
}


public class GenericSerializationInfo : IResolverRegisterInfo, IEquatable<GenericSerializationInfo>
public class GenericSerializationInfo : IResolverRegisterInfo
{
public string FullName { get; set; }
public string FullName { get; }

public string FormatterName { get; set; }
public string FormatterName { get; }

public bool Equals(GenericSerializationInfo other)
{
return FullName.Equals(other.FullName);
}
public IReadOnlyList<string> IfDirectiveConditions { get; }
public bool HasIfDirectiveConditions => IfDirectiveConditions.Any();

public override int GetHashCode()
public GenericSerializationInfo(string fullName, string formatterName, IReadOnlyList<string> ifDirectiveConditions)
{
return FullName.GetHashCode();
}

public override string ToString()
{
return FullName;
FullName = fullName;
FormatterName = formatterName;
IfDirectiveConditions = ifDirectiveConditions;
}
}

public class EnumSerializationInfo : IResolverRegisterInfo, IEquatable<EnumSerializationInfo>
public class EnumSerializationInfo : IResolverRegisterInfo
{
public string Namespace { get; set; }
public string Name { get; set; }
public string FullName { get; set; }
public string UnderlyingType { get; set; }
public string Namespace { get; }
public string Name { get;}
public string FullName { get; }
public string UnderlyingType { get; }

public string FormatterName => Name + "Formatter()";

public bool Equals(EnumSerializationInfo other)
{
return FullName.Equals(other.FullName);
}

public override int GetHashCode()
{
return FullName.GetHashCode();
}
public IReadOnlyList<string> IfDirectiveConditions { get; }
public bool HasIfDirectiveConditions => IfDirectiveConditions.Any();

public override string ToString()
public EnumSerializationInfo(string @namespace, string name, string fullName, string underlyingType, IReadOnlyList<string> ifDirectiveConditions)
{
return FullName;
Namespace = @namespace;
Name = name;
FullName = fullName;
UnderlyingType = underlyingType;
IfDirectiveConditions = ifDirectiveConditions;
}
}



}
8 changes: 4 additions & 4 deletions src/MagicOnion.GeneratorCore/CodeAnalysis/MethodCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public InterfaceDefinition[] CollectServiceInterface()
FullName = x.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),
Namespace = x.ContainingNamespace.IsGlobalNamespace ? null : x.ContainingNamespace.ToDisplayString(),
IsServiceDefinition = true,
IsIfDebug = x.GetAttributes().FindAttributeShortName("GenerateDefineDebugAttribute") != null,
IfDirectiveCondition = x.GetDefinedGenerateIfCondition(),
Methods = x.GetMembers()
.OfType<IMethodSymbol>()
.Select(CreateMethodDefinition)
Expand All @@ -142,7 +142,7 @@ public InterfaceDefinition[] CollectServiceInterface()
Name = x.ToDisplayString(shortTypeNameFormat),
FullName = x.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),
Namespace = x.ContainingNamespace.IsGlobalNamespace ? null : x.ContainingNamespace.ToDisplayString(),
IsIfDebug = x.GetAttributes().FindAttributeShortName("GenerateDefineDebugAttribute") != null,
IfDirectiveCondition = x.GetDefinedGenerateIfCondition(),
Methods = x.GetMembers()
.OfType<IMethodSymbol>()
.Select(CreateMethodDefinition)
Expand All @@ -156,7 +156,7 @@ public InterfaceDefinition[] CollectServiceInterface()
Name = receiver.ToDisplayString(shortTypeNameFormat),
FullName = receiver.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),
Namespace = receiver.ContainingNamespace.IsGlobalNamespace ? null : receiver.ContainingNamespace.ToDisplayString(),
IsIfDebug = receiver.GetAttributes().FindAttributeShortName("GenerateDefineDebugAttribute") != null,
IfDirectiveCondition = receiver.GetDefinedGenerateIfCondition(),
Methods = receiver.GetMembers()
.OfType<IMethodSymbol>()
.Select(CreateMethodDefinition)
Expand Down Expand Up @@ -207,7 +207,7 @@ private MethodDefinition CreateMethodDefinition(IMethodSymbol y)
ResponseType = responseType,
UnwrappedOriginalResposneTypeSymbol = unwrappedOriginalResponseType,
OriginalResponseTypeSymbol = y.ReturnType,
IsIfDebug = y.GetAttributes().FindAttributeShortName("GenerateDefineDebugAttribute") != null,
IfDirectiveCondition = y.GetDefinedGenerateIfCondition(),
HubId = id,
Parameters = y.Parameters.Select(p =>
{
Expand Down
38 changes: 23 additions & 15 deletions src/MagicOnion.GeneratorCore/Generator/CodeTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version: 16.0.0.0
// Runtime Version: 17.0.0.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
Expand All @@ -18,7 +18,7 @@ namespace MagicOnion.Generator
/// <summary>
/// Class to produce the template output
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "16.0.0.0")]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "17.0.0.0")]
public partial class CodeTemplate : CodeTemplateBase
{
/// <summary>
Expand All @@ -33,8 +33,10 @@ public virtual string TransformText()
"ng Grpc.Core;\r\n using MessagePack;\r\n");
foreach(var interfaceDef in Interfaces) {
this.Write("\r\n");
if(interfaceDef.IsIfDebug) {
this.Write("#if DEBUG\r\n");
if(interfaceDef.HasIfDirectiveCondition) {
this.Write("#if ");
this.Write(this.ToStringHelper.ToStringWithCulture(interfaceDef.IfDirectiveCondition));
this.Write("\r\n");
}
var clientName = interfaceDef.ClientName;
this.Write(" [Ignore]\r\n public class ");
Expand All @@ -45,8 +47,10 @@ public virtual string TransformText()
this.Write(this.ToStringHelper.ToStringWithCulture(interfaceDef.FullName));
this.Write("\r\n {\r\n");
foreach(var item in interfaceDef.Methods) {
if(item.IsIfDebug) {
this.Write("#if DEBUG\r\n");
if(item.HasIfDirectiveCondition) {
this.Write("#if ");
this.Write(this.ToStringHelper.ToStringWithCulture(interfaceDef.IfDirectiveCondition));
this.Write("\r\n");
}
this.Write(" static readonly Method<byte[], byte[]> ");
this.Write(this.ToStringHelper.ToStringWithCulture(item.Name));
Expand All @@ -56,16 +60,18 @@ public virtual string TransformText()
this.Write(this.ToStringHelper.ToStringWithCulture(item.Name));
this.Write("Delegate;\r\n");
}
if(item.IsIfDebug) {
if(item.HasIfDirectiveCondition) {
this.Write("#endif\r\n");
}
}
this.Write("\r\n static ");
this.Write(this.ToStringHelper.ToStringWithCulture(clientName));
this.Write("()\r\n {\r\n");
foreach(var item in interfaceDef.Methods) {
if(item.IsIfDebug) {
this.Write("#if DEBUG\r\n");
if(item.HasIfDirectiveCondition) {
this.Write("#if ");
this.Write(this.ToStringHelper.ToStringWithCulture(interfaceDef.IfDirectiveCondition));
this.Write("\r\n");
}
this.Write(" ");
this.Write(this.ToStringHelper.ToStringWithCulture(item.Name));
Expand All @@ -84,7 +90,7 @@ public virtual string TransformText()
this.Write(this.ToStringHelper.ToStringWithCulture(item.Name));
this.Write(";\r\n");
}
if(item.IsIfDebug) {
if(item.HasIfDirectiveCondition) {
this.Write("#endif\r\n");
}
}
Expand Down Expand Up @@ -125,8 +131,10 @@ public virtual string TransformText()
this.Write(" WithOptions(CallOptions option)\r\n {\r\n return base.WithOptions(" +
"option);\r\n }\r\n \r\n");
foreach(var item in interfaceDef.Methods) {
if(item.IsIfDebug) {
this.Write("#if DEBUG\r\n");
if(item.HasIfDirectiveCondition) {
this.Write("#if ");
this.Write(this.ToStringHelper.ToStringWithCulture(interfaceDef.IfDirectiveCondition));
this.Write("\r\n");
}
if(item.MethodType == MethodType.Unary) {
this.Write(" static ResponseContext _");
Expand Down Expand Up @@ -220,12 +228,12 @@ public virtual string TransformText()
"\r\n");
}
this.Write(" }\r\n");
if(item.IsIfDebug) {
if(item.HasIfDirectiveCondition) {
this.Write("#endif\r\n");
}
}
this.Write(" }\r\n");
if(interfaceDef.IsIfDebug) {
if(interfaceDef.HasIfDirectiveCondition) {
this.Write("#endif \r\n");
}
}
Expand All @@ -239,7 +247,7 @@ public virtual string TransformText()
/// <summary>
/// Base class for this transformation
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "16.0.0.0")]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "17.0.0.0")]
public class CodeTemplateBase
{
#region Fields
Expand Down
24 changes: 12 additions & 12 deletions src/MagicOnion.GeneratorCore/Generator/CodeTemplate.tt
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,37 @@
using MessagePack;
<# foreach(var interfaceDef in Interfaces) { #>

<# if(interfaceDef.IsIfDebug) { #>
#if DEBUG
<# if(interfaceDef.HasIfDirectiveCondition) { #>
#if <#= interfaceDef.IfDirectiveCondition #>
<# } #>
<# var clientName = interfaceDef.ClientName; #>
[Ignore]
public class <#= clientName #> : MagicOnionClientBase<<#= interfaceDef.FullName #>>, <#= interfaceDef.FullName #>
{
<# foreach(var item in interfaceDef.Methods) { #>
<# if(item.IsIfDebug) { #>
#if DEBUG
<# if(item.HasIfDirectiveCondition) { #>
#if <#= interfaceDef.IfDirectiveCondition #>
<# } #>
static readonly Method<byte[], byte[]> <#= item.Name #>Method;
<# if(item.MethodType == MethodType.Unary) { #>
static readonly Func<RequestContext, ResponseContext> <#= item.Name #>Delegate;
<# } #>
<# if(item.IsIfDebug) { #>
<# if(item.HasIfDirectiveCondition) { #>
#endif
<# } #>
<# } #>

static <#= clientName #>()
{
<# foreach(var item in interfaceDef.Methods) { #>
<# if(item.IsIfDebug) { #>
#if DEBUG
<# if(item.HasIfDirectiveCondition) { #>
#if <#= interfaceDef.IfDirectiveCondition #>
<# } #>
<#= item.Name #>Method = new Method<byte[], byte[]>(MethodType.<#= item.MethodType.ToString() #>, "<#= interfaceDef.Name #>", "<#= item.Name #>", MagicOnionMarshallers.ThroughMarshaller, MagicOnionMarshallers.ThroughMarshaller);
<# if(item.MethodType == MethodType.Unary) { #>
<#= item.Name #>Delegate = _<#= item.Name #>;
<# } #>
<# if(item.IsIfDebug) { #>
<# if(item.HasIfDirectiveCondition) { #>
#endif
<# } #>
<# } #>
Expand Down Expand Up @@ -100,8 +100,8 @@
}

<# foreach(var item in interfaceDef.Methods) { #>
<# if(item.IsIfDebug) { #>
#if DEBUG
<# if(item.HasIfDirectiveCondition) { #>
#if <#= interfaceDef.IfDirectiveCondition #>
<# } #>
<# if(item.MethodType == MethodType.Unary) { #>
static ResponseContext _<#= item.Name #>(RequestContext __context)
Expand Down Expand Up @@ -134,12 +134,12 @@
return System.Threading.Tasks.Task.FromResult(new DuplexStreamingResult<<#= item.RequestType #>, <#= item.ResponseType #>>(__callResult, new MarshallingClientStreamWriter<<#= item.RequestType #>>(__callResult.RequestStream, serializerOptions), new MarshallingAsyncStreamReader<<#= item.ResponseType #>>(__callResult.ResponseStream, base.serializerOptions), base.serializerOptions));
<# } #>
}
<# if(item.IsIfDebug) { #>
<# if(item.HasIfDirectiveCondition) { #>
#endif
<# } #>
<# } #>
}
<# if(interfaceDef.IsIfDebug) { #>
<# if(interfaceDef.HasIfDirectiveCondition) { #>
#endif
<# } #>
<# } #>
Expand Down
Loading

0 comments on commit 4959c64

Please sign in to comment.