【cue语言系列学习】base64编解码

【cue语言系列学习】base64编解码

release author: ningan123
release time: 2022-08-14

在线运行

工具

三种不同的输出方式,输出的结果是不一样的

image-20220812151050020

image-20220812151109172

image-20220812151149851

本地模拟

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'
}

image-20220812150805711

// 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)
}

image-20220812151403446

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安安csdn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值