File: finder.go

package info (click to toggle)
golang-github-samber-slog-common 0.0~git20250908.8eb9dfc-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 152 kB
  • sloc: makefile: 34
file content (27 lines) | stat: -rw-r--r-- 584 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package slogcommon

import "log/slog"

func FindAttrByKey(attrs []slog.Attr, key string) (slog.Attr, bool) {
	for i := range attrs {
		if attrs[i].Key == key {
			return attrs[i], true
		}
	}

	return slog.Attr{}, false
}

func FindAttrByGroupAndKey(attrs []slog.Attr, groups []string, key string) (slog.Attr, bool) {
	if len(groups) == 0 {
		return FindAttrByKey(attrs, key)
	}

	for i := range attrs {
		if attrs[i].Key == key && attrs[i].Value.Kind() == slog.KindGroup {
			return FindAttrByGroupAndKey(attrs[i].Value.Group(), groups[1:], key)
		}
	}

	return slog.Attr{}, false
}