Skip to content

Commit 2a5192e

Browse files
committed
Minor changes to note support
* Check if node is non-nil to prevent panic * Omit empty message field from API response * Use native string type for message fields
1 parent dbfccf1 commit 2a5192e

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

ast/builtins.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ var DefaultBuiltins = [...]*Builtin{
133133
// HTTP
134134
HTTPSend,
135135

136-
//Trace
136+
// Tracing
137137
Trace,
138138
}
139139

@@ -980,7 +980,7 @@ var HTTPSend = &Builtin{
980980
* Trace
981981
*/
982982

983-
// Trace built-in for supporting debug messages
983+
// Trace prints a note that is included in the query explanation.
984984
var Trace = &Builtin{
985985
Name: "trace",
986986
Decl: types.NewFunction(

docs/book/language-reference.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ evaluation query will always return the same value.
144144
| ------- |--------|-------------|
145145
| <span class="opa-keep-it-together">``http.send(request, output)``</span> | 1 | ``http.send`` executes a HTTP request and returns the response.``request`` is an object containing keys ``method``, ``url`` and optionally ``body``. For example, ``http.send({"method": "get", "url": "http://www.openpolicyagent.org/"}, output)``. ``output`` is an object containing keys ``status``, ``status_code`` and ``body`` which represent the HTTP status, status code and response body respectively. Sample output, ``{"status": "200 OK", "status_code": 200, "body": null``}|
146146

147-
### <a name="trace"/>Debug Trace
147+
### <a name="debugging"/>Debugging
148148
| Built-in | Inputs | Description |
149149
| ------- |--------|-------------|
150-
| <span class="opa-keep-it-together">``trace("debug message")``</span> | 1 | ``trace`` outputs the debug message in the ``Note`` if tracing is enabled. For example, ``trace("Hello There!")`` includes ``Note "Hello There!"`` in the trace. To print variable values, include sprintf in the trace message. For example, ``person = "Bob" trace(sprintf("Hello There! %v", [person]))`` will return ``Note "Hello There! Bob"``|
150+
| <span class="opa-keep-it-together">``trace(string)``</span> | 1 | ``trace`` outputs the debug message ``string`` as a ``Note`` event in the query explanation. For example, ``trace("Hello There!")`` includes ``Note "Hello There!"`` in the query explanation. To print variables, use sprintf. For example, ``person = "Bob"; trace(sprintf("Hello There! %v", [person]))`` will emit ``Note "Hello There! Bob"``. |
151151

152152
## <a name="reserved-names"></a> Reserved Names
153153

server/types/types.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,13 @@ func newRawTraceV1(trace []*topdown.Event) (TraceV1, error) {
242242
Op: strings.ToLower(string(trace[i].Op)),
243243
QueryID: trace[i].QueryID,
244244
ParentID: trace[i].ParentID,
245-
Type: ast.TypeName(trace[i].Node),
246-
Node: trace[i].Node,
247245
Locals: NewBindingsV1(trace[i].Locals),
248246
Message: trace[i].Message,
249247
}
248+
if trace[i].Node != nil {
249+
result[i].Type = ast.TypeName(trace[i].Node)
250+
result[i].Node = trace[i].Node
251+
}
250252
}
251253

252254
b, err := json.Marshal(result)
@@ -276,7 +278,7 @@ type TraceEventV1 struct {
276278
Type string `json:"type"`
277279
Node interface{} `json:"node"`
278280
Locals BindingsV1 `json:"locals"`
279-
Message ast.String `json:"message"`
281+
Message string `json:"message,omitempty"`
280282
}
281283

282284
// UnmarshalJSON deserializes a TraceEventV1 object. The Node field is

topdown/trace.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const (
3636
// FailOp is emitted when an expression evaluates to false.
3737
FailOp Op = "Fail"
3838

39-
//NoteOp is emitted when trace is requested
39+
// NoteOp is emitted when an expression invokes a tracing built-in function.
4040
NoteOp Op = "Note"
4141
)
4242

@@ -47,7 +47,7 @@ type Event struct {
4747
QueryID uint64 // Identifies the query this event belongs to.
4848
ParentID uint64 // Identifies the parent query this event belongs to.
4949
Locals *ast.ValueMap // Contains local variable bindings from the query context.
50-
Message ast.String
50+
Message string // Contains message for Note events.
5151
}
5252

5353
// HasRule returns true if the Event contains an ast.Rule.
@@ -149,7 +149,7 @@ func PrettyTrace(w io.Writer, trace []*Event) {
149149
func formatEvent(event *Event, depth int) string {
150150
padding := formatEventPadding(event, depth)
151151
if event.Op == NoteOp {
152-
return fmt.Sprintf("%v%v %v", padding, event.Op, event.Message)
152+
return fmt.Sprintf("%v%v %q", padding, event.Op, event.Message)
153153
}
154154
return fmt.Sprintf("%v%v %v", padding, event.Op, event.Node)
155155
}
@@ -205,7 +205,7 @@ func builtinTrace(bctx BuiltinContext, args []*ast.Term, iter func(*ast.Term) er
205205
Op: NoteOp,
206206
QueryID: bctx.QueryID,
207207
ParentID: bctx.ParentID,
208-
Message: str,
208+
Message: string(str),
209209
}
210210
bctx.Tracer.Trace(evt)
211211

0 commit comments

Comments
 (0)