protoc-gen-cue

command module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

README

protoc-gen-cue

This protoc plugin generates CUE files.

Conversions

Complies with protojson.

Basic Types
Proto Type CUE Type Comments
map<K, V> { [string]: V } All keys are converted to strings.
repeated V [...V]
bool bool Use default option for defaults.
string string Use default option for defaults.
bytes bytes Use default option for defaults.
int32 int32 Use default option for defaults.
fixed32 int32 Use default option for defaults.
uint32 uint32 Use default option for defaults.
int64 int64 Use default option for defaults.
fixed64 int64 Use default option for defaults.
uint64 uint64 Use default option for defaults.
float float32 Use default option for defaults.
double float64 Use default option for defaults.
Any *null | { "@type": string, ... }
Struct *null | { [string]: _ }
Value *null | _
ListValue *null | [...]
NullValue *null | null
BoolValue *null | bool
StringValue *null | string
Int32Value *null | int32
UInt32Value *null | uint32
Int64Value *null | int64
UInt64Value *null | uint64
FloatValue *null | float32
DoubleValue *null | double
Empty *null | close({})
Timestamp *null | string See the Timestamp section for more information.
Duration *null | string See the Duration section for more information.
FieldMask *null | string
Message
import "github.com/Bestowinc/protoc-gen-cue/pkg/options/cue.proto";

message Foo {
  string name = 1 [(cue.field).expr = '!="xxx"'];
  int32 age = 2 [(cue.field).expr = '<100'];
  int32 age_next_year = 3 [(cue.field).expr = 'age+1'];
}

To:

#Foo: {
  name: string
  name: !="xxx"
  age: int32
  age: <100
  ageNextYear: int32
  ageNextYear: age+1
}
Default Values

You can specify default values using the default field option:

message User {
  bool active = 1 [(cue.field).default = "*true"];
  int32 age = 2 [(cue.field).default = "*18"];
  string role = 3 [(cue.field).default = "*\"user\""];
}

To:

#User: {
  active: *true | bool
  age: *18 | int32
  role: *"user" | string
}
Custom CUE Package

You can specify a custom CUE import path for your proto files:

option (cue.cue_package) = "example.com/schemas/v1";

This allows you to control the CUE import path independently from the Go package path.

Enum
enum Bar {
  ZERO = 0;
  ONE = 1;
}

To:

#Bar: *#Bar_ZERO | #Bar_ONE

#Bar_ZERO: "ZERO"
#Bar_ONE:  "ONE"
Oneof
message Car {
  oneof id {
    string product_name = 1;
    int32 serial_number = 2;
  }
}

To:

#Car: {
  _oneof_id: productName & serialNumber
  productName?: string
  serialNumber?: int32
}
Timestamp

Currently defined by an unconstrained string. This is due to the fact that CUE's built-in time.Time constraint is incompatible with the JSON format defined in the timestamppb. We plan to fix this issue in a future version to follow the original format. See for more details: time.Time on pkg.go.dev and timestamppb.Timestamp

Duration

Currently defined by an unconstrained string. This is due to the fact that CUE's built-in time.Duration constraint is incompatible with the JSON format defined in the durationpb. We plan to fix this issue in a future version to follow the original format. See for more details: time.Duration in pkg.go.dev and descriptorpb.Duration

Optional (proto3)
message Dog {
  optional string nick_name = 1;
}

To:

#Dog: {
  nickName?: string
}

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
example
internal
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL