filter.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 StrPrefixSubFilter(channel string, substr string) FilterFunc {
  41. return func(conn *Line) (ok bool) {
  42. return strings.HasPrefix(conn.channel, channel) && strings.Contains(conn.channel, substr)
  43. }
  44. }
  45. // 连接过滤器
  46. func LineLinkFilter(line *Line) FilterFunc {
  47. return func(conn *Line) (ok bool) {
  48. return line == conn
  49. }
  50. }
  51. // GetFunctionName returns the fully qualified name of the function passed as an interface.
  52. func GetFunctionName(temp any) string {
  53. // Use reflect to get the pointer to the function's code.
  54. pc := reflect.ValueOf(temp).Pointer()
  55. // Use runtime.FuncForPC to get function details.
  56. f := runtime.FuncForPC(pc)
  57. if f == nil {
  58. return ""
  59. }
  60. return f.Name()
  61. }