Cue相关

作者: 茂茂的小破号 | 来源:发表于2022-06-11 14:38 被阅读0次

安装

系统: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

使用参考手册

常用命令

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)

  1. 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
}
  1. 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]
  1. 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]
  1. 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
}
  1. 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"
    }
}

  1. 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

相关文章

  • Cue相关

    安装 系统:centos 安装golang 安装cueGo设置 go get 为国内源 - ChnMig - 博客...

  • CUE

    我想很多有意义的事做着做着都会让人慢慢觉得枯燥。因为,乐此不疲的活儿没有几个。想想也是,睡觉很爽,但一直睡...

  • 2018-05-11

    如果层间独立, 单cue,无论有效性大小,都可独立建层,有rcb,没有rcc。 mulit-cue,每个cue都可...

  • Cue Lang介绍

    Cue[https://github.com/cuelang/cue],是一种开源语言,用于定义,生成和验证各种数...

  • 2019-06-27

    一文了解MAX3232CUE+T MAX3222 / MAX3232CUE+T / MAX3237 / MA...

  • GAS - Gameplay Cue

    Overview 在gameplay effect的文中提到gameplay cue主要负责释放技能效果时的表现。...

  • Cue in to the value of originali

    摘自:Toastmasters Magazine September Creative speech conten...

  • 突然被cue

    一直都是很低调的人,突然在公众场合被cue到,还挺不习惯的,更别说要发言,啥也没有准备,傻乎乎的,哎…… 下午开会...

  • Feather for Pranayama

    This is a quick and simple pranayama cue. Imagine a feath...

  • 2021-05-08

    日常被导师cue,简直如同坐过山车(ಥ_ಥ)

网友评论

    本文标题:Cue相关

    本文链接:https://www.haomeiwen.com/subject/utoimrtx.html