-
Notifications
You must be signed in to change notification settings - Fork 7
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
lib: add evaluation state dump utility type #74
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package lib | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/google/cel-go/cel" | ||
"github.com/google/cel-go/common" | ||
) | ||
|
||
// NewDump returns an evaluation dump that can be used to examine the complete | ||
// set of evaluation states from a CEL program. The program must have been | ||
// constructed with a cel.Env.Program call including the cel.OptTrackState | ||
// evaluation option. The ast and details parameters must be valid for the | ||
// program. | ||
func NewDump(ast *cel.Ast, details *cel.EvalDetails) *Dump { | ||
if ast == nil || details == nil { | ||
return nil | ||
} | ||
return &Dump{ast: ast, det: details} | ||
} | ||
|
||
// Dump is an evaluation dump. | ||
type Dump struct { | ||
ast *cel.Ast | ||
det *cel.EvalDetails | ||
} | ||
|
||
func (d *Dump) String() string { | ||
if d == nil { | ||
return "" | ||
} | ||
var buf strings.Builder | ||
for i, v := range d.NodeValues() { | ||
if i != 0 { | ||
buf.WriteByte('\n') | ||
} | ||
fmt.Fprint(&buf, v) | ||
} | ||
return buf.String() | ||
} | ||
|
||
// NodeValues returns the evaluation results, source location and source | ||
// snippets for the expressions in the dump. The nodes are sorted in | ||
// source order. | ||
func (d *Dump) NodeValues() []NodeValue { | ||
if d == nil { | ||
return nil | ||
} | ||
es := d.det.State() | ||
var values []NodeValue | ||
for _, id := range es.IDs() { | ||
if id == 0 { | ||
continue | ||
} | ||
v, ok := es.Value(id) | ||
if !ok { | ||
continue | ||
} | ||
values = append(values, d.nodeValue(v, id)) | ||
} | ||
sort.Slice(values, func(i, j int) bool { | ||
vi := values[i].loc | ||
vj := values[j].loc | ||
switch { | ||
case vi.Line() < vj.Line(): | ||
return true | ||
case vi.Line() > vj.Line(): | ||
return false | ||
} | ||
switch { | ||
case vi.Column() < vj.Column(): | ||
return true | ||
case vi.Column() > vj.Column(): | ||
return false | ||
default: | ||
// If we are here we have executed more than once | ||
// and have different values, so sort lexically. | ||
// This is not ideal given that values may include | ||
// maps which do not render consistently and so | ||
// we're breaking the sort invariant that comparisons | ||
// will be consistent. For what we are doing this is | ||
// good enough. | ||
return fmt.Sprint(values[i].val) < fmt.Sprint(values[j].val) | ||
} | ||
}) | ||
return values | ||
} | ||
|
||
func (d *Dump) nodeValue(val any, id int64) NodeValue { | ||
v := NodeValue{ | ||
loc: d.ast.NativeRep().SourceInfo().GetStartLocation(id), | ||
src: d.ast.Source(), | ||
val: val, | ||
} | ||
return v | ||
} | ||
|
||
// NodeValue is a CEL expression node value and annotation. | ||
type NodeValue struct { | ||
loc common.Location | ||
src common.Source | ||
val any | ||
} | ||
|
||
func (v NodeValue) MarshalJSON() ([]byte, error) { | ||
type val struct { | ||
Location string `json:"loc"` | ||
Src string `json:"src"` | ||
Val any `json:"val"` | ||
} | ||
var buf bytes.Buffer | ||
enc := json.NewEncoder(&buf) | ||
enc.SetEscapeHTML(false) | ||
err := enc.Encode(val{ | ||
Location: v.Loc(), | ||
Src: v.Src(), | ||
Val: v.val, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
} | ||
|
||
func (v NodeValue) String() string { | ||
return fmt.Sprintf("%s\n%s\n%v\n", v.Loc(), v.Src(), v.Val()) | ||
} | ||
|
||
func (v NodeValue) Val() any { | ||
return v.val | ||
} | ||
|
||
func (v NodeValue) Loc() string { | ||
return fmt.Sprintf("%s:%d:%d", v.src.Description(), v.loc.Line(), v.loc.Column()+1) | ||
} | ||
|
||
func (v NodeValue) Src() string { | ||
snippet, ok := v.src.Snippet(v.loc.Line()) | ||
if !ok { | ||
return "" | ||
} | ||
src := " | " + strings.Replace(snippet, "\t", " ", -1) | ||
ind := "\n | " + strings.Repeat(".", minInt(v.loc.Column(), len(snippet))) + "^" | ||
return src + ind | ||
} | ||
|
||
func minInt(a, b int) int { | ||
if a < b { | ||
return a | ||
} | ||
return b | ||
} |
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are the memory implications of turning this on? Like how big does the
EvalState
become if say the program is processing a large XML response?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would imagine this would be untenably large. The actual size would depend how many allocations were made to perform evaluations. In the worst case this would be the sum of all sizes for each evaluation node. In practice, I would imagine that this is mitigated by sharing between values when there has not been mutation or type conversion that requires a new allocation (e.g.
[]byte
→string
or map construction, though in the latter, the only allocation would be the shallow value). I think we need to experiment to get a completish picture.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what I hoping too, like a copy-on-write paradigm to achieve immutability while being memory efficient.