Skip to content

Commit fb75404

Browse files
authored
feat: Implement include-fragment in compiler (#67)
1 parent 6317a8b commit fb75404

File tree

6 files changed

+104
-0
lines changed

6 files changed

+104
-0
lines changed

src/Authoring/IBackendContext.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ public interface IBackendContext : IHaveExpressionContext
5959
/// <param name="config"></param>
6060
void CacheStoreValue(CacheStoreValueConfig config);
6161

62+
/// <summary>
63+
/// The policy inserts the policy fragment as-is at the location you select in the policy definition.<br />
64+
/// Compiled to <a href="https://learn.microsoft.com/en-us/azure/api-management/include-fragment-policy">include-fragment</a> policy.
65+
/// </summary>
66+
/// <param name="fragmentId">A string. Specifies the identifier (name) of a policy fragment created in the API Management instance. Policy expressions aren't allowed.</param>
67+
void IncludeFragment(string fragmentId);
68+
6269
/// <summary>
6370
/// Inlines the specified policy as is to policy document.
6471
/// </summary>

src/Authoring/IInboundContext.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,13 @@ public interface IInboundContext : IHaveExpressionContext
254254
/// <param name="config"></param>
255255
void CacheRemoveValue(CacheRemoveValueConfig config);
256256

257+
/// <summary>
258+
/// The policy inserts the policy fragment as-is at the location you select in the policy definition.<br />
259+
/// Compiled to <a href="https://learn.microsoft.com/en-us/azure/api-management/include-fragment-policy">include-fragment</a> policy.
260+
/// </summary>
261+
/// <param name="fragmentId">A string. Specifies the identifier (name) of a policy fragment created in the API Management instance. Policy expressions aren't allowed.</param>
262+
void IncludeFragment(string fragmentId);
263+
257264
/// <summary>
258265
/// Inlines the specified policy as is to policy document.
259266
/// </summary>

src/Authoring/IOnErrorContext.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ public interface IOnErrorContext : IHaveExpressionContext
110110
/// <param name="config"></param>
111111
void CacheStoreValue(CacheStoreValueConfig config);
112112

113+
/// <summary>
114+
/// The policy inserts the policy fragment as-is at the location you select in the policy definition.<br />
115+
/// Compiled to <a href="https://learn.microsoft.com/en-us/azure/api-management/include-fragment-policy">include-fragment</a> policy.
116+
/// </summary>
117+
/// <param name="fragmentId">A string. Specifies the identifier (name) of a policy fragment created in the API Management instance. Policy expressions aren't allowed.</param>
118+
void IncludeFragment(string fragmentId);
119+
113120
/// <summary>
114121
/// Inlines the specified policy as is to policy document.
115122
/// </summary>

src/Authoring/IOutboundContext.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ public interface IOutboundContext : IHaveExpressionContext
132132
/// <param name="config"></param>
133133
void CacheStoreValue(CacheStoreValueConfig config);
134134

135+
/// <summary>
136+
/// The policy inserts the policy fragment as-is at the location you select in the policy definition.<br />
137+
/// Compiled to <a href="https://learn.microsoft.com/en-us/azure/api-management/include-fragment-policy">include-fragment</a> policy.
138+
/// </summary>
139+
/// <param name="fragmentId">A string. Specifies the identifier (name) of a policy fragment created in the API Management instance. Policy expressions aren't allowed.</param>
140+
void IncludeFragment(string fragmentId);
141+
135142
/// <summary>
136143
/// Inlines the specified policy as is to policy document.
137144
/// </summary>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System.Xml.Linq;
5+
6+
using Azure.ApiManagement.PolicyToolkit.Authoring;
7+
using Azure.ApiManagement.PolicyToolkit.Compiling.Diagnostics;
8+
9+
using Microsoft.CodeAnalysis;
10+
using Microsoft.CodeAnalysis.CSharp.Syntax;
11+
12+
namespace Azure.ApiManagement.PolicyToolkit.Compiling.Policy;
13+
14+
public class IncludeFragmentCompiler : IMethodPolicyHandler
15+
{
16+
public string MethodName => nameof(IInboundContext.IncludeFragment);
17+
18+
public void Handle(ICompilationContext context, InvocationExpressionSyntax node)
19+
{
20+
if (node.ArgumentList.Arguments.Count != 1)
21+
{
22+
context.Report(Diagnostic.Create(
23+
CompilationErrors.ArgumentCountMissMatchForPolicy,
24+
node.ArgumentList.GetLocation(),
25+
"include-fragment"
26+
));
27+
return;
28+
}
29+
30+
var fragmentId = node.ArgumentList.Arguments[0].Expression.ProcessParameter(context);
31+
context.AddPolicy(new XElement("include-fragment", new XAttribute("fragment-id", fragmentId)));
32+
}
33+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
namespace Azure.ApiManagement.PolicyToolkit.Compiling;
5+
6+
[TestClass]
7+
public class IncludeFragmentTests
8+
{
9+
[TestMethod]
10+
[DataRow(
11+
"""
12+
[Document]
13+
public class PolicyDocument : IDocument
14+
{
15+
public void Inbound(IInboundContext context) { context.IncludeFragment("fragment-inbound"); }
16+
public void Outbound(IOutboundContext context) { context.IncludeFragment("fragment-outbound"); }
17+
public void Backend(IBackendContext context) { context.IncludeFragment("fragment-backend"); }
18+
public void OnError(IOnErrorContext context) { context.IncludeFragment("fragment-on-error"); }
19+
}
20+
""",
21+
"""
22+
<policies>
23+
<inbound>
24+
<include-fragment fragment-id="fragment-inbound" />
25+
</inbound>
26+
<outbound>
27+
<include-fragment fragment-id="fragment-outbound" />
28+
</outbound>
29+
<backend>
30+
<include-fragment fragment-id="fragment-backend" />
31+
</backend>
32+
<on-error>
33+
<include-fragment fragment-id="fragment-on-error" />
34+
</on-error>
35+
</policies>
36+
""",
37+
DisplayName = "Should compile include-fragment policy in sections"
38+
)]
39+
public void ShouldCompileIncludeFragmentPolicy(string code, string expectedXml)
40+
{
41+
code.CompileDocument().Should().BeSuccessful().And.DocumentEquivalentTo(expectedXml);
42+
}
43+
}

0 commit comments

Comments
 (0)