Created
February 26, 2025 11:19
-
-
Save anderseknert/31adc7f42732fbc86d324842fe86a212 to your computer and use it in GitHub Desktop.
From AST JSON to Rego
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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