filter.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package tinymq
  2. import (
  3. "reflect"
  4. "regexp"
  5. "runtime"
  6. "strings"
  7. )
  8. // 匹配所有的频道
  9. func AllChannelFilter() FilterFunc {
  10. return func(conn *Line) (ok bool) {
  11. return true
  12. }
  13. }
  14. // 正则频道过滤器
  15. func RegChannelFilter(channel *regexp.Regexp) FilterFunc {
  16. return func(conn *Line) (ok bool) {
  17. return channel.MatchString(conn.channel)
  18. }
  19. }
  20. // 字符串频道过滤器
  21. func StrChannelFilter(channel string) FilterFunc {
  22. return func(conn *Line) (ok bool) {
  23. return strings.Contains(conn.channel, channel)
  24. }
  25. }
  26. // 开始字符串频道过滤器
  27. func StrPrefixFilter(channel string) FilterFunc {
  28. return func(conn *Line) (ok bool) {
  29. return strings.HasPrefix(conn.channel, channel)
  30. }
  31. }
  32. // 包括名称的频道过滤器
  33. func NamePrefixFilter(name string, channel string) FilterFunc {
  34. c := name + "@" + channel
  35. return func(conn *Line) (ok bool) {
  36. return strings.HasPrefix(conn.channel, c)
  37. }
  38. }
  39. // 连接过滤器
  40. func LineLinkFilter(line *Line) FilterFunc {
  41. return func(conn *Line) (ok bool) {
  42. return line == conn
  43. }
  44. }
  45. // GetFunctionName returns the fully qualified name of the function passed as an interface.
  46. func GetFunctionName(temp any) string {
  47. // Use reflect to get the pointer to the function's code.
  48. pc := reflect.ValueOf(temp).Pointer()
  49. // Use runtime.FuncForPC to get function details.
  50. f := runtime.FuncForPC(pc)
  51. if f == nil {
  52. return ""
  53. }
  54. return f.Name()
  55. }