Skip to content

Commit 6668e86

Browse files
author
Felipe Dutra Tine e Silva
committed
Add context returned for debug
1 parent 4e1336c commit 6668e86

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

tree.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type TreeOptions struct {
1414
StopIfConvertingError bool
1515
Operators map[string]Operator
1616
OverrideExistingOperator bool
17+
context context.Context
1718
}
1819

1920
// Tree represent a Tree
@@ -103,6 +104,10 @@ func (t *Tree) Next(jsonRequest map[string]interface{}, config *TreeOptions) (*T
103104
if t.ctx != nil {
104105
t.ctx = contextValue(t.ctx, n.ID, n.Key, jsonValue, n.Operator, n.Value)
105106
}
107+
108+
if config.context != nil {
109+
config.context = contextValue(config.context, n.ID, n.Key, jsonValue, n.Operator, n.Value)
110+
}
106111
return selected, nil
107112
}
108113
}
@@ -173,6 +178,40 @@ func (t *Tree) Resolve(request map[string]interface{}, options ...func(t *TreeOp
173178
return t.resolve(request, config)
174179
}
175180

181+
// ResolveJSONWithContext calculate which will be the selected node according to the jsonRequest
182+
func (t *Tree) ResolveJSONWithContext(ctx context.Context, jsonRequest []byte, options ...func(t *TreeOptions)) (*Tree, context.Context, error) {
183+
var request map[string]interface{}
184+
err := json.Unmarshal(jsonRequest, &request)
185+
if err != nil {
186+
return nil, ctx, err
187+
}
188+
189+
return t.ResolveWithContext(ctx, request, options...)
190+
}
191+
192+
// ResolveWithContext calculate which will be the selected node according to the map request
193+
func (t *Tree) ResolveWithContext(ctx context.Context, request map[string]interface{}, options ...func(t *TreeOptions)) (*Tree, context.Context, error) {
194+
config := &TreeOptions{
195+
context: ctx,
196+
}
197+
198+
for _, option := range options {
199+
option(config)
200+
}
201+
202+
if len(config.Operators) > 0 {
203+
for k := range config.Operators {
204+
if isExistingOperator(k) {
205+
config.OverrideExistingOperator = true
206+
break
207+
}
208+
}
209+
}
210+
211+
result, err := t.resolve(request, config)
212+
return result, config.context, err
213+
}
214+
176215
func (t *Tree) resolve(request map[string]interface{}, config *TreeOptions) (*Tree, error) {
177216
temp, err := t.Next(request, config)
178217
if err != nil {

0 commit comments

Comments
 (0)