【cue语言系列学习】base64编解码
release author: ningan123
release time: 2022-08-14
在线运行
三种不同的输出方式,输出的结果是不一样的
本地模拟
base64.cue
[root@master base64]# cat base64.cue
import "encoding/base64"
t1: base64.Encode(null, "foo")
t2: base64.Decode(null, base64.Encode(null, "foo"))
[root@master cuePractice]# cue version
cue version v0.4.3 linux/amd64
[root@master cuePractice]#
[root@master cuePractice]# cue export base64/base64.cue
{
"t1": "Zm9v",
"t2": "Zm9v"
}
[root@master cuePractice]# cue export base64/base64.cue --out json
{
"t1": "Zm9v",
"t2": "Zm9v"
}
[root@master cuePractice]# cue export base64/base64.cue --out yaml
t1: Zm9v
t2: !!binary Zm9v
[root@master cuePractice]#
[root@master cuePractice]# go run base64/base64.go
scheme:
{
t1: "Zm9v"
t2: 'foo'
}
// base64.go
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
cueerror "cuelang.org/go/cue/errors"
)
func main() {
ctx := cuecontext.New()
clusterVal := ctx.NewList()
confPath := "/root/cuePractice/base64"
fileData, err := os.ReadFile(filepath.Join(confPath, "base64.cue"))
if err != nil {
fmt.Println(err.Error())
}
scheme := ctx.CompileBytes(fileData, cue.Scope(clusterVal))
if scheme.Err() != nil {
msg := cueerror.Details(scheme.Err(), nil)
fmt.Println(errors.New(msg))
}
fmt.Printf("scheme: \n%s\n", scheme)
}