Skip to content

Commit

Permalink
Merge pull request #28 from strongdm/main
Browse files Browse the repository at this point in the history
cedar: change JSON marshaling of the Position struct to use conventional lower case keys
  • Loading branch information
patjakdev authored Sep 5, 2024
2 parents 17b4d78 + a94e3e2 commit 76c9352
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
15 changes: 11 additions & 4 deletions policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,17 @@ func (p *Policy) Effect() Effect {

// A Position describes an arbitrary source position including the file, line, and column location.
type Position struct {
Filename string // optional name of the source file for the enclosing policy, "" if the source is unknown or not a named file
Offset int // byte offset, starting at 0
Line int // line number, starting at 1
Column int // column number, starting at 1 (character count per line)
// Filename is the optional name of the source file for the enclosing policy, "" if the source is unknown or not a named file
Filename string `json:"filename"`

// Offset is the byte offset, starting at 0
Offset int `json:"offset"`

// Line is the line number, starting at 1
Line int `json:"line"`

// Column is the column number, starting at 1 (character count per line)
Column int `json:"column"`
}

// Position retrieves the position of this policy.
Expand Down
18 changes: 18 additions & 0 deletions policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,21 @@ func TestUnmarshalCedarPolicyErr(t *testing.T) {
err := p.UnmarshalCedar([]byte("!@#$"))
testutil.Error(t, err)
}

func TestPositionJSON(t *testing.T) {
t.Parallel()
p := cedar.Position{Filename: "foo.cedar", Offset: 1, Line: 2, Column: 3}

marshaled, err := json.MarshalIndent(p, "", "\t")
testutil.OK(t, err)

var want bytes.Buffer
_ = json.Indent(&want, []byte(`{ "filename": "foo.cedar", "offset": 1, "line": 2, "column": 3 }`), "", "\t")

testutil.Equals(t, string(marshaled), want.String())

var unmarshaled cedar.Position
testutil.OK(t, json.Unmarshal(want.Bytes(), &unmarshaled))

testutil.Equals(t, unmarshaled, p)
}

0 comments on commit 76c9352

Please sign in to comment.