Skip to content

Commit

Permalink
🙈 template: fix tag func
Browse files Browse the repository at this point in the history
  • Loading branch information
rjeczalik committed Jul 7, 2024
1 parent 149d4bb commit da22a2a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
12 changes: 6 additions & 6 deletions pkg/plugin/builtin/event/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ func MakeTags() *Tags {
}
}

func (t *Tags) tag(name string, value ...any) (any, error) {
switch len(value) {
func (t *Tags) tag(name string, values ...any) (any, error) {
switch len(values) {
case 0:
t.mu.Lock()
value, ok := t.m[name]
v, ok := t.m[name]
t.mu.Unlock()
if !ok {
return nil, errors.New("tag not found: %q", name)
}
return value, nil
return v, nil
case 1:
t.mu.Lock()
t.m[name] = value
t.m[name] = values[0]
t.mu.Unlock()
return value, nil
return values[0], nil
default:
return nil, errors.New("too many arguments for tag: %q", name)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/proto/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (t *T) Match(ctx context.Context, data string) func(context.Context, any) (
)

err := tmpl.Execute(&buf, got)
tr.ExecuteMatch(ctx, buf.Bytes(), err)
tr.ExecuteMatch(ctx, []byte(data), buf.Bytes(), err)
if err != nil {
return false, errors.New("failed to evaluate %q: %w", data, err)
}
Expand Down
17 changes: 9 additions & 8 deletions pkg/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (
ParseKey: func(context.Context, *gojq.Query, error) {},
UnmarshalValue: func(context.Context, []byte, any, error) {},
TemplateValue: func(context.Context, string, *template.Template, error) {},
ExecuteMatch: func(context.Context, []byte, error) {},
ExecuteMatch: func(context.Context, []byte, []byte, error) {},
UnmarshalMatch: func(context.Context, []byte, any, error) {},
EqualMatch: func(context.Context, any, any, bool) {},
MatchTimeout: func(context.Context) {},
Expand Down Expand Up @@ -97,9 +97,10 @@ func LogPattern() PatternTrace {
slog.Info("trace: TemplateValue", tags...)
}
},
ExecuteMatch: func(ctx context.Context, p []byte, err error) {
ExecuteMatch: func(ctx context.Context, data, result []byte, err error) {
tags := append(attrs(ctx),
"raw", string(p),
"data", string(data),
"result", string(result),
)
if err != nil {
tags = append(tags, tint.Err(err))
Expand Down Expand Up @@ -356,7 +357,7 @@ type PatternTrace struct {
ParseKey func(context.Context, *gojq.Query, error)
UnmarshalValue func(context.Context, []byte, any, error)
TemplateValue func(context.Context, string, *template.Template, error)
ExecuteMatch func(context.Context, []byte, error)
ExecuteMatch func(context.Context, []byte, []byte, error)
UnmarshalMatch func(context.Context, []byte, any, error)
EqualMatch func(context.Context, any, any, bool)
MatchTimeout func(context.Context)
Expand Down Expand Up @@ -386,9 +387,9 @@ func (pt PatternTrace) Join(extra PatternTrace) PatternTrace {
}
if extra.ExecuteMatch != nil {
fn := pt.ExecuteMatch
pt.ExecuteMatch = func(ctx context.Context, p []byte, err error) {
fn(ctx, p, err)
extra.ExecuteMatch(ctx, p, err)
pt.ExecuteMatch = func(ctx context.Context, data, result []byte, err error) {
fn(ctx, data, result, err)
extra.ExecuteMatch(ctx, data, result, err)
}
}
if extra.UnmarshalMatch != nil {
Expand All @@ -400,7 +401,7 @@ func (pt PatternTrace) Join(extra PatternTrace) PatternTrace {
}
if extra.EqualMatch != nil {
fn := pt.EqualMatch
pt.EqualMatch = func(ctx context.Context, want any, got any, ok bool) {
pt.EqualMatch = func(ctx context.Context, want, got any, ok bool) {
fn(ctx, want, got, ok)
extra.EqualMatch(ctx, want, got, ok)
}
Expand Down

0 comments on commit da22a2a

Please sign in to comment.