Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline expressions #59

Open
Mielek opened this issue Jan 6, 2025 · 0 comments
Open

Inline expressions #59

Mielek opened this issue Jan 6, 2025 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@Mielek
Copy link
Collaborator

Mielek commented Jan 6, 2025

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

@Mielek Mielek added the enhancement New feature or request label Jan 6, 2025
@Mielek Mielek self-assigned this Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant