-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add wait policy to compiler (#105)
- Loading branch information
Showing
5 changed files
with
280 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System.Xml.Linq; | ||
|
||
using Azure.ApiManagement.PolicyToolkit.Authoring; | ||
using Azure.ApiManagement.PolicyToolkit.Compiling.Diagnostics; | ||
using Azure.ApiManagement.PolicyToolkit.Compiling.Syntax; | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Azure.ApiManagement.PolicyToolkit.Compiling.Policy; | ||
|
||
public class WaitCompiler : IMethodPolicyHandler | ||
{ | ||
private readonly Lazy<BlockCompiler> _blockCompiler; | ||
|
||
public WaitCompiler(Lazy<BlockCompiler> blockCompiler) | ||
{ | ||
_blockCompiler = blockCompiler; | ||
} | ||
|
||
public string MethodName { get; } = nameof(IInboundContext.Wait); | ||
|
||
public void Handle(ICompilationContext context, InvocationExpressionSyntax node) | ||
{ | ||
if (node.ArgumentList.Arguments.Count is > 2 or < 1) | ||
{ | ||
context.Report(Diagnostic.Create( | ||
CompilationErrors.ArgumentCountMissMatchForPolicy, | ||
node.ArgumentList.GetLocation(), | ||
"wait")); | ||
return; | ||
} | ||
|
||
ExpressionSyntax childPoliciesLambdaExpression = node.ArgumentList.Arguments[0].Expression; | ||
if (childPoliciesLambdaExpression is not LambdaExpressionSyntax lambda) | ||
{ | ||
context.Report(Diagnostic.Create( | ||
CompilationErrors.ValueShouldBe, | ||
childPoliciesLambdaExpression.GetLocation(), | ||
"wait", | ||
nameof(LambdaExpressionSyntax))); | ||
return; | ||
} | ||
|
||
if (lambda.Block is null) | ||
{ | ||
context.Report(Diagnostic.Create( | ||
CompilationErrors.NotSupportedStatement, | ||
lambda.GetLocation(), | ||
childPoliciesLambdaExpression.GetType().FullName | ||
)); | ||
return; | ||
} | ||
|
||
XElement element = new("wait"); | ||
|
||
if (node.ArgumentList.Arguments.Count == 2) | ||
{ | ||
string value = node.ArgumentList.Arguments[1].Expression.ProcessParameter(context); | ||
element.Add(new XAttribute("for", value)); | ||
} | ||
|
||
SubCompilationContext subContext = new(context, element); | ||
_blockCompiler.Value.Compile(subContext, lambda.Block); | ||
|
||
context.AddPolicy(element); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
namespace Azure.ApiManagement.PolicyToolkit.Compiling; | ||
|
||
[TestClass] | ||
public class WaitTests | ||
{ | ||
[TestMethod] | ||
[DataRow( | ||
""" | ||
[Document] | ||
public class PolicyDocument : IDocument | ||
{ | ||
public void Inbound(IInboundContext context) | ||
{ | ||
context.Wait(() => | ||
{ | ||
context.SendRequest(new SendRequestConfig { | ||
ResponseVariableName = "variable" | ||
}); | ||
}); | ||
} | ||
public void Backend(IBackendContext context) | ||
{ | ||
context.Wait(() => | ||
{ | ||
context.SendRequest(new SendRequestConfig { | ||
ResponseVariableName = "variable" | ||
}); | ||
}); | ||
} | ||
public void Outbound(IOutboundContext context) | ||
{ | ||
context.Wait(() => | ||
{ | ||
context.SendRequest(new SendRequestConfig { | ||
ResponseVariableName = "variable" | ||
}); | ||
}); | ||
} | ||
} | ||
""", | ||
""" | ||
<policies> | ||
<inbound> | ||
<wait> | ||
<send-request response-variable-name="variable" /> | ||
</wait> | ||
</inbound> | ||
<backend> | ||
<wait> | ||
<send-request response-variable-name="variable" /> | ||
</wait> | ||
</backend> | ||
<outbound> | ||
<wait> | ||
<send-request response-variable-name="variable" /> | ||
</wait> | ||
</outbound> | ||
</policies> | ||
""", | ||
DisplayName = "Should compile wait policy in sections" | ||
)] | ||
[DataRow( | ||
""" | ||
[Document] | ||
public class PolicyDocument : IDocument | ||
{ | ||
public void Inbound(IInboundContext context) | ||
{ | ||
context.Wait(() => | ||
{ | ||
context.SendRequest(new SendRequestConfig { | ||
ResponseVariableName = "variable" | ||
}); | ||
}, | ||
"any"); | ||
} | ||
} | ||
""", | ||
""" | ||
<policies> | ||
<inbound> | ||
<wait for="any"> | ||
<send-request response-variable-name="variable" /> | ||
</wait> | ||
</inbound> | ||
</policies> | ||
""", | ||
DisplayName = "Should compile wait policy with for attribute" | ||
)] | ||
[DataRow( | ||
""" | ||
[Document] | ||
public class PolicyDocument : IDocument | ||
{ | ||
public void Inbound(IInboundContext context) | ||
{ | ||
context.Wait(() => | ||
{ | ||
context.SendRequest(new SendRequestConfig { | ||
ResponseVariableName = "variable" | ||
}); | ||
}, | ||
GetForAtt(context.ExpressionContext)); | ||
} | ||
string GetForAtt(IExpressionContext context) => context.Variables.ContainsKey("any") ? "any" : "all"; | ||
} | ||
""", | ||
""" | ||
<policies> | ||
<inbound> | ||
<wait for="@(context.Variables.ContainsKey("any") ? "any" : "all")"> | ||
<send-request response-variable-name="variable" /> | ||
</wait> | ||
</inbound> | ||
</policies> | ||
""", | ||
DisplayName = "Should compile wait policy with expression in for attribute" | ||
)] | ||
[DataRow( | ||
""" | ||
[Document] | ||
public class PolicyDocument : IDocument | ||
{ | ||
public void Inbound(IInboundContext context) | ||
{ | ||
context.Wait(() => | ||
{ | ||
if(CacheCondition(context.ExpressionContext)) | ||
{ | ||
context.CacheLookupValue(new CacheLookupValueConfig { | ||
Key = "key", | ||
VariableName = "cache" | ||
}); | ||
} | ||
if(RequestCondition(context.ExpressionContext)) | ||
{ | ||
context.SendRequest(new SendRequestConfig { | ||
ResponseVariableName = "request" | ||
}); | ||
} | ||
}); | ||
} | ||
bool CacheCondition(IExpressionContext context) => !context.Variables.ContainsKey("cache"); | ||
bool RequestCondition(IExpressionContext context) => !context.Variables.ContainsKey("request"); | ||
} | ||
""", | ||
""" | ||
<policies> | ||
<inbound> | ||
<wait> | ||
<choose> | ||
<when condition="@(!context.Variables.ContainsKey("cache"))"> | ||
<cache-lookup-value key="key" variable-name="cache" /> | ||
</when> | ||
</choose> | ||
<choose> | ||
<when condition="@(!context.Variables.ContainsKey("request"))"> | ||
<send-request response-variable-name="request" /> | ||
</when> | ||
</choose> | ||
</wait> | ||
</inbound> | ||
</policies> | ||
""", | ||
DisplayName = "Should compile wait policy with send-request, cache-lookup-value and choose policies" | ||
)] | ||
public void ShouldCompileWaitPolicy(string code, string expectedXml) | ||
{ | ||
code.CompileDocument().Should().BeSuccessful().And.DocumentEquivalentTo(expectedXml); | ||
} | ||
} |