安装
系统:centos
安装golang
yum install golang -y
安装cue
Go设置 go get 为国内源 - ChnMig - 博客园 (cnblogs.com)
cue/install.md at master · cue-lang/cue (github.com)
# cue 切换国内源
export GOPROXY=https://goproxy.cn,direct
# 安装cue
go install cuelang.org/go/cmd/cue@latest
# 将cue添加到环境变量
export PATH=/root/go/bin:$PATH
使用参考手册
- CUE (cuelang.org)
- Cue Lang介绍 - 简书 (jianshu.com)
- CUE是何方神圣?_russellgao的博客-CSDN博客
- Introduction | CUE (cuelang.org)
- (38条消息) CUE是何方神圣?_russellgao的博客-CSDN博客
常用命令
1 导出为具体格式的文件
- 转化成:
json cue export json.cue --out json
- 转化成:
yaml cue export json.cue --out yaml
- 转化成 text :
cue export json.cue --out text
- 输出到文件:
cue export json.cue --out json --outfile json.cue.json
示例:定义模板、数据文件,使用cue输出yaml文件
--schema.cue--
#Language: {
tag: string
name: =~"^\\p{Lu}" // Must start with an uppercase letter.
}
languages: [...#Language]
--data.yaml--
languages:
- tag: en
name: English
- tag: nl
name: Dutch
- tag: no
name: Norwegian
# 校验成功的输出
[root@VM-12-11-centos ~]# cue export schema.cue data.yaml --out yaml
languages:
- tag: en
name: English
- tag: nl
name: Dutch
- tag: no
name: Norwegian
# 校验失败输出
[root@VM-12-11-centos ~]# cue export schema.cue data.yaml --out yaml
languages.1.name: invalid value "dutch" (out of bound =~"^\\p{Lu}"):
./schema.cue:3:8
./data.yaml:5:12
2 模板数据校验
基础练习
cue/doc/tutorial/basics at master · cue-lang/cue (github.com)
- json
cue export json.cue
cmp stdout expect-stdout-cue
-- json.cue --
one: 1
two: 2
// A field using quotes.
"two-and-a-half": 2.5
list: [
1,
2,
3,
]
-- expect-stdout-cue --
{
"list": [
1,
2,
3
],
"one": 1,
"two": 2,
"two-and-a-half": 2.5
}
- cue
cue eval dup.cue
cmp stdout expect-stdout-cue
-- dup.cue --
a: 4
a: 4
s: { b: 2 }
s: { c: 2 }
l: [ 1, 2 ]
l: [ 1, 2 ]
-- expect-stdout-cue --
a: 4
s: {
b: 2
c: 2
}
l: [1, 2]
- depulate
cue eval dup.cue
cmp stdout expect-stdout-cue
-- dup.cue --
a: 4
a: 4
s: { b: 2 }
s: { c: 2 }
l: [ 1, 2 ]
l: [ 1, 2 ]
-- expect-stdout-cue --
a: 4
s: {
b: 2
c: 2
}
l: [1, 2]
- constraint
cue eval check.cue
cmp stdout expect-stdout-cue
-- check.cue --
schema: {
name: string
age: int
human: true // always true
}
viola: schema
viola: {
name: "Viola"
age: 38
}
-- expect-stdout-cue --
schema: {
name: string
age: int
human: true
}
viola: {
name: "Viola"
age: 38
human: true
}
- schema
cue export schema.cue
cmp stdout expect-stdout-cue
-- frontmatter.toml --
title = "Definitions"
description = ""
-- text.md --
In CUE, schemas are typically written as Definitions.
A definition is a field which identifier starts with
`#` or `_#`.
This tells CUE that they are to be used for validation and should
not be output as data; it is okay for them to remain unspecified.
A definition also tells CUE the full set of allowed fields.
In other words, definitions define "closed" structs.
Including a `...` in struct keeps it open.
-- schema.cue --
#Conn: {
address: string
port: int
protocol: string
// ... // uncomment this to allow any field
}
lossy: #Conn & {
address: "1.2.3.4"
port: 8888
protocol: "udp"
// foo: 2 // uncomment this to get an error
}
-- expect-stdout-cue --
{
"lossy": {
"address": "1.2.3.4",
"port": 8888,
"protocol": "udp"
}
}
- validation
! cue vet schema.cue data.yaml
cmp stderr expect-stderr
cue eval schema.cue data.yaml
-- frontmatter.toml --
title = "Validation"
description = ""
-- text.md --
Constraints can be used to validate values of concrete instances.
They can be applied to CUE data, or directly to YAML or JSON.
-- schema.cue --
#Language: {
tag: string
name: =~"^\\p{Lu}" // Must start with an uppercase letter.
}
languages: [...#Language]
-- data.yaml --
languages:
- tag: en
name: English
- tag: nl
name: dutch
- tag: no
name: Norwegian
-- expect-stderr --
languages.1.name: invalid value "dutch" (does not match =~"^\\p{Lu}"):
./schema.cue:3:8
./data.yaml:5:12
网友评论