• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

me-viper / OpaDotNet.Compilation / 6942841257

21 Nov 2023 09:53AM UTC coverage: 88.266%. Remained the same
6942841257

push

github

me-viper
chore: Update CHANGELOG

180 of 220 branches covered (0.0%)

Branch coverage included in aggregate %.

670 of 743 relevant lines covered (90.17%)

533.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.34
/src/OpaDotNet.Compilation.Abstractions/RegoCompilerExtensions.cs
1
namespace OpaDotNet.Compilation.Abstractions;
2

3
/// <summary>
4
/// Compilation extensions.
5
/// </summary>
6
public static class RegoCompilerExtensions
7
{
8
    /// <summary>
9
    /// Compiles OPA bundle from bundle directory.
10
    /// </summary>
11
    /// <param name="compiler">Compiler instance.</param>
12
    /// <param name="bundlePath">Bundle directory or bundle archive path.</param>
13
    /// <param name="entrypoints">Which documents (entrypoints) will be queried when asking for policy decisions.</param>
14
    /// <param name="capabilitiesFilePath">
15
    /// Capabilities file that defines the built-in functions and other language features that policies may depend on.
16
    /// </param>
17
    /// <param name="cancellationToken">Cancellation token.</param>
18
    /// <returns>Compiled OPA bundle stream.</returns>
19
    public static Task<Stream> CompileBundle(
20
        this IRegoCompiler compiler,
21
        string bundlePath,
22
        IEnumerable<string>? entrypoints = null,
23
        string? capabilitiesFilePath = null,
24
        CancellationToken cancellationToken = default)
25
    {
74✔
26
        ArgumentException.ThrowIfNullOrEmpty(bundlePath);
74✔
27

28
        var p = new CompilationParameters
74!
29
        {
74✔
30
            IsBundle = true,
74✔
31
            Entrypoints = entrypoints?.ToHashSet(),
74✔
32
            CapabilitiesFilePath = capabilitiesFilePath,
74✔
33
        };
74✔
34

35
        return compiler.Compile(bundlePath, p, cancellationToken);
74✔
36
    }
74✔
37

38
    /// <summary>
39
    /// Compiles OPA bundle from rego policy source file.
40
    /// </summary>
41
    /// <param name="compiler">Compiler instance.</param>
42
    /// <param name="sourceFilePath">Source file path.</param>
43
    /// <param name="entrypoints">Which documents (entrypoints) will be queried when asking for policy decisions.</param>
44
    /// <param name="cancellationToken">Cancellation token.</param>
45
    /// <returns>Compiled OPA bundle stream.</returns>
46
    public static Task<Stream> CompileFile(
47
        this IRegoCompiler compiler,
48
        string sourceFilePath,
49
        IEnumerable<string>? entrypoints = null,
50
        CancellationToken cancellationToken = default)
51
    {
30✔
52
        ArgumentException.ThrowIfNullOrEmpty(sourceFilePath);
30✔
53

54
        var p = new CompilationParameters
30✔
55
        {
30✔
56
            IsBundle = false,
30✔
57
            Entrypoints = entrypoints?.ToHashSet(),
30✔
58
        };
30✔
59

60
        return compiler.Compile(sourceFilePath, p, cancellationToken);
30✔
61
    }
30✔
62

63
    /// <summary>
64
    /// Compiles OPA bundle from rego bundle stream.
65
    /// </summary>
66
    /// <param name="compiler">Compiler instance.</param>
67
    /// <param name="bundle">Rego bundle stream.</param>
68
    /// <param name="entrypoints">Which documents (entrypoints) will be queried when asking for policy decisions.</param>
69
    /// <param name="capabilitiesJson">
70
    /// Capabilities json that defines the built-in functions and other language features that policies may depend on.
71
    /// </param>
72
    /// <param name="cancellationToken">Cancellation token.</param>
73
    /// <returns>Compiled OPA bundle stream.</returns>
74
    public static Task<Stream> CompileStream(
75
        this IRegoCompiler compiler,
76
        Stream bundle,
77
        IEnumerable<string>? entrypoints = null,
78
        Stream? capabilitiesJson = null,
79
        CancellationToken cancellationToken = default)
80
    {
36✔
81
        ArgumentNullException.ThrowIfNull(bundle);
36✔
82

83
        var p = new CompilationParameters
36!
84
        {
36✔
85
            IsBundle = true,
36✔
86
            Entrypoints = entrypoints?.ToHashSet(),
36✔
87
            CapabilitiesStream = capabilitiesJson,
36✔
88
        };
36✔
89

90
        return compiler.Compile(bundle, p, cancellationToken);
36✔
91
    }
36✔
92

93
    /// <summary>
94
    /// Compiles OPA bundle from rego policy source code.
95
    /// </summary>
96
    /// <param name="compiler">Compiler instance.</param>
97
    /// <param name="source">Source file path.</param>
98
    /// <param name="entrypoints">Which documents (entrypoints) will be queried when asking for policy decisions.</param>
99
    /// <param name="cancellationToken">Cancellation token.</param>
100
    /// <returns>Compiled OPA bundle stream.</returns>
101
    public static async Task<Stream> CompileSource(
102
        this IRegoCompiler compiler,
103
        string source,
104
        IEnumerable<string>? entrypoints = null,
105
        CancellationToken cancellationToken = default)
106
    {
8✔
107
        ArgumentException.ThrowIfNullOrEmpty(source);
8✔
108

109
        using var bundle = new MemoryStream();
8✔
110
        var bw = new BundleWriter(bundle);
8✔
111

112
        await using (bw.ConfigureAwait(false))
8✔
113
        {
8✔
114
            bw.WriteEntry(source, "policy.rego");
8✔
115
        }
8✔
116

117
        bundle.Seek(0, SeekOrigin.Begin);
8✔
118

119
        var p = new CompilationParameters
8!
120
        {
8✔
121
            IsBundle = true,
8✔
122
            Entrypoints = entrypoints?.ToHashSet(),
8✔
123
        };
8✔
124

125
        return await compiler.Compile(bundle, p, cancellationToken).ConfigureAwait(false);
8✔
126
    }
4✔
127
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc