Skip to content

Repository files navigation

cue-dotnet

.NET 10 bindings and code-generation tooling for CUE

See LICENSE in this repository and the license of the libcue/CUE project for the respective licensing terms.

Project structure

Warning

cue-dotnet depends on the separate libcue project. The native library must be built first and copied to the root of this repository before building, testing, or running the generator.

This project integrates with the native Go implementation of cue through a CGO adapter forked from libcue:

flowchart LR
    A["cuelang/cue<br/>Go"]
    B["intresrl/libcue<br/>Go + CGO"]
    D["this repository<br/>Cue.Api<br/>P/Invoke"]
    E[".NET consumers"]
    F["this repository<br/>Cue.Generator<br/>C#"]
    G["Generated C#"]

    A --> B --> D
    D --> E
    D --> F --> G
Loading

This repository has the following layout:

  • Cue.Api --- managed .NET API over libcue using P/Invoke.
  • Cue.Generator --- CLI that compiles CUE schemas and generates C# code.
  • Examples --- sample CUE schemas, generated C# files, andgenerator debug output.

Building

Prerequisites

  • .NET 10 SDK.
  • Go 1.25.0: CGO must be enabled and a compatible C compiler must be installed.

To check CGO run:

go env CGO_ENABLED # should output '1'

Building libcue

A convenient checkout layout is:

<your_clone_directory>/
├── libcue/
└── cue-dotnet/

The output of the libcue build must be generated or copied into the root of cue-dotnet.

On Linux:

cd libcue && go build -buildmode=c-shared -o ../cue-dotnet/libcue.so

On Windows (Git Bash, msys2 or similar):

Caution

On Windows, keep the "lib" prefix in libcue.dll to avoid overwriting cue.h in libcue.

cd libcue && go build -buildmode=c-shared -o ../cue-dotnet/libcue.dll

Build cue-dotnet

After the native dependency is present in the repository root:

dotnet restore
dotnet build

Warning

Run a rebuild every time you make changes in libcue. The DLL or shared library is copied in the output directory of Cue.Api.

Cue.Api (CUE to dotnet adapter library)

CUE operations begin with a context:

using Cuelang.Cue;

using var ctx = new CueContext();
using var value = ctx.Compile("name: string");

CueContext owns the native CUE context while Value represents a managed wrapper around a native CUE value.

Keep the context alive for the lifetime of values created from it and dispose native-backed objects appropriately.

Cue.Generator

Cue.Generator is a .NET CLI that compiles a CUE schema and generates C# source.

You may execute it like this:

# dotnet run --project Cue.Generator -- <input.cue> <output.cs>
dotnet run --project Cue.Generator -- Examples/simple.cue generated.cs

An optional debug output path can be supplied via the --debug parameter.

Generator concepts

The current implementation and tests cover CUE concepts including:

  • structs;
  • lists;
  • definitions;
  • references;
  • nullable values;
  • disjunctions;
  • constrained primitive values;
  • expressions;
  • discriminated alternatives;
  • matchN expressions.

The generated representation can model alternatives as interfaces and record implementations instead of arbitrarily reducing a CUE disjunction to one type.

About

P/Invoke integration for libcue and CUE to C# transpiler for CUE schema definitions

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages