str: "hello world"num: 42flt: 3.14// Special field name (and a comment)"k8s.io/annotation": "secure-me"// lists can have different element typeslist: ["a", "b", "c",1,2,3,]obj: { foo: "bar"// reuse another field?! L: list}
hello: "world"hello: "world"// set a types: {a: int}// set some datas: {a: 1, b: 2}// set a nested field without curly bracess: c: d: 3// lists must have the same elements// and cannot change lengthl: ["abc", "123"]l: ["abc","123",]
#Album: { artist: string title: string year: int// ... // 2. uncomment to open and fix error, must be last}// This is a conjunction, it says "album" has to be "#Album"album: #Album & { artist: "Led Zeppelin" title: "Led Zeppelin I" year: 1969// studio: true // 1. uncomment to trigger error}
s: {// field with a default hello: string|*"world"|"apple"// an optional integer count?: int// uncomment to cause failure when not supplied// needed!: _}
cue export default-optional.cue --out json
{
"s": {
"hello": "world" }
}
不完全(Incomplete)和完全 (Concrete)
没有包含所有字段的数据,可以成为不完全的值,CUE 将不会导出未完成的值,而会返回错误。
相反的,完全的值是那些所有字段都被指定的数据。
开放(Open)和封闭 (Close)
开放表示 struct 可以被扩展,而封闭表示不能被扩展。
默认情况下,struct 是开放的,而 definition 是封闭的。
CUE 也支持让我们显式地做相反的声明。
open-closed.cue
// Closed structs: close({ foo: "bar"})// Open definition#d: { foo: "bar" ... // must be last}
#Base: { name: string kind: string}#Meta: {// string and a semver regex version: string&=~"^v[0-9]+\\.[0-9]+\\.[0-9]+$"// list of strings labels: [...string]}#Permissions: { role: string public: bool|*false}// Building up a schema using embeddings#Schema: {// embed other schemas #Base #Meta #Permissions// with no '...' this is final}value: #Schema & { name: "app" kind: "deploy" version: "v1.0.42" labels: ["server", "prod"] role: "backend"// public: false (by default)}
// you can add constraints aftera: 3a: inta: >1// define a struct in one places: { x: int y: int}// define a struct in partss: y: ints: x: int// the above is shorthand// when setting a nested value
order-2.cue
// we can apply constraints after setting dataa: >0&<10// regex implies stringb: =~"[a-z]+"
cue export order.cue order-2.cue --out json
{
"a": 3,
"b": "bb",
"s": {
"x": 1,
"y": 2 }
}
简单的说,合并只是组合、交换,而且是幂等的。
图灵非完备
CUE 是图灵非完备的,意味着不像是通常意义的语言。你只提供值、类型、定义和约束条件,然后 CUE 会告诉你写的是不是正确。
这是有意为之的,而且是基于 Google 多年在管理配置的经验。
核心思想是:
用数据封装代码,而不是用代码封装数据
没有原始的递归或继承
最初的学习曲线值得长期维护
这些限制条件主要灵感是:
Difficulties with Borgcfg and GCL as complexity grew (i.e. object oriented and lambdas)
Lingo and Typed Feature Structure Grammars (managing massive configurations)
Logical and functional languages (various pieces like comprehensions in immutability)
随着 Borgcfg 和 GCL 复杂性不断增加遇到的困难(比如 面向对象和 lambdas)
术语和类型化结构性的语法(管理大规模配置)
逻辑和函数式编程语言(各种部分,像是对不变性的理解)
基于 Golang
CUE 开始于 Go 的一个分支,目的是为了简化作为新语言的启动,
Marcel 在 Google 也是 Go 团队的成员,有很多哲学思想也被延续了下来: