This release is characterized by a major syntax change. We don't take such changes lightly. The new syntax makes the language more regular (and simpler to parse), prepares it for some key features, and, we believe, makes it easier to understand. This is also a major step towards stabilizing the grammar of the language to the extent we can start guaranteeing backwards compatibility.
The old syntax is still supported. The cue fmt
will rewrite your files from the old syntax to the new one.
Colons instead of spaces as separators for shorthands
CUE allows single-field structs to be written without curly braces if they are declared at another field. In the old syntax
a: {
b: {
c: 4
}
}
could be written as
a b c: 4
In the new syntax, this is written as
a: b: c: 4
Aside from enabling some new features and simplifying the parser, this syntax also allows the shorthand to be used for both definitions and regular fields. For instance,
a: b:: c: int
previous had to be written as
a: {
b:: { c: int }
}
Bulk optional fields
The new syntax now allows optional fields to be specified in bulk:
// Define a map from string key to numbers
numMap: [string]: number
// Apply constraint T to all fields with a name ending in "Test"
[=~"Test$"]: T
Using [string]: number
is very similar to using a "bind label" previously. More on that later.
The old syntax for optional fields
foo?: bar
is now just syntactic sugar for
["foo"]: bar
Label filters are the equivalent of patternProperties
in JSON schema.
Note that using colons as separators, which often have a connotation of defining something, interacts well with this language construct. For instance, a map of maps of ints is written as:
[string]: [string]: int
More aliasing
Shadowing of fields is a common issue in CUE (or similar languages, for that matter). CUE already supported various mechanisms to deal with this. For instance, in this example an alias is used to reach the outer a
:
A=a
a: c: a: A.b
b: 4
Other tricks up CUE sleeves were quoted identifiers and allowing to make field non-referable using double quotes. Together these tricks proved both cumbersome and inadequate. The existing techniques also proved to be hard to handle for code generators and rewriters.
CUE now extends the aliasing model to field and label aliases. A field alias,
X=foo: bar
binds X
to bar
just as foo
does. This allows the above to be written as
X=a: c: a: X.b
b: 4
This is not a great win, bit in many cases it will be. Moreover, it allows defining references to fields that can otherwise not be referenced, such as
X="foo-bar": baz
Y="\(x)Test": y
CUE now also allows label aliases, which are placed inside the square brackets:
[X=string]: { name: X }
These are only visible within the field's value. This replaces the "bind" labels, such as <Name>: { name: Name }
. These are essentially the same, but explained differently and with a different syntax. The old syntax makes CUE hard to parse and is less flexible and will be phased out.
Packaging
Another change is the way the cue
tool manages packaging. The cue.mod
file and pkg
directory are replaced with a cue.mod
directory marking the root of a CUE module and with contents managed by the cue
tool, analogously to how the git
tool manages the .git
directory.
See https://cuelang.org/docs/concepts/packages for more details.
Deprecations
Fields names starting with double underscore __
This was defined in the language specification, but previously not enforced. It now is. Field aliases make it easier to work around this restriction.
<X>
syntax
Bind labels, or templates, are being replaced by the new optional field construct.
cue fmt
will rewrite the old format to the new one.
Back-quoted identifiers
Back-quoted identifiers (i.e. foo-bar
) were introduced to improve reachability of fields in case of shadowing. But they only solve half the problem. Field proved to be a more general solution, making back-quoted identifiers a redundant part of the language. As they are not trivial, they will be removed. As for now, the cue
tool will rewrite programs using back-quoted identifiers using aliases.
Changelog
fe8ab34 Fix typo (#164)
2cd4162 README.md: bump supported go version to 1.12+
391e7ae Readme.md: add GolangCI badge
9707302 cmd/cue/cmd: added cue mod init command
fd127fe cmd/cue/cmd: clarify location for commands in error message
f979aa6 cmd/cue/cmd: correct module output
329cd5b cmd/cue/cmd: get go: convert Go block comments
29f70f2 cmd/cue/cmd: report error for certain deprecated syntax
72b36fb cmd/cue/cmd: update more examples to the new format
26a1a0e cmd/cue/cmd: update trim documentation to new syntax
b763fde cmd/cue: rewrite quoted identifiers
9752844 cue/ast: add NewList helper
4759dd2 cue/format: formatting of nested single-line fields
0b4f016 cue/load: allow cue.mod to be directory
b34bce7 cue/load: allow cue.mod to be directory
b13155b cue/load: make cue.mod the default desitination
60b298b cue/load: revert: report when "tool" pkg is used in non-tool file
312fca9 cue/parser: disallow declarations with __foo identifiers as per spec
7057dde cue/parser: fix comment attachement issue
4177df9 cue/parser: fix comment attachement issues
98b8d2f cue/parser: record position of deprecated feature
fa59c10 cue: generate builtins to update task documentation
8192b54 cue: generate new-style maps when exporting
ff34272 cue: implement key filters using new syntax
300af3e cue: introduce colon-separated labels
6d8c95d cue: more conversions to new style templates
467d7bc cue: move to square brackets
39df6c9 doc/ref/spec.md: bug fix: add solidus to escaped_char
c7791ac doc/ref/spec.md: introduce colons as separators
9ffcbbc doc/ref/spec.md: write out spec for aliases and optional field sets
2437f9d doc/tutorial/kubernetes: update formatting
23623fa doc/tutorial/kubernetes: update to new syntax