Skip to content

Inline expressions #59

@Mielek

Description

@Mielek

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.

[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:

[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

<policies>
  <inbound>
    <base />
    <choose>
      <when condition="@(context.Request.IpAddress.StartsWith("10.0.0."))">
        <authentication-basic username="{{username}}" password="{{password}}" />
      </when>
    </choose>
  </inbound>
</policies>

Example with inline expression in policy property:

[Document]
public class ApiOperationPolicy : IDocument
{
    public void Inbound(IInboundContext context)
    {
        context.SetHeader("X-User-Email", context.ExpressionContext.User.Email);
    }
}

Compiled to

<policies>
  <inbound>
    <set-header name="X-User-Email" exists-action="override">
      <value>@(context.User.Email)</value>
    </set-header>
  </inbound>
</policies>

Component

Compiler

Contact Details

No response

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions