CUE基础语法

参考

https://kubevela.io/zh/docs/platform-engineers/cue/basic/

CUE的执行模式


CUE 的执行模型:声明式而非命令式

CUE 是一种声明式语言,而不是传统的命令式编程语言(如 Python 或 Go)。这意味着:


代码分析

import (
    "vela/op"
    "encoding/json"
)

request: {
    alias: ""
    annotations: {}
    attributes: {}
    description: "Send request to the url"
    labels: {}
    type: "workflow-step"
}

template: {
    http: op.#HTTPDo & {
        method: parameter.method
        url:    parameter.url
        request: {
            if parameter.body != _|_ {
                body: json.Marshal(parameter.body)
            }
            if parameter.header != _|_ {
                header: parameter.header
            }
        }
    }
    fail: op.#Steps & {
        if http.response.statusCode > 400 {
            requestFail: op.#Fail & {
                message: "request of \\\\(parameter.url) is fail: \\\\(http.response.statusCode)"
            }
        }
    }
    response: json.Unmarshal(http.response.body)
    parameter: {
        url:    string
        method: *"GET" | "POST" | "PUT" | "DELETE"
        body?: {...}
        header?: [string]: string
    }
}

CUE 的求值过程

代码中的“执行顺序”

尽管 CUE 本身没有严格的“从上往下”执行,但在 KubeVela 工作流中,这些字段会被解释为一个逻辑步骤序列。具体到代码,在 KubeVela 的运行时(而不是 CUE 的求值时),逻辑顺序如下: