Table of Contents

OpaDotNet SDK Interop Compiler

Interop compiler is a wrapper over OPA SDK.

Important

RegoInteropCompiler supports only linux-x64 and windows-x64 platforms.

Add usings


using OpaDotNet.Wasm;
using OpaDotNet.Compilation.Interop;

Compiling single policy file


var compiler = new RegoInteropCompiler();

var policy = await compiler.CompileFileAsync(

    // Policy source file.
    "quickstart/example.rego",
    new()
    {
        // Entrypoints (same you would pass for -e parameter for opa build).
        Entrypoints = ["example/hello"],
    },
    cancellationToken
    );

// RegoCliCompiler will always produce bundle.
using var engine = OpaBundleEvaluatorFactory.Create(policy);

Compiling bundle

Create bundle directory


var compiler = new RegoInteropCompiler();

var policy = await compiler.CompileBundleAsync(

    // Directory with bundle sources.
    "quickstart/",
    new()
    {
        // Entrypoints (same you would pass for -e parameter for opa build).
        Entrypoints = ["example/hello"],
    },
    cancellationToken
    );

// RegoCliCompiler will always produce bundle.
using var engine = OpaBundleEvaluatorFactory.Create(policy);

Compiling policy source


IRegoCompiler compiler = new RegoInteropCompiler();

var policySource = """
    package example

    default hello = false

    hello {
        x := input.message
        x == data.world
    }
    """;

var policy = await compiler.CompileSourceAsync(

    // Policy source code.
    policySource,
    new()
    {
        // Entrypoints (same you would pass for -e parameter for opa build).
        Entrypoints = ["example/hello"],
    },
    cancellationToken
    );

// RegoCliCompiler will always produce bundle.
using var engine = OpaBundleEvaluatorFactory.Create(policy);