Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to marshal location text #6234

Merged

Conversation

charlieegan3
Copy link
Contributor

@charlieegan3 charlieegan3 commented Sep 20, 2023

Fixes #6213

Here is an example of who this change impacts the output of opa parse. Updating the options in opa parse is not required as part of this PR since it's mainly intended for Go API users. However, I felt that it might make sense to include all the location data for now and see what others thought.

opa $ cat file.rego 
package foo

allow := true
opa $ opa version
Version: 0.56.0
Build Commit: 
Build Timestamp: 
Build Hostname: 
Go Version: go1.21.0
Platform: darwin/arm64
WebAssembly: unavailable
opa $ opa parse file.rego -f json --json-include locations
{
  "package": {
    "location": {
      "file": "file.rego",
      "row": 1,
      "col": 1
    },
    "path": [
      {
        "location": {
          "file": "file.rego",
          "row": 1,
          "col": 9
        },
        "type": "var",
        "value": "data"
      },
      {
        "location": {
          "file": "file.rego",
          "row": 1,
          "col": 9
        },
        "type": "string",
        "value": "foo"
      }
    ]
  },
  "rules": [
    {
      "body": [
        {
          "index": 0,
          "location": {
            "file": "file.rego",
            "row": 3,
            "col": 10
          },
          "terms": {
            "location": {
              "file": "file.rego",
              "row": 3,
              "col": 10
            },
            "type": "boolean",
            "value": true
          }
        }
      ],
      "head": {
        "name": "allow",
        "value": {
          "location": {
            "file": "file.rego",
            "row": 3,
            "col": 10
          },
          "type": "boolean",
          "value": true
        },
        "assign": true,
        "ref": [
          {
            "type": "var",
            "value": "allow"
          }
        ],
        "location": {
          "file": "file.rego",
          "row": 3,
          "col": 1
        }
      },
      "location": {
        "file": "file.rego",
        "row": 3,
        "col": 1
      }
    }
  ]
}
opa $ go run main.go parse file.rego -f json --json-include locations
{
  "package": {
    "location": {
      "file": "file.rego",
      "row": 1,
      "col": 1,
      "text": "cGFja2FnZQ=="
    },
    "path": [
      {
        "location": {
          "file": "file.rego",
          "row": 1,
          "col": 9,
          "text": "Zm9v"
        },
        "type": "var",
        "value": "data"
      },
      {
        "location": {
          "file": "file.rego",
          "row": 1,
          "col": 9,
          "text": "Zm9v"
        },
        "type": "string",
        "value": "foo"
      }
    ]
  },
  "rules": [
    {
      "body": [
        {
          "index": 0,
          "location": {
            "file": "file.rego",
            "row": 3,
            "col": 10,
            "text": "dHJ1ZQ=="
          },
          "terms": {
            "location": {
              "file": "file.rego",
              "row": 3,
              "col": 10,
              "text": "dHJ1ZQ=="
            },
            "type": "boolean",
            "value": true
          }
        }
      ],
      "head": {
        "name": "allow",
        "value": {
          "location": {
            "file": "file.rego",
            "row": 3,
            "col": 10,
            "text": "dHJ1ZQ=="
          },
          "type": "boolean",
          "value": true
        },
        "assign": true,
        "ref": [
          {
            "location": {
              "file": "file.rego",
              "row": 3,
              "col": 1,
              "text": "YWxsb3c="
            },
            "type": "var",
            "value": "allow"
          }
        ],
        "location": {
          "file": "file.rego",
          "row": 3,
          "col": 1,
          "text": "YWxsb3cgOj0gdHJ1ZQ=="
        }
      },
      "location": {
        "file": "file.rego",
        "row": 3,
        "col": 1,
        "text": "YWxsb3cgOj0gdHJ1ZQ=="
      }
    }
  ]
}

ast/marshal/json.go Outdated Show resolved Hide resolved
ast/marshal/json.go Outdated Show resolved Hide resolved
srenatus
srenatus previously approved these changes Sep 20, 2023
ast/parser.go Show resolved Hide resolved
ast/marshal/json.go Outdated Show resolved Hide resolved
srenatus
srenatus previously approved these changes Sep 20, 2023
ast/location/location.go Outdated Show resolved Hide resolved
ast/location/location.go Outdated Show resolved Hide resolved
Signed-off-by: Charlie Egan <charlie@styra.com>
Signed-off-by: Charlie Egan <charlie@styra.com>
This was failing the linter

Signed-off-by: Charlie Egan <charlie@styra.com>
Signed-off-by: Charlie Egan <charlie@styra.com>
Signed-off-by: Charlie Egan <charlie@styra.com>
Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ ✅ ✅

@charlieegan3 charlieegan3 merged commit cd0bf5b into open-policy-agent:main Sep 21, 2023
24 checks passed
@charlieegan3
Copy link
Contributor Author

Thanks Stephan!

@charlieegan3 charlieegan3 deleted the include-location-text branch September 21, 2023 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AST: Allow jsonOptions to include Text field of Location struct
2 participants