Skip to content

Instantly share code, notes, and snippets.

@anderseknert
Created February 26, 2025 11:19
Show Gist options
  • Save anderseknert/31adc7f42732fbc86d324842fe86a212 to your computer and use it in GitHub Desktop.
Save anderseknert/31adc7f42732fbc86d324842fe86a212 to your computer and use it in GitHub Desktop.
From AST JSON to Rego
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"github.com/open-policy-agent/opa/v1/ast"
"github.com/open-policy-agent/opa/v1/format"
)
func main() {
// Read AST JSON from stdin
b, err := io.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
// Unmarshal to *Module
var module ast.Module
if err = json.Unmarshal(b, &module); err != nil {
panic(err)
}
// Format to Rego
f := format.MustAstWithOpts(&module, format.Opts{RegoVersion: ast.RegoV1})
fmt.Println(string(f))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment