Azure / Azure/azure-api-management-policy-toolkit

Inline expressions

Open
#59 0 comments 0 reactions 1 assignee Claimed by @Mielek View on GitHub
enhancement
Dominant language
C#
Stars
94
Forks
36
Avg merge
2d 6m
Merged PRs (30d)
6

Description

### Proposal

Currently any expression needs to be created as a seperete method. Method needs to be used in a place where expression should be included. e.g.

```csharp
[Document]
public class ApiOperationPolicy : IDocument
{
public void Inbound(IInboundContext context)
{
context.Base();
if (IsFromCompanyIp(context.ExpressionContext))
{
context.AuthenticationBasic("{{username}}", "{{password}}");
}
}

public bool IsFromCompanyIp(IExpressionContext context)
=> context.Request.IpAddress.StartsWith("10.0.0.");
}
```

The issue with that approach is that user needs to create seperete named method to for every, even a smallest expression. Even tens of expression can make file hard to maintain. Toolkit targets as best as possible user experience and flexibility that is why, we need to look into that problem.

The solution to above issue is writing expression inline. Inline expression is a feature which would allow users to write simple expressions in place without a need of creating a seperete method. This would allow developer to make a decision which expression it is worth to extract and potentially test and which ones should be kept in line for readability purposes.

Feature would only allow one-line expressions to be possible in line. Multiline expressions will not be touched and will need a seperete method.

Example of inline expression in choose/when policy:

```csharp
[Document]
public class ApiOperationPolicy : IDocument
{
public void Inbound(IInboundContext context)
{
context.Base();
if (context.ExpressionContext.Request.IpAddress.StartsWith("10.0.0."))
{
context.AuthenticationBasic("{{username}}", "{{password}}");
}
}
}
```
Compiled to
```cshtml








```

Example with inline expression in policy property:
```csharp
[Document]
public class ApiOperationPolicy : IDocument
{
public void Inbound(IInboundContext context)
{
context.SetHeader("X-User-Email", context.ExpressionContext.User.Email);
}
}
```
Compiled to
```cshtml



@(context.User.Email)

```

### Component

Compiler

### Contact Details

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.