Files
archie/modules/events/internal/match/match.go
T

48 lines
1005 B
Go

// Package match decides whether an event's fields satisfy a trigger's filter.
package match
import (
"encoding/json/jsontext"
json "encoding/json/v2"
"fmt"
"strings"
)
// Fields reports whether every filter entry equals the event field at its
// dotted path ("labels.alertname"). An empty filter matches every event.
func Fields(fields jsontext.Value, filter map[string]string) bool {
if len(filter) == 0 {
return true
}
var doc any
if err := json.Unmarshal(fields, &doc); err != nil {
return false
}
for path, want := range filter {
got, ok := lookup(doc, strings.Split(path, "."))
if !ok || got != want {
return false
}
}
return true
}
func lookup(doc any, path []string) (string, bool) {
for _, key := range path {
obj, ok := doc.(map[string]any)
if !ok {
return "", false
}
if doc, ok = obj[key]; !ok {
return "", false
}
}
switch v := doc.(type) {
case string:
return v, true
case float64, bool:
return fmt.Sprint(v), true
}
return "", false
}