Attributes are a form of markup in CUE intended for tools building on Cuelang.
cue tool has several reserved attributes
and you can make your own attributes with their own meanings.
Attributes are added to fields and structs as a form of markup. They are not processed during evaluation, only tracked like comments. Nor are they accessible from within CUE and require you to write Go to work with them.
Despite being simple markup, attributes offer significant flexibility and configurability for custom Go tools. They were originally added to help map between different representations like Go and Protobuf. However, you can assign any meaning or behavior to them as they are only interpretable by your applications. In that sense they are a lot like Go struct tags, for the familiar.
Attributes have the following syntax:
- they start with
@, have a label, and parentheses
- appear after fields or in structs
- can have comma separated keys with optional values
- values are strings and can have any format you wish
cue tool has several predefined attributes.
You can run
cue help injection at the command line for quick reference.
@tag() is used for injecting data from the command line
You can use this capability to inject data and control the values which get evaluated.
type option to set interpretation and
short for commonly used values.
@if() is used for including a file when a tag is present
Use this special built tag to include entire files in evaluation.
@go() connect representations
CUE adds struct tags during Go code import.
Currently the following will be carried over (
@embed() has been proposed for embedding other files into CUE
This would work similar to Go’s embedding system.
There are rules for how attributes propagate through unification, structural embedding, and imports. This allows you to add attributes to a definition and they will show up in the values. You can also add to the attributes.
Field attributes do not propagate due to the following issue:
Working with attributes
To work with attributes you have to write Go code.
cue does this for the predefined attributes.
See the go-api/attributes section
for details and code snippets.