match.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package dbus
  2. import (
  3. "strconv"
  4. "strings"
  5. )
  6. // MatchOption specifies option for dbus routing match rule. Options can be constructed with WithMatch* helpers.
  7. // For full list of available options consult
  8. // https://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-routing-match-rules
  9. type MatchOption struct {
  10. key string
  11. value string
  12. }
  13. func formatMatchOptions(options []MatchOption) string {
  14. items := make([]string, 0, len(options))
  15. for _, option := range options {
  16. items = append(items, option.key+"='"+option.value+"'")
  17. }
  18. return strings.Join(items, ",")
  19. }
  20. // WithMatchOption creates match option with given key and value
  21. func WithMatchOption(key, value string) MatchOption {
  22. return MatchOption{key, value}
  23. }
  24. // doesn't make sense to export this option because clients can only
  25. // subscribe to messages with signal type.
  26. func withMatchType(typ string) MatchOption {
  27. return WithMatchOption("type", typ)
  28. }
  29. // WithMatchSender sets sender match option.
  30. func WithMatchSender(sender string) MatchOption {
  31. return WithMatchOption("sender", sender)
  32. }
  33. // WithMatchSender sets interface match option.
  34. func WithMatchInterface(iface string) MatchOption {
  35. return WithMatchOption("interface", iface)
  36. }
  37. // WithMatchMember sets member match option.
  38. func WithMatchMember(member string) MatchOption {
  39. return WithMatchOption("member", member)
  40. }
  41. // WithMatchObjectPath creates match option that filters events based on given path
  42. func WithMatchObjectPath(path ObjectPath) MatchOption {
  43. return WithMatchOption("path", string(path))
  44. }
  45. // WithMatchPathNamespace sets path_namespace match option.
  46. func WithMatchPathNamespace(namespace ObjectPath) MatchOption {
  47. return WithMatchOption("path_namespace", string(namespace))
  48. }
  49. // WithMatchDestination sets destination match option.
  50. func WithMatchDestination(destination string) MatchOption {
  51. return WithMatchOption("destination", destination)
  52. }
  53. // WithMatchArg sets argN match option, range of N is 0 to 63.
  54. func WithMatchArg(argIdx int, value string) MatchOption {
  55. if argIdx < 0 || argIdx > 63 {
  56. panic("range of argument index is 0 to 63")
  57. }
  58. return WithMatchOption("arg"+strconv.Itoa(argIdx), value)
  59. }
  60. // WithMatchArgPath sets argN path match option, range of N is 0 to 63.
  61. func WithMatchArgPath(argIdx int, path string) MatchOption {
  62. if argIdx < 0 || argIdx > 63 {
  63. panic("range of argument index is 0 to 63")
  64. }
  65. return WithMatchOption("arg"+strconv.Itoa(argIdx)+"path", path)
  66. }
  67. // WithMatchArg0Namespace sets arg0namespace match option.
  68. func WithMatchArg0Namespace(arg0Namespace string) MatchOption {
  69. return WithMatchOption("arg0namespace", arg0Namespace)
  70. }
  71. // WithMatchEavesdrop sets eavesdrop match option.
  72. func WithMatchEavesdrop(eavesdrop bool) MatchOption {
  73. return WithMatchOption("eavesdrop", strconv.FormatBool(eavesdrop))
  74. }