open-policy-agent/opa

Allow custom builtin function to provide a description

Closed this issue · 2 comments

What is the underlying problem you're trying to solve?

I would like to generate documentation for the custom builtin rego function provided by a certain project, see example. To achieve this, I can iterate through the ast.Builtins slice to extract information about each function, such as name, args, and results. From that, I can format the data as needed. The limitation I'm having is not being able to set a description for the custom rego functions.

Describe the solution

When registering a builtin function, allow a description to be set, maybe here. So that custom builtin functions have the Description attribute set.

A workaround is to call ast.RegisterBuiltin directly, e.g.:

rego.RegisterBuiltin1(&decl, ...)
ast.RegisterBuiltin(&ast.Builtin{
    Name:             decl.Name,
    Description:      "My descripition",
    Decl:             decl.Decl,
    Nondeterministic: decl.Nondeterministic,
})

Seems fine to update the Function struct and RegisterBuiltin* logic to include a description.