问题描述
有没有办法在我想创建的新内置函数中调用内置函数(如 io.jwt.decode_verify(string,constraints))?
解决方法
您应该能够使用从 topdown.GetBuiltin
函数导出的内置函数。在下面的示例中,base64.encode
函数是从 my_custom
自定义内置函数调用的:
rego.RegisterBuiltin2(
®o.Function{
Name: "my_custom",Decl: types.NewFunction(types.Args(types.S),types.A),},func(bctx rego.BuiltinContext,a,b *ast.Term) (*ast.Term,error) {
b64encode := topdown.GetBuiltin("base64.encode")
if b64encode == nil {
panic("base64.encode missing")
}
var res *ast.Term
if err := b64encode(bctx,[]*ast.Term{a},func(encoded *ast.Term) error {
res = encoded
return nil
}); err != nil {
return nil,err
}
return res,nil
},)