ir: Add end locations in IR plan statements - #9007
Conversation
|
|
||
| // SetLocation sets the Location for a given Stmt. | ||
| func (l *Location) SetLocation(index, row, col int, file, text string) { | ||
| func (l *Location) SetLocation(index, row, col int, file string, text []byte) { |
There was a problem hiding this comment.
This is a breaking change, but it seemed acceptable given:
- IR plan integration point is intended to be not go.
- text []byte is a simple change for any callers
- text in location is []byte
There was a problem hiding this comment.
Switching text to []byte changes what Pretty prints, pretty.go dumps the struct with %+v, and Go renders a []byte as numbers rather than text.
A String() method on ir.Location would fix it without giving up the []byte change, since %+v picks it up automatically.
There was a problem hiding this comment.
Hmm, interesting... I have added 26ade847c to address. it's not ideal but adding String() on the location doesn't work since it's embedded into Stmt and so String set there risks only showing the Location string representation.
07eb8f2 to
a03c2fa
Compare
| got := strings.ReplaceAll(string(planBytes), root, "TEMPDIR") | ||
|
|
||
| expected := `{"static":{"strings":[{"value":"result"},{"value":"p"},{"value":"1"},{"value":"test"}],"files":[{"value":"TEMPDIR/test.rego"}]},"plans":{"plans":[{"name":"test","blocks":[{"stmts":[{"type":"MakeObjectStmt","stmt":{"target":2,"file":0,"col":0,"row":0}},{"type":"BlockStmt","stmt":{"blocks":[{"stmts":[{"type":"CallStmt","stmt":{"func":"g0.data.test.p","args":[{"type":"local","value":0},{"type":"local","value":1}],"result":3,"file":0,"col":0,"row":0}},{"type":"ObjectInsertStmt","stmt":{"key":{"type":"string_index","value":1},"value":{"type":"local","value":3},"object":2,"file":0,"col":0,"row":0}}]}],"file":0,"col":0,"row":0}},{"type":"BlockStmt","stmt":{"blocks":[{"stmts":[{"type":"BlockStmt","stmt":{"blocks":[{"stmts":[{"type":"DotStmt","stmt":{"source":{"type":"local","value":1},"key":{"type":"string_index","value":3},"target":5,"file":0,"col":0,"row":0}},{"type":"ObjectMergeStmt","stmt":{"a":5,"b":2,"target":4,"file":0,"col":0,"row":0}},{"type":"BreakStmt","stmt":{"index":1,"file":0,"col":0,"row":0}}]}],"file":0,"col":0,"row":0}},{"type":"AssignVarStmt","stmt":{"source":{"type":"local","value":2},"target":4,"file":0,"col":0,"row":0}}]}],"file":0,"col":0,"row":0}},{"type":"AssignVarStmt","stmt":{"source":{"type":"local","value":4},"target":6,"file":0,"col":0,"row":0}},{"type":"MakeObjectStmt","stmt":{"target":7,"file":0,"col":0,"row":0}},{"type":"ObjectInsertStmt","stmt":{"key":{"type":"string_index","value":0},"value":{"type":"local","value":6},"object":7,"file":0,"col":0,"row":0}},{"type":"ResultSetAddStmt","stmt":{"value":7,"file":0,"col":0,"row":0}}]}]}]},"funcs":{"funcs":[{"name":"g0.data.test.p","params":[0,1],"return":2,"blocks":[{"stmts":[{"type":"ResetLocalStmt","stmt":{"target":3,"file":0,"col":4,"row":3}},{"type":"MakeNumberRefStmt","stmt":{"file":0,"col":4,"row":3,"index":2,"Index":2,"target":4}},{"type":"AssignVarOnceStmt","stmt":{"source":{"type":"local","value":4},"target":3,"file":0,"col":4,"row":3}}]},{"stmts":[{"type":"IsDefinedStmt","stmt":{"source":3,"file":0,"col":4,"row":3}},{"type":"AssignVarOnceStmt","stmt":{"source":{"type":"local","value":3},"target":2,"file":0,"col":4,"row":3}}]},{"stmts":[{"type":"ReturnLocalStmt","stmt":{"source":2,"file":0,"col":4,"row":3}}]}],"path":["g0","test","p"]}]}}` | ||
| expected := `{"static":{"strings":[{"value":"result"},{"value":"p"},{"value":"1"},{"value":"test"}],"files":[{"value":"TEMPDIR/test.rego"}]},"plans":{"plans":[{"name":"test","blocks":[{"stmts":[{"type":"MakeObjectStmt","stmt":{"target":2,"file":0,"col":0,"row":0,"end_col":0,"end_row":0}},{"type":"BlockStmt","stmt":{"blocks":[{"stmts":[{"type":"CallStmt","stmt":{"func":"g0.data.test.p","args":[{"type":"local","value":0},{"type":"local","value":1}],"result":3,"file":0,"col":0,"row":0,"end_col":0,"end_row":0}},{"type":"ObjectInsertStmt","stmt":{"key":{"type":"string_index","value":1},"value":{"type":"local","value":3},"object":2,"file":0,"col":0,"row":0,"end_col":0,"end_row":0}}]}],"file":0,"col":0,"row":0,"end_col":0,"end_row":0}},{"type":"BlockStmt","stmt":{"blocks":[{"stmts":[{"type":"BlockStmt","stmt":{"blocks":[{"stmts":[{"type":"DotStmt","stmt":{"source":{"type":"local","value":1},"key":{"type":"string_index","value":3},"target":5,"file":0,"col":0,"row":0,"end_col":0,"end_row":0}},{"type":"ObjectMergeStmt","stmt":{"a":5,"b":2,"target":4,"file":0,"col":0,"row":0,"end_col":0,"end_row":0}},{"type":"BreakStmt","stmt":{"index":1,"file":0,"col":0,"row":0,"end_col":0,"end_row":0}}]}],"file":0,"col":0,"row":0,"end_col":0,"end_row":0}},{"type":"AssignVarStmt","stmt":{"source":{"type":"local","value":2},"target":4,"file":0,"col":0,"row":0,"end_col":0,"end_row":0}}]}],"file":0,"col":0,"row":0,"end_col":0,"end_row":0}},{"type":"AssignVarStmt","stmt":{"source":{"type":"local","value":4},"target":6,"file":0,"col":0,"row":0,"end_col":0,"end_row":0}},{"type":"MakeObjectStmt","stmt":{"target":7,"file":0,"col":0,"row":0,"end_col":0,"end_row":0}},{"type":"ObjectInsertStmt","stmt":{"key":{"type":"string_index","value":0},"value":{"type":"local","value":6},"object":7,"file":0,"col":0,"row":0,"end_col":0,"end_row":0}},{"type":"ResultSetAddStmt","stmt":{"value":7,"file":0,"col":0,"row":0,"end_col":0,"end_row":0}}]}]}]},"funcs":{"funcs":[{"name":"g0.data.test.p","params":[0,1],"return":2,"blocks":[{"stmts":[{"type":"ResetLocalStmt","stmt":{"target":3,"file":0,"col":4,"row":3,"end_col":9,"end_row":3}},{"type":"MakeNumberRefStmt","stmt":{"file":0,"col":4,"row":3,"end_col":9,"end_row":3,"index":2,"Index":2,"target":4}},{"type":"AssignVarOnceStmt","stmt":{"source":{"type":"local","value":4},"target":3,"file":0,"col":4,"row":3,"end_col":9,"end_row":3}}]},{"stmts":[{"type":"IsDefinedStmt","stmt":{"source":3,"file":0,"col":4,"row":3,"end_col":9,"end_row":3}},{"type":"AssignVarOnceStmt","stmt":{"source":{"type":"local","value":3},"target":2,"file":0,"col":4,"row":3,"end_col":9,"end_row":3}}]},{"stmts":[{"type":"ReturnLocalStmt","stmt":{"source":2,"file":0,"col":4,"row":3,"end_col":9,"end_row":3}}]}],"path":["g0","test","p"]}]}}` |
There was a problem hiding this comment.
These just add the new fields.
This will allow sub line coverage reporting in IR evaluators. Follows/related to open-policy-agent#8752 to allow feature parity with topdown reporting. Signed-off-by: Charlie Egan <charlie_egan@apple.com>
a03c2fa to
53578e1
Compare
|
|
||
| // SetLocation sets the Location for a given Stmt. | ||
| func (l *Location) SetLocation(index, row, col int, file, text string) { | ||
| func (l *Location) SetLocation(index, row, col int, file string, text []byte) { |
There was a problem hiding this comment.
Switching text to []byte changes what Pretty prints, pretty.go dumps the struct with %+v, and Go renders a []byte as numbers rather than text.
A String() method on ir.Location would fix it without giving up the []byte change, since %+v picks it up automatically.
This mean End() will work after unmarshalling too. Signed-off-by: Charlie Egan <charlie_egan@apple.com>
1e437da to
c06efb8
Compare
This is tricky as the type is embedded and so we need to have a custom type to handle the printing of text rather than registering String on Location itself. Signed-off-by: Charlie Egan <charlie_egan@apple.com>
c06efb8 to
fe0cd8c
Compare
|
Thanks! |
Benchmark Comparison (`d00b87c6372bb1b165ac60be2466e15db39eb832` vs `48bb309719d039dd725d0c4ba2614e784ec28596`)
This comment was automatically generated by the benchmarks workflow. |
This will allow sub line coverage reporting in IR evaluators.
Follows/related to #8752 to allow feature parity with topdown reporting.
Helps with addressing issues like this:
Example before
Example after