登录
首页 >  Golang >  Go问答

如何在开放策略代理的另一个内置中使用内置

来源:stackoverflow

时间:2024-04-28 17:51:26 129浏览 收藏

IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《如何在开放策略代理的另一个内置中使用内置》,聊聊,我们一起来看看吧!

问题内容

有没有办法在我想要创建的新内置函数中调用内置函数(例如 io.jwt.decode_verify(string,constraints))?

或者有没有办法调用OPA内部包的方法?


解决方案


您应该能够使用从 topdown.getbuiltin 函数导出的内置函数。在下面的示例中,从 my_custom 自定义内置函数调用 base64.encode 函数:

    rego.RegisterBuiltin2(
        &rego.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
        },
    )

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>