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

me-viper / OpaDotNet / 13673886962

05 Mar 2025 10:14AM UTC coverage: 82.124% (+0.03%) from 82.091%
13673886962

push

github

me-viper
refactor: Move internal types into corresponding namespace

2056 of 2662 branches covered (77.24%)

Branch coverage included in aggregate %.

3999 of 4711 relevant lines covered (84.89%)

8038.57 hits per line

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

0.0
/src/Compilation/Interop/src/RegoInteropCompiler.cs
1
using System.Runtime.InteropServices;
2

3
using Microsoft.Extensions.Logging;
4
using Microsoft.Extensions.Logging.Abstractions;
5

6
using OpaDotNet.Compilation.Abstractions;
7

8
namespace OpaDotNet.Compilation.Interop;
9

10
/// <summary>
11
/// Compiles OPA bundle with OPA SDK interop wrapper.
12
/// </summary>
13
[PublicAPI]
14
public class RegoInteropCompiler : IRegoCompiler
15
{
16
    private readonly ILogger _logger;
17

18
    /// <summary>
19
    /// Creates new instance of <see cref="RegoInteropCompiler"/> class.
20
    /// </summary>
21
    /// <param name="logger">Logger instance</param>
22
    public RegoInteropCompiler(ILogger<RegoInteropCompiler>? logger = null)
×
23
    {
24
        _logger = logger ?? NullLogger<RegoInteropCompiler>.Instance;
×
25
    }
×
26

27
    private static string NormalizePath(string path) => path.Replace("\\", "/");
×
28

29
    /// <inheritdoc />
30
    public Task<RegoCompilerVersion> Version(CancellationToken cancellationToken = default)
31
    {
32
        var vp = nint.Zero;
×
33

34
        try
35
        {
36
            Interop.OpaGetVersion(out vp);
×
37

38
            if (vp == nint.Zero)
×
39
                throw new RegoCompilationException("Failed to get version");
×
40

41
            var v = Marshal.PtrToStructure<Interop.OpaVersion>(vp);
×
42

43
            var result = new RegoCompilerVersion
×
44
            {
×
45
                Version = v.LibVersion,
×
46
                Commit = v.Commit,
×
47
                Platform = v.Platform,
×
48
                GoVersion = v.GoVersion,
×
49
            };
×
50

51
            return Task.FromResult(result);
×
52
        }
53
        finally
54
        {
55
            if (vp != nint.Zero)
×
56
                Interop.OpaFreeVersion(vp);
×
57
        }
×
58
    }
×
59

60
    /// <inheritdoc />
61
    public Task<Stream> Compile(
62
        string path,
63
        CompilationParameters parameters,
64
        CancellationToken cancellationToken)
65
    {
66
        ArgumentException.ThrowIfNullOrEmpty(path);
×
67
        ArgumentNullException.ThrowIfNull(parameters);
×
68

69
        if (path.StartsWith("./") || path.StartsWith(".\\"))
×
70
            path = path[2..];
×
71

72
        var result = Interop.Compile(
×
73
            NormalizePath(path),
×
74
            parameters,
×
75
            false,
×
76
            _logger
×
77
            );
×
78

79
        return Task.FromResult(result);
×
80
    }
81

82
    /// <inheritdoc />
83
    public async Task<Stream> Compile(
84
        Stream stream,
85
        CompilationParameters parameters,
86
        CancellationToken cancellationToken)
87
    {
88
        ArgumentNullException.ThrowIfNull(stream);
×
89
        ArgumentNullException.ThrowIfNull(parameters);
×
90

91
        Stream? bundle = null;
×
92

93
        try
94
        {
95
            if (!parameters.IsBundle)
×
96
            {
97
                bundle = new MemoryStream();
×
98
                var bw = new BundleWriter(bundle);
×
99

100
                await using (bw.ConfigureAwait(false))
×
101
                    bw.WriteEntry(stream, "policy.rego");
×
102

103
                bundle.Seek(0, SeekOrigin.Begin);
×
104
            }
105

106
            var result = Interop.Compile(
×
107
                bundle ?? stream,
×
108
                parameters,
×
109
                true,
×
110
                _logger
×
111
                );
×
112

113
            return result;
×
114
        }
115
        finally
116
        {
117
            if (bundle != null)
×
118
                await bundle.DisposeAsync().ConfigureAwait(false);
×
119
        }
120
    }
×
121
}
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

© 2026 Coveralls, Inc