go is logging this line fmt.Printf(“🚩 %v”, filters) as [sign share] are those two values in filters which is supposed to be a []string
Yes — if you’re seeing output like:
🚩 [sign share]
then filters
is a []string{"sign", "share"}
.
When you do:
fmt.Printf("🚩 %v", filters)
and filters
is a []string
, Go will print it in the default format:
[elem1 elem2 ...]
If you want to double-check the type or print with quotes (to catch whitespace or weirdness), you could do:
fmt.Printf("🚩 %#v\n", filters) // prints: []string{"sign", "share"}
If this isn’t what you expected filters
to contain, the issue lies in how it’s being populated. Want to trace that next?
func matchesAnyFilter(text string, filters []string) bool { text = strings.ToLower(text) for _, f := range filters { if strings.Contains(text, strings.ToLower(f)) { return true } } return false }
for _, record := range records {
actionText := record.Action
if source := record.Source; source != "" && source != "direct" {
actionText = fmt.Sprintf("%s via %s", record.Action, source)
}
elapsed := now.Sub(record.CreatedAt)
when := humanizeTimeAgo(elapsed)
if len(filters) > 0 && !matchesAnyFilter(actionText, filters) {
continue
}
result := map[string]any{
"action": actionText,
"Location": nil, // TODO: add location
"when": when,
"user_name": record.MemberName,
}
results = append(results, result)
}
i only want to append to results if the actionText has a match in the filters when the filters but its not working
Your code logic is mostly correct, but there’s a likely subtle issue in how actionText
and the filters
are structured or formatted. Let’s isolate what could be going wrong.
matchesAnyFilter
does a case-insensitive Contains
, but doesn’t trim whitespace or normalize." share"
vs "share"
).Update your debug print to catch subtle issues:
fmt.Printf("🚩 filters: %#v, actionText: %q\n", filters, actionText)
Improve matchesAnyFilter
defensively:
func matchesAnyFilter(text string, filters []string) bool {
text = strings.ToLower(strings.TrimSpace(text))
for _, f := range filters {
f = strings.ToLower(strings.TrimSpace(f))
if strings.Contains(text, f) {
return true
}
}
return false
}
Log inside the loop:
fmt.Printf("🧪 checking: %q against filters: %#v\n", actionText, filters)
If nothing gets appended, likely no match is happening — try a simple dummy filter like ["via"]
and confirm it appends.
Want to test this exact path together with a mock record to confirm the behavior?
actionText can be “signed” or “Shared via twitter” or “view” “viewed” etc
Got it — sounds like the filter matching logic is too naive. Here’s the mismatch:
You’re expecting filters like [“sign”, “share”] to match actions like:
"signed"
"Shared via twitter"
But strings.Contains("signed", "sign")
works, while strings.Contains("shared", "share")
does too — but only if cases and spaces line up. More problematic:
actionText
could be "Shared via Twitter"
(capitalized or spaced)"share"
won’t match "Shared"
unless lowercased and substring-compatibleactionText
and filters ✅If your filters are verbs, consider prefix matching instead of contains
:
func matchesAnyFilter(text string, filters []string) bool {
text = strings.ToLower(strings.TrimSpace(text))
for _, f := range filters {
f = strings.ToLower(strings.TrimSpace(f))
if strings.HasPrefix(text, f) || strings.Contains(text, " "+f) {
return true
}
}
return false
}
Or use a simple stemming heuristic:
func matchesAnyFilter(text string, filters []string) bool {
text = strings.ToLower(text)
for _, f := range filters {
f = strings.ToLower(f)
if strings.Contains(text, f) {
return true
}
// crude stem-based match
if strings.HasSuffix(f, "e") && strings.Contains(text, f+"d") {
return true // e.g., "share" matches "shared"
}
if strings.Contains(text, f+"ed") {
return true // e.g., "view" matches "viewed"
}
}
return false
}
Would you prefer to use proper stemming (e.g. porterstemmer
) or keep it heuristic?
text = strings.ToLower(strings.TrimSpace(text)) text will contain things like “viewed via xyx” “shared via zzz” etc, just match with the first word instead of the whole string as it will guaranteed contain the verb, the rest of the string is overmatching with strings we dont need
Perfect — the cleanest fix is to just match filters against the first word of actionText
.
func matchesAnyFilter(text string, filters []string) bool {
words := strings.Fields(strings.ToLower(strings.TrimSpace(text)))
if len(words) == 0 {
return false
}
first := words[0]
for _, f := range filters {
if first == strings.ToLower(strings.TrimSpace(f)) {
return true
}
}
return false
}
"Shared via Twitter"
matches "share"
✅"Viewed via link"
matches "view"
✅"Signed document"
matches "sign"
✅"Uploaded via portal"
doesn’t match if filter is ["view", "share"]
❌This avoids false positives from the "via xyz"
part and keeps intent tight.
Want to add plural/ed-form tolerance (e.g., “share” matching “shared”) too, or keep strict?